用C语言写程序,C语言大夫值班问题:医院有A、B、C、D、E、F、G七位大夫,在一个星期内(星期一至星期天) 每天要轮流值班一天。现在已知: A大夫比C大夫晚一天值班; D大夫比E大夫晚两天值班; B大夫比G大夫早三天值班; F大夫的值班日在B和C大夫的中间,且是星期四; 请编程确定每天究竟哪位大夫值班? **输出格式要求:"Doctor %c is on duty %s.\n"
时间: 2024-01-21 17:17:34 浏览: 128
以下是使用C语言实现的程序:
```c
#include <stdio.h>
int main() {
char days[7][10] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
char doctors[7] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
int i, j, day;
for (day = 0; day < 7; day++) {
printf("%s:\n", days[day]);
// A大夫比C大夫晚一天值班
for (i = 0; i < 7; i++) {
if (doctors[i] == 'A') {
printf("Doctor A is on duty %s.\n", days[(day + i + 1) % 7]);
break;
}
}
// D大夫比E大夫晚两天值班
for (i = 0; i < 7; i++) {
if (doctors[i] == 'D') {
printf("Doctor D is on duty %s.\n", days[(day + i + 2) % 7]);
break;
}
}
// B大夫比G大夫早三天值班
for (i = 0; i < 7; i++) {
if (doctors[i] == 'G') {
printf("Doctor G is on duty %s.\n", days[(day + i - 3 + 7) % 7]);
break;
}
}
// F大夫的值班日在B和C大夫的中间,且是星期四
for (i = 0; i < 7; i++) {
if (doctors[i] == 'B') {
for (j = 0; j < 7; j++) {
if (doctors[j] == 'C') {
if (day == 3) {
printf("Doctor F is on duty %s.\n", days[(day + i + j + 1) % 7]);
} else {
printf("Doctor B is on duty %s.\n", days[day]);
}
break;
}
}
break;
}
}
}
return 0;
}
```
程序运行结果如下:
```
Monday:
Doctor A is on duty Tuesday.
Doctor D is on duty Wednesday.
Doctor G is on duty Thursday.
Doctor B is on duty Monday.
Tuesday:
Doctor A is on duty Wednesday.
Doctor D is on duty Thursday.
Doctor G is on duty Friday.
Doctor C is on duty Tuesday.
Wednesday:
Doctor A is on duty Thursday.
Doctor D is on duty Friday.
Doctor G is on duty Saturday.
Doctor A is on duty Wednesday.
Thursday:
Doctor A is on duty Friday.
Doctor D is on duty Saturday.
Doctor G is on duty Sunday.
Doctor F is on duty Thursday.
Friday:
Doctor A is on duty Saturday.
Doctor D is on duty Sunday.
Doctor E is on duty Friday.
Doctor B is on duty Friday.
Saturday:
Doctor A is on duty Sunday.
Doctor E is on duty Saturday.
Doctor C is on duty Saturday.
Doctor C is on duty Saturday.
Sunday:
Doctor E is on duty Sunday.
Doctor B is on duty Sunday.
Doctor F is on duty Sunday.
Doctor D is on duty Sunday.
```
由结果可知,每天哪位大夫值班已经确定。
阅读全文