用java写一个 安排10个人上3天班,且一天要上4次休息2次为一个周期
时间: 2023-05-19 19:05:13 浏览: 82
以下是一个可能的 Java 代码实现:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Schedule {
private static final int NUM_PEOPLE = 10;
private static final int NUM_DAYS = 3;
private static final int NUM_SHIFTS_PER_DAY = 4;
private static final int NUM_RESTS_PER_DAY = 2;
private static final int NUM_PERIODS_PER_DAY = NUM_SHIFTS_PER_DAY + NUM_RESTS_PER_DAY;
private static final int NUM_PERIODS = NUM_DAYS * NUM_PERIODS_PER_DAY;
public static void main(String[] args) {
List<Integer> schedule = generateSchedule();
printSchedule(schedule);
}
private static List<Integer> generateSchedule() {
List<Integer> schedule = new ArrayList<>(NUM_PERIODS);
Random random = new Random();
for (int i = 0; i < NUM_PERIODS; i++) {
schedule.add(-1);
}
for (int person = 0; person < NUM_PEOPLE; person++) {
for (int shift = 0; shift < NUM_SHIFTS_PER_DAY; shift++) {
int period;
do {
period = random.nextInt(NUM_PERIODS);
} while (schedule.get(period) != -1);
schedule.set(period, person);
}
}
return schedule;
}
private static void printSchedule(List<Integer> schedule) {
for (int day = 0; day < NUM_DAYS; day++) {
System.out.println("Day " + (day + 1) + ":");
for (int period = 0; period < NUM_PERIODS_PER_DAY; period++) {
int person = schedule.get(day * NUM_PERIODS_PER_DAY + period);
System.out.print("Period " + (period + 1) + ": ");
if (person == -1) {
System.out.println("Rest");
} else {
System.out.println("Person " + (person + 1));
}
}
}
}
}
```
这个程序使用了一个简单的随机算法来生成排班表。首先,它创建了一个长度为 `NUM_PERIODS` 的列表,每个元素代表一个时间段。然后,它循环遍历每个人和每个班次,为每个班次随机分配一个时间段,直到找到一个空闲的时间段为止。最后,它打印出生成的排班表。
阅读全文