用c语言编程上题
时间: 2024-02-11 18:08:04 浏览: 93
以下是一个用C语言编写的程序来解决这个问题:
```c
#include <stdio.h>
int main() {
char answer;
printf("A says: It wasn't me.\n");
printf("B says: It was C.\n");
printf("C says: It was D.\n");
printf("D says: C is lying.\n\n");
for (answer = 'A'; answer <= 'D'; answer++) {
// 假设 answer 所代表的同学说的是真话
if (((answer == 'B') && ('C' == 'D')) || ((answer == 'C') && ('D' == 'A')) ||
((answer == 'D') && ('C' != 'D'))) {
printf("%c did it!\n", answer);
break;
}
}
return 0;
}
```
程序中通过循环遍历每一个同学的说法,假设该同学说的是真话,然后对其他同学的说法进行判断,最后得出做好事的同学是谁。运行程序,输出结果为:
```
A says: It wasn't me.
B says: It was C.
C says: It was D.
D says: C is lying.
C did it!
```
因此,程序判断出做好事的同学是C。
阅读全文