请用C语言编程判断是谁做的好事,A说:不是我。B说:是C。C说:是D。D说:C胡说,编程判断谁做了好事
时间: 2024-10-18 15:22:27 浏览: 23
在C语言中,这个问题可以通过创建一个简单的结构体或者枚举来表示人物,并使用循环和条件语句来模拟他们的对话。由于C语言本身并没有直接支持类似“聊天”的功能,我们可以编写一个简单的程序来代表这个逻辑。这里假设我们有四个字符`A`, `B`, `C`, 和 `D`,并创建一个变量`culprit` 来存储做好事的人。
```c
#include <stdio.h>
typedef enum {NO_CULPRIT, A, B, C, D} Person;
void hearStatement(Person speaker, Person culprit) {
switch (speaker) {
case A:
if (culprit == C || culprit == D) {
printf("A says: It's either C or D.\n");
} else {
printf("A says: Not me.\n");
}
break;
case B:
if (culprit == C) {
printf("B says: It is C.\n");
} else {
printf("B's statement doesn't add to the information.\n");
}
break;
case C:
printf("C says: It's D.\n");
break;
case D:
printf("D says: C is lying!\n");
// Since we know D is not saying it was themselves, it must be C.
culprit = C;
break;
default:
printf("Invalid input.\n");
}
}
int main() {
Person culprit = NO_CULPRIT; // Initially no one is known to have done the good deed
// Let's start with the statements and update the culprit accordingly
hearStatement(A, culprit);
hearStatement(B, culprit);
hearStatement(C, culprit);
hearStatement(D, culprit);
// Now, output the final result
if (culprit == A) {
printf("It turns out that A did the good deed.\n");
} else if (culprit == B) {
printf("B did the good deed.\n");
} else if (culprit == C) {
printf("C did the good deed.\n");
} else if (culprit == D) {
printf("D did the good deed.\n");
} else {
printf("No one did the good deed according to this sequence of events.\n");
}
return 0;
}
```
阅读全文