You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调试这段C程序?提交至URI平台显示答案错误

代码错误分析与修正

你的代码提交URI平台出错,核心是两个致命逻辑错误:

错误1:无法区分Pedra(石头)和Papel(布)

Pedra(拼写pedra)和Papel(拼写papel)都以p开头,你用s[0] == 'p'来判定是Papel,会把Pedra也纳入Papel的胜负逻辑中,导致判断完全错误。比如:

  • 当Sheldon出Pedra、Raj出Papel时,实际是Papel击败Pedra,应该输出Raj trapaceou!,但你的代码会错误判定Sheldon赢,输出Bazinga!。

错误2:混淆了Pedra和Spock的特征

你用s[4] == 'a'来代表Spock,但这个特征属于Pedra(pedra长度为5,索引4的字符是a);而Spock的拼写是spock,长度为5,索引4的字符是k,所有涉及Spock的判断都用错了特征值,导致这部分逻辑全部颠倒。


修正方案

要准确判断每个选项,需用每个单词的唯一特征:

  • Pedra:s[4] == 'a'(最后一个字符为a)
  • Papel:s[4] == 'l'(最后一个字符为l,papel的最后一位是l)
  • Tesoura:s[0] == 't'(唯一以t开头的选项)
  • Lagarto:s[0] == 'l'(唯一以l开头的选项)
  • Spock:s[4] == 'k'(最后一个字符为k)或s[0] == 's'(唯一以s开头的选项)

根据题目胜负规则重新梳理逻辑,示例代码片段:

#include <stdio.h>

int main()
{
    int c, i, x;
    char s[10], r[10];
    scanf("%d", &x);

    for(i = 0, c = 0; i < x; i++){
        scanf("%s%s", s, r);
        c++;

        // Sheldon出Pedra
        if(s[4] == 'a'){
            if(r[0] == 't' || r[0] == 'l'){
                printf("Caso #%d: Bazinga!\n", c);
            } else if(r[4] == 'l' || r[4] == 'k'){
                printf("Caso #%d: Raj trapaceou!\n", c);
            } else {
                printf("Caso #%d: De novo!\n", c);
            }
        }
        // Sheldon出Papel
        else if(s[4] == 'l'){
            if(r[4] == 'a' || r[4] == 'k'){
                printf("Caso #%d: Bazinga!\n", c);
            } else if(r[0] == 't' || r[0] == 'l'){
                printf("Caso #%d: Raj trapaceou!\n", c);
            } else {
                printf("Caso #%d: De novo!\n", c);
            }
        }
        // Sheldon出Tesoura
        else if(s[0] == 't'){
            if(r[4] == 'l' || r[4] == 'k'){
                printf("Caso #%d: Bazinga!\n", c);
            } else if(r[4] == 'a' || r[0] == 's'){
                printf("Caso #%d: Raj trapaceou!\n", c);
            } else {
                printf("Caso #%d: De novo!\n", c);
            }
        }
        // Sheldon出Lagarto
        else if(s[0] == 'l'){
            if(r[4] == 'l' || r[4] == 'k'){
                printf("Caso #%d: Bazinga!\n", c);
            } else if(r[4] == 'a' || r[0] == 't'){
                printf("Caso #%d: Raj trapaceou!\n", c);
            } else {
                printf("Caso #%d: De novo!\n", c);
            }
        }
        // Sheldon出Spock
        else if(s[4] == 'k'){
            if(r[4] == 'a' || r[0] == 't'){
                printf("Caso #%d: Bazinga!\n", c);
            } else if(r[4] == 'l' || r[0] == 'l'){
                printf("Caso #%d: Raj trapaceou!\n", c);
            } else {
                printf("Caso #%d: De novo!\n", c);
            }
        }
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者WK Siyam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:09:26