亦行之 2024-03-11 10:41 采纳率: 34.5%
浏览 1

PAT2015可以得到正确答案,但提交后却是错误的


#include<stdio.h>
#include<stdlib.h>
struct student{
    int num;
    int de;
    int cai;
    int sum;
};
int cmp(const void *a,const void *b){
    struct student *s,*t;
    s=(struct student *)a;
    t=(struct student *)b;
    int fs,ft;
    fs=s->de +s->cai ;
    ft=t->de +t->cai ;
    if(fs!=ft)
    return ft-fs;
    else if(fs==ft)//总相同 德降序 
    return (t->de )-(s->de );
    else//德相同 号升序 
    return (s->num )-(t->num );
}
void print(struct student a[],int n){
    for(int i=0;i<n;i++){
        printf("%d %d %d\n",a[i].num,a[i].de,a[i].cai);
    } 
}
int main(){
    int n,l,h;
    int i1=0,i2=0,i3=0,i4=0;
//    int print(int a[],int n);
    scanf("%d %d %d",&n,&l,&h);
    struct student st;
    struct student st1[n],st2[n],st3[n],st4[n];
    for(int j=0;j<n;j++){
        scanf("%d %d %d",&st.num ,&st.de ,&st.cai );
        if(st.de >=l&&st.cai >=l){
            if(st.de>=h&&st.cai >=h){
                st1[i1++]=st;
            /*if(st[i].de >=h&&st[i].cai >=h){
                st1[i1].de =st[i].de ;
                st1[i1].cai =st[i].cai ;
                st1[i1].sum =st[i].de +st[i].cai ;
                i1++;*/
            }
            else if(st.de >=h){
                    st2[i2++]=st;
                /*st2[i2].de =st[i].de ;
                st2[i2].cai =st[i].cai ;
                st2[i2].sum =st[i].de +st[i].cai ;
                i2++;*/
            }
            else if(st.de >=st.cai ){
                st3[i3++]=st;
                /*st3[i3].de =st[i].de ;
                st3[i3].cai =st[i].cai ;
                st3[i3].sum =st[i].de +st[i].cai ;
                i3++;*/
            }
            else{
                st4[i4++]=st;
            /*    st4[i4].de=st[i].de ;
                st4[i4].cai=st[i].cai ;
                st4[i4].sum=st[i].cai +st[i].de ;
                i4++;*/
        }
    }
}
    qsort(st1,i1,sizeof *st1,cmp);
    qsort(st2,i2,sizeof *st2,cmp);
    qsort(st3,i3,sizeof *st3,cmp);
    qsort(st4,i4,sizeof *st4,cmp);
    printf("%d\n",i1+i2+i3+i4);
    print(st1,i1);
    print(st2,i2);
    print(st3,i3);
    print(st4,i4);
    return 0;
}

img

img


不明白哪里有误,在devc++上输入后能得到正确输出 但提交到pta后却显示错误

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-11 10:42
    关注

    引自免费微信小程序:皆我百晓生

    在分析您的代码后,我注意到您按照司马光的德才论对考生进行分类和排序。根据题目描述及您提供的样例输入,代码逻辑基本正确,但问题可能出在提交PAT测试系统时遇到的边界条件或者数据格式处理上。

    首先,请确认以下几点:

    1. i1, i2, i3, 或 i4 为0时,在调用 print 函数之前没有输出任何信息。尽管这不会导致编译错误,但在PAT系统中可能会因为没有遵循输出格式而被判题程序判定为答案错误。

    2. cmp 函数中,当 fs==ft 时,按德分降序排列;若德分也相同,则按准考证号升序排列。这里逻辑看起来是正确的,但需要确保所有情况都已经覆盖,并且在实际比较中能够正确区分不同考生。

    3. 考虑到PAT评测系统的严格性,检查一下读取输入数据时是否有潜在的问题,例如:如果输入的数据行数少于预期的考生总数n,代码可能无法正常处理这种情况。

    4. 在打印考生信息时,应当包括每个考生的总分(即德分与才分之和),但当前 print 函数并未打印 sum 字段。

    5. 需要确保内存分配合理,数组大小不要超过PAT系统允许的最大值。由于N最大只有10,因此这里的数组大小设置为n应该没问题。

    针对上述可能出现的问题,您可以尝试调整代码如下:

    #include<stdio.h>
    #include<stdlib.h>
    
    struct student{
        int num;
        int de;
        int cai;
        int sum; // Add this field to store the total score
    };
    
    int cmp(const void *a, const void *b){
        struct student *s = (struct student *)a;
        struct student *t = (struct student *)b;
    
        if(s->sum != t->sum) return t->sum - s->sum;
        else if(s->de == t->de) return s->num - t->num;
        else return t->de - s->de;
    }
    
    void print(struct student a[], int n){
        for(int i=0; i<n; i++){
            printf("%d %d %d %d\n", a[i].num, a[i].de, a[i].cai, a[i].sum); // Include sum in output
        }
    }
    
    int main(){
        int n, l, h;
        scanf("%d %d %d", &n, &l, &h);
    
        struct student st[n];
        int count = 0;
    
        for(int j=0; j<n; j++){
            scanf("%d %d %d", &st[j].num, &st[j].de, &st[j].cai);
            st[j].sum = st[j].de + st[j].cai; // Calculate sum here
    
            if(st[j].de >= l && st[j].cai >= l) {
                ++count;
                // No need to sort into separate arrays, just sort the entire array once at the end
            }
        }
    
        qsort(st, count, sizeof *st, cmp);
    
        printf("%d\n", count);
        for(int k=0; k<count; k++) {
            print(&st[k], 1); // Print each eligible student one by one
        }
    
        return 0;
    }
    

    通过合并所有符合条件的学生到一个数组 st[] 中并一次性对其进行排序,可以简化代码并降低出错的可能性。同时,增加了 sum 字段并在 print 函数中输出。再次强调,请务必根据PAT平台的要求仔细检查输入输出格式是否完全匹配。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月11日

悬赏问题

  • ¥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仿真