Starrity 2023-08-24 16:56 采纳率: 100%
浏览 11
已结题

每日委托:一位大学生的忏悔

救命,好后悔上学期没好好学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分组进行判定,不知道哪里还不完善。(躺)

  • 写回答

3条回答 默认 最新

  • 梦幻精灵_cq 2023-08-24 19:43
    关注
    • 算法:根据回答重置目标数(Tom心中所想的数)可能区间的上下限,当出现非法区间(上限数小于下限时),即证明Tom is dishonest。

    • 我仅会点Python,就用python代码走一走“算法逻辑”

      img

    python代码

    #!/sur/bin/nve python
    # coding: utf-8
    
    def isHonest(text):
        ''' 猜数字游戏诚实判定 '''
        high, low = 10, 1
        text = text.split('\n')
        k = 0
        
        while True:
            
            if text[k] == '0':
                break # 收到游戏结束信号,退出while循环。
            
            num, tip = text[k:k+2] # 截取每次猜测的数字和回答。
            
            if tip == 'too high':
                high = int(num) - 1
            elif tip == 'too low':
                low = int(num) + 1
            elif tip == 'right on':
                
                if low == high:
                    print(f"\n{' 恭喜猜中!':~^36}")
                high, low = 10, 1
            myRange = list(range(low, high+1))
            
            if not myRange:
                return 'Tom is dishonest.'
            
            print(f"\n目标数据区间:{myRange}")
            k += 2
        
        return 'Tom may be honest.'
    
    
    if __name__ == '__main__':
        # 测试用例
        text = '''5
    too low
    7
    too high
    6
    right on
    10
    too high
    3
    too low
    4
    too high
    2
    right on
    0'''
        print(f"\n游戏过程:\n{text}\n\n诚实判定:{isHonest(text)}")
    
    
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月24日