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

题目:
PAT甲级1036最后一个测试点答案错误的问题_第1张图片
下面的代码,使用结构体数组接收全部数据,并用变量存男性最低分的下标,女性最高分下标,最后输出,提交后会在最后一个测试点错误,其他测试点都对:

#include 

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 

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;
}

你可能感兴趣的:(PAT甲级1036最后一个测试点答案错误的问题)