PAT甲级1036最后一个测试点答案错误的问题

题目:
在这里插入图片描述
下面的代码,使用结构体数组接收全部数据,并用变量存男性最低分的下标,女性最高分下标,最后输出,提交后会在最后一个测试点错误,其他测试点都对:

#include <stdio.h>

struct person{
    char name[11];
    char gender;
    char id[11];
    int grade;
};

int main(){
    int N, m_lowest=0, f_highest=0;
    int count_m=0, count_f=0;
    scanf("%d", &N);
    person p[N];
    for(int i=0; i<N; i++){
        scanf("%s %c %s %d", p[i].name, &p[i].gender, p[i].id, &p[i].grade);
        if(p[i].gender=='M'){
            count_m++;
            if(p[i].grade<p[m_lowest].grade) m_lowest = i;
        }
        else{
            count_f++;
            if(p[i].grade>p[f_highest].grade) f_highest = i;
        }
    }

    if(count_f==0) printf("Absent\n");
    else printf("%s %s\n", p[f_highest].name, p[f_highest].id);

    if(count_m==0) printf("Absent\n");
    else printf("%s %s\n", p[m_lowest].name, p[m_lowest].id);

    if(count_f>0 && count_m>0) printf("%d", p[f_highest].grade - p[m_lowest].grade);
    else printf("NA");

    return 0;
}

下面的代码,不设数组,用两个结构体分别记录,所有测试点都能过,很奇怪,原因暂时不清楚:

#include <stdio.h>

struct person{
    char name[11];
    char gender;
    char id[11];
    int grade;
};

int main(){
    int N;
    int count_m=0, count_f=0;
    scanf("%d", &N);
    person m_lowest = {"m", 'M', "id", 101};
    person f_highest = {"f", 'F', "id", -1};
    person temp;
    for(int i=0; i<N; i++){
        scanf("%s %c %s %d", temp.name, &temp.gender, temp.id, &temp.grade);
        if(temp.gender=='M'){
            count_m++;
            if(temp.grade<m_lowest.grade) m_lowest = temp;
        }
        else{
            count_f++;
            if(temp.grade>f_highest.grade) f_highest = temp;
        }
    }

    if(count_f==0) printf("Absent\n");
    else printf("%s %s\n", f_highest.name, f_highest.id);

    if(count_m==0) printf("Absent\n");
    else printf("%s %s\n", m_lowest.name, m_lowest.id);

    if(count_f>0 && count_m>0) printf("%d", f_highest.grade - m_lowest.grade);
    else printf("NA");

    return 0;
}

发布了2 篇原创文章 · 获赞 0 · 访问量 19
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览