PAT A 1036 Boys vs Girls

This time you are asked to tell the difference between the lowest gradeof all the male students and the highest grade of all the femalestudents.

Input Specification:

Each input file contains one test case. Each case contains a positiveinteger N, followed by N lines of student information. Each linecontains a student's name, gender, ID and grade, separated by a space,where name and ID are strings of no more than 10 characters with nospace, gender is either F (female) or M (male), and grade is an integerbetween 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name andID of the female student with the highest grade, and the second linegives that of the male student with the lowest grade. The third linegives the difference grade~F~-grade~M~. If one such kind of student ismissing, output "Absent" in the corresponding line, and output "NA" inthe third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA
#include<cstdio>
struct student {
	char name[15];
	char gender;
	char id[15];
	int grade;
}temp, ans1, ans2;        //没有必要去开数组,开个中间变量temp就行了

int main() {
	int n;
	scanf("%d", &n);
	ans1.grade = -1, ans2.grade = 101;
	for (int i = 0; i < n; i++) {
		scanf("%s %c %s %d", temp.name, &temp.gender, temp.id, &temp.grade);
		if (temp.gender == 'F'&&temp.grade > ans1.grade)ans1 = temp;
		if (temp.gender == 'M'&&temp.grade < ans2.grade)ans2 = temp;
	}
	if (ans1.grade == -1)printf("Absent\n");
	else printf("%s %s\n", ans1.name, ans1.id);
	if (ans2.grade == 101) printf("Absent\n");
	else printf("%s %s\n", ans2.name, ans2.id);
	if (ans1.grade != -1 && ans2.grade != 101) printf("%d\n", ans1.grade - ans2.grade);
	else printf("NA\n");
	return 0;
}//纪念一下第一次写了和算法笔记上面几乎一样的代码

猜你喜欢

转载自blog.csdn.net/joah_ge/article/details/80548861