成为最强的理塘丁真 2024-01-22 14:25 采纳率: 45.5%
浏览 3

C语言oj搞不清楚,我觉得我的根样例的挺像的

img

img


#include <stdio.h>  
#include <string.h>  
  
#define MAX_NAME_LENGTH 20  
#define MAX_TEST_COUNT 1000  
  
struct Title {  
    char name[MAX_NAME_LENGTH];  
    int count;  
};  
  
int main() {  
  int t;
  scanf("%d",&t);
  while(t--)
  { 
    int test_count;  
    scanf("%d", &test_count);  
  
    struct Title title[MAX_TEST_COUNT];  
    int i, j;  
    for (i = 0; i < test_count; i++) {  
        scanf("%s %d", title[i].name, &title[i].count);  
    }  
  

    for (i = 0; i < test_count - 1; i++) {  
        for (j = i + 1; j < test_count; j++) {  
            if (title[i].count < title[j].count) {  
                struct Title temp = title[i];  
                title[i] = title[j];  
                title[j] = temp;  
            }  
        }  
    }  
  

    for (i = 0; i < test_count; i++) {  
        printf("%s\n", title[i].name);  
    }  
  
    return 0;  
}
}

为什自己运行出来我觉得没问题,在oj里面却是答案错误呢

  • 写回答

3条回答 默认 最新

  • a5156520 2024-01-22 14:39
    关注

    将第43行放到while循环后面试试,因为测试组数t可能不止一组。

    修改如下:

    #include <stdio.h>
    #include <string.h>
    
    #define MAX_NAME_LENGTH 20
    #define MAX_TEST_COUNT 1000
    
    struct Title {
        char name[MAX_NAME_LENGTH];
        int count;
    };
    
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            int test_count;
            scanf("%d", &test_count);
    
            struct Title title[MAX_TEST_COUNT];
            int i, j;
            for (i = 0; i < test_count; i++) {
                scanf("%s %d", title[i].name, &title[i].count);
            }
    
    
            for (i = 0; i < test_count - 1; i++) {
                for (j = i + 1; j < test_count; j++) {
                    if (title[i].count < title[j].count) {
                        struct Title temp = title[i];
                        title[i] = title[j];
                        title[j] = temp;
                    }
                }
            }
    
    
            for (i = 0; i < test_count; i++) {
                printf("%s\n", title[i].name);
            }
    
        
        }
        
        return 0;  // 将这行代码放到while循环后面,因为数据组数可能不止一组 
    }
    
    
    

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 1月22日

悬赏问题

  • ¥20 wordpress如何限制ip访问频率
  • ¥15 自研小游戏,需要后台服务器存储用户数据关卡配置等数据
  • ¥15 请求解答odoo17外发加工某工序的实操方法
  • ¥20 IDEA ssm项目 跳转页面报错500
  • ¥20 系统直接进入应急模式了,请教一下,人要裂开了
  • ¥15 页面初次加载时,JS调用对象可以调用成功,但是一旦跳转到另一个页面,再跳转回来后这个对象就显示undefind了,调用也调用不了了,什么原因?
  • ¥15 来个厉害的程序开发的,安排点活给你做
  • ¥15 rexroth indramotion MTX micro系统轴耦合编程
  • ¥100 springboot2.7.x 整合 sharding 的问题
  • ¥15 如何通过命令行操作统信360安全浏览器?