PAT A1036 Boys vs Girls(25)
AC代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int max_n = 11000;
struct Stu {
char name[20];
char gender;
char id[20];
char grade;
} boys_l, girls_h, temp;
void init() { //初始化,男生女生的成绩
//不能设置为100和0, 考虑边界值
boys_l.grade = 101;
girls_h.grade = -1;
}
int main() {
init();
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE}
int n, girls_num = 0, boys_num = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s %c %s %d", temp.name, &temp.gender, temp.id, &temp.grade);
//printf("%s %c %s %d\n", temp.name, temp.gender, temp.id, temp.grade);
if(temp.gender == 'F') { //找出女生中成绩最高的且计算人数
girls_num++;
if(temp.grade > girls_h.grade) girls_h = temp;
}
if(temp.gender == 'M') {
boys_num++;
if(temp.grade < boys_l.grade) boys_l = temp;
}
}
/* printf("%s %s %d\n", girls_h.name, girls_h.id, girls_h.grade);
printf("%s %s %d\n", boys_l.name, boys_l.id, boys_l.grade);
printf("%d", girls_h.grade - boys_l.grade);
*/
if(boys_num&&girls_num) {
printf("%s %s\n", girls_h.name, girls_h.id);
printf("%s %s\n", boys_l.name, boys_l.id);
printf("%d", girls_h.grade - boys_l.grade);
} else if(boys_num == 0 && girls_num != 0) {
printf("%s %s\n", girls_h.name, girls_h.id);
printf("Absent\n");
printf("NA");
} else if(girls_num == 0 && boys_num != 0) {
printf("Absent\n");
printf("%s %s\n", boys_l.name, boys_l.id);
printf("NA");
} else {
printf("Absent\n");
printf("Absent\n");
printf("NA");
}
return 0;
}
吾生也有涯,而知也无涯。