C语言程序:根据条件生成唯一值班表
#include <stdio.h>
int main() { int n; scanf("%d", &n);
// 初始化值班表,0表示未确定值班人员
int schedule[7] = {0, 0, 0, 0, 0, 0, 0};
// 处理每个条件
for (int i = 0; i < n; i++) {
char doctor1, doctor2;
char op; // 比较运算符
int days;
scanf(" %c%c%c%d", &doctor1, &op, &doctor2, &days);
if (op == '>') {
// doctor1 晚于 doctor2
if (schedule[days % 7] != 0) {
// 当前天已有值班人员,检查是否与 doctor1 相同
if (schedule[days % 7] != doctor1) {
printf("impossible\n");
return 0;
}
} else {
schedule[days % 7] = doctor1;
}
} else if (op == '<') {
// doctor1 早于 doctor2
if (schedule[days % 7] != 0) {
// 当前天已有值班人员,检查是否与 doctor2 相同
if (schedule[days % 7] != doctor2) {
printf("impossible\n");
return 0;
}
} else {
schedule[days % 7] = doctor2;
}
} else if (op == '=') {
// doctor1 在指定天值班
if (schedule[days % 7] != 0) {
// 当前天已有值班人员,检查是否与 doctor1 相同
if (schedule[days % 7] != doctor1) {
printf("impossible\n");
return 0;
}
} else {
schedule[days % 7] = doctor1;
}
}
}
// 输出值班表
printf("Monday: %c\n", schedule[0]);
printf("Tuesday: %c\n", schedule[1]);
printf("Wednesday: %c\n", schedule[2]);
printf("Thursday: %c\n", schedule[3]);
printf("Friday: %c\n", schedule[4]);
printf("Saturday: %c\n", schedule[5]);
printf("Sunday: %c\n", schedule[6]);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/ojUs 著作权归作者所有。请勿转载和采集!