救命,好后悔上学期没好好学c语言搞得现在小学期痛苦的想死。
是一道挺简单的题了,已经挣扎了两天了。
我的测试用例已经过去了,但是还有一个隐藏的就是过不去。
描述
Tom 和 Jerry 做猜数字的游戏,Tom 想一个数字然后让 Jerry 去猜,数字的范围在 1 到 10 之间。对于 Jerry 每讲的一个数,Tom 都要讲这个数是 too high 或者 too low 或者 right on,直到 right on 结束。为了防止 Tom 作弊,Jerry 把每一次的对话记录下来,现在让你去判断 Tom 有没有作弊。
输入
游戏可能做很多次,直到 Jerry 猜 0 的时候游戏结束,每一次猜测由一个正整数和一行回答组成。
输出
对每一次游戏如果 Tom 的回答有自相矛盾的地方,就输出 Tom is dishonest,否则输出 Tom may be honest。
测试输入
5↵
too low↵
7↵
too high↵
6↵
right on↵
10↵
too high↵
3↵
too low↵
4↵
too high↵
2↵
right on↵
0↵
期望输出
Tom may be honest↵
Tom is dishonest↵
然后是我那个拉胯的代码
```c
#include <stdio.h>
#include <string.h>
typedef struct {
int guess;
char response[15];
} Record;
int main() {
int guess;
char response[15];
int correctAnswer = 0;
int foundCorrectAnswer = 0;
int i = 0;
// 定义记录数组和计数变量
Record history[1000]; // 假设最多记录1000个数据
int count = 0;
// 定义诚实程度标志变量
int isHonest = 1;
while (scanf("%d", &guess) == 1 && guess != 0) {
getchar();
fgets(response, sizeof(response), stdin);
response[strcspn(response, "\n")] = '\0';
// 判断是否为 "right on",并设置正确答案
if (strcmp(response, "right on") == 0) {
if (foundCorrectAnswer==i) {
correctAnswer = guess;
foundCorrectAnswer++;
i++;
break;
}
}
// 存储记录
Record record;
record.guess = guess;
strcpy(record.response, response);
history[count] = record;
count++;
// 判断之前记录中的描述是否与正确答案相符
for (int i = 0; i < count - 1; i++) {
if (history[i].guess <= correctAnswer && strcmp(history[i].response, "too high") == 0 ) {
isHonest = 0;
break;
}
if (history[i].guess >= correctAnswer && strcmp(history[i].response, "too low") == 0) {
isHonest = 0;
break;
}
}
// 根据判断结果输出诚实程度
if (!isHonest) {
printf("Tom is dishonest\n");
}
else {
printf("Tom may be honest\n");
}
}
return 0;
}
思路是先把他们存起来按照right on分组进行判定,不知道哪里还不完善。(躺)
