题意:
课堂上有n个学生(n<=10)。每个学生都有一个“清醒-睡眠”周期。其中第i个学生醒 ai 分钟后睡bi 分钟,然后重复(1<=ai,bi<=5),初始时第i个学生处在他的周期的第 ci 分钟。每个学生在临睡前会察看全班睡觉人数是否严格大于清醒人数,只有这个条件满足时才睡觉,否则就坚持听课 ai分钟后再次检查这个条件。问经过多长时间后全班都清醒。
思路:
遍历每个学生,修改状态,如果都醒着,停止遍历。
代码:
#include<stdio.h>
#define maxn 1000000
typedef struct Stu {
int a, b, c;
} Stu;
int main() {
int n, flag = 0;
while (scanf("%d", &n) && n) {
Stu stu[n];
for (int i = 0; i < n; i++)
scanf("%d%d%d", &stu[i].a, &stu[i].b, &stu[i].c);
bool ok = false;
int x = 1;
for (; x < maxn; x++) {
int awake = 0;//醒着的人数
for (int i = 0; i < n; i++)
if (stu[i].c <= stu[i].a)
awake++;
if (awake == n) {
ok = true;
break;
}
for (int i = 0; i < n; i++) {
if (stu[i].c == stu[i].a + stu[i].b
|| (stu[i].c == stu[i].a && awake >= n - awake))
stu[i].c = 0;
stu[i].c++;
}
}
printf("Case %d: %d\n", ++flag, ok ? x : -1);
}
return 0;
}