题目链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016
题目大意:
通过给出的男生和女生的分数,输出分数最高女生和分数最低男生的姓名和ID,以及两者分数之差。若某一性别的学生不存在,则输出Absent,相应分数之差用NA代替。
思路分析:
设置一个长度为2的数组score[2]存储男生和女生的最低分和最高分。初始值设为,101,-1。在输入的时候找出到此次为止女生分数最高的信息,以及男生分数最低的信息。
参考代码:
#include <cstdio>
struct Pnode{
char name[11];
char gd;
char id[11];
int grade;
}stu[2], temp;
int main(){
int n;
scanf("%d",&n);
int score[2];
score[0] = -1; //求女孩最高分,设初始值为-1;
score[1] = 101; //男孩最低分初始值设为101.
for(int i = 0; i < n; i++){
scanf("%s %c %s %d",temp.name, &temp.gd, temp.id, &temp.grade);
if( temp.gd == 'F'){
if( score[0] < temp.grade){
stu[0] = temp;
score[0] = temp.grade;
}
}else{
if( score[1] > temp.grade){
stu[1] = temp;
score[1] = temp.grade;
}
}
}
if(score[0] != -1) printf("%s %s\n",stu[0].name, stu[0].id);
else printf("Absent\n");
if(score[1] != 101) printf("%s %s\n",stu[1].name, stu[1].id);
else printf("Absent\n");
if(score[0] != -1 && score[1] != 101) printf("%d",stu[0].grade - stu[1].grade);
else printf("NA");
return 0;
}