亦行之 2024-03-07 21:20 采纳率: 34.5%
浏览 1
已结题

PAT1058c语言运行超时

img

#include<stdio.h>
#include<ctype.h>//isalpha的头文件
 #include<string.h>//strcmp的头文件
struct problem{
    int score;
    int cnt;
    int right_cnt;
    char right[6];//所有正确选项存在里面 
    int wrong;
};
int main()
{
    int n,m,max_wrong=0;
    scanf("%d %d",&n,&m);
    struct problem ti[m];
    int p;
    for(p=0;p<m;p++){
        scanf("%d %d %d ",&ti[p].score ,&ti[p].cnt ,&ti[p].right_cnt );
        ti[p].wrong =0;//初始化 
        char c;
        int j=0;
        while(c==getchar()!='\n'){//妙
            if(isalpha(c)){
                ti[p].right [j]=c;//存储正确选项有哪些
                j++; 
            }
        } 
        //
        ti[p].right [j]='\0';
    }
    int q;
    for(q=0;q<n;q++){
        int xxnum,grade=0,j;
         for(j=0;j<m;j++){
             scanf("(%d",&xxnum);
             char c,choose[6];//存储该人该题所选择的选项们
             int k=0;
              while(c=getchar()!=')'){
                 if(isalpha(c)){
                     choose[k]=c;
                     k++;
                 }
             } 
             //
             choose[k]='\0';
             if(xxnum==ti[j].right_cnt &&strcmp(choose,ti[j].right )==0){
                 //正确则加起来分数 
                 grade+=ti[j].score ;
             }
             else{
                 //错误记录该题的错误次数 
                 ti[j].wrong ++;
             }
             if(max_wrong<ti[j].wrong ){
                 max_wrong=ti[j].wrong ;
             }
             getchar();//读入空格! 
         }
         printf("%d\n",grade);
    }
    if(max_wrong==0){
        printf("Too Simple");
    }
    else{
        printf("%d",max_wrong);
        for(int i=0;i<m;i++){
            if(max_wrong==ti[i].wrong ){
                printf(" %d",i+1);//递增输出第几道题 
            }
        }
    }
    return 0;
 } 

img


为什么我的代码提交到pta后运行超时?哪一步有问题?

  • 写回答

4条回答 默认 最新

  • 关注

    亦行之 晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    你的代码在处理输入时,存在一些低效的部分,这可能是导致超时的原因。主要问题在于:

    1. while(c==getchar()!='\n') 这个循环条件写法有误,应修改为 while ((c = getchar()) != '\n')
    2. 在读取每个测试案例的选项时,使用了 getchar()!=')' 作为循环条件,同样这里有语法错误,并且会导致每次循环都会多读一个字符,应该修正为 while ((c = getchar()) != ')' && c != EOF),防止因提前遇到文件结束符EOF而死循环。

    修改后的代码如下:

    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    struct problem{
        int score;
        int cnt;
        int right_cnt;
        char right[6];
        int wrong;
    };
    
    int main() {
        int n, m, max_wrong = 0;
        scanf("%d %d", &n, &m);
        struct problem ti[m];
        int p;
        for (p = 0; p < m; p++) {
            scanf("%d %d %d ", &ti[p].score, &ti[p].cnt, &ti[p].right_cnt);
            ti[p].wrong = 0;
            char c;
            int j = 0;
            while ((c = getchar()) != '\n') { // 修复此处循环条件
                if (isalpha(c)) {
                    ti[p].right[j] = c;
                    j++;
                }
            }
            ti[p].right[j] = '\0';
        }
    
        int q;
        for (q = 0; q < n; q++) {
            int xxnum, grade = 0, j;
            for (j = 0; j < m; j++) {
                scanf("(%d",&xxnum);
                char c, choose[6];
                int k = 0;
                while ((c = getchar()) != ')' && c != EOF) { // 防止死循环并正确处理EOF
                    if (isalpha(c)) {
                        choose[k] = c;
                        k++;
                    }
                } 
                choose[k] = '\0';
                if (xxnum == ti[j].right_cnt && strcmp(choose, ti[j].right) == 0) {
                    grade += ti[j].score;
                }
                else {
                    ti[j].wrong++;
                }
                if (max_wrong < ti[j].wrong) {
                    max_wrong = ti[j].wrong;
                }
                getchar(); // 读入空格!
            }
            printf("%d\n", grade);
        }
    
        if (max_wrong == 0) {
            printf("Too Simple");
        }
        else {
            printf("%d", max_wrong);
            for (int i = 0; i < m; i++) {
                if (max_wrong == ti[i].wrong) {
                    printf(" %d", i + 1);
                }
            }
        }
        
        return 0;
    }
    

    尽管上述修改可能解决部分问题,但要完全避免超时,还需要进一步优化算法逻辑,例如减少不必要的字符读取操作和字符串比较等。

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

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 修改了问题 3月7日
  • 创建了问题 3月7日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真