PAT A1036 Boys vs Girls (25 分)

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

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains 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 no space, gender is either F (female) or M (male), and grade is an integer between 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 and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF − gradeM. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the 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

题意:

给定学生的姓名、性别、ID和成绩,求出成绩最高的女学生的姓名、ID,成绩最低的男学生的姓名、ID和他们成绩的差值.

输入样式:

学生数N
姓名 性别 ID 成绩
. . .

输出样式:

成绩最高的女学生的姓名 成绩最高的女学生的ID
成绩最低的男学生的姓名 成绩最低的男学生的ID
女学生的最高成绩-男学生的最低成绩

思路:

(1)设定一个结构体students存放学生的姓名、性别、ID和成绩,开一个结构体数组student存放所有学生的信息;
(2)设定max和maxi存放女学生的最高成绩和对应的数组下标,设定min和mini存放男学生的最低成绩和对应的数组下标,每次输入完一名学生的信息就判断是否需要更新max、maxi或min、mini的值;
(3)设定bool型变量output,output==true表示max与min都存在,输出差值,否则输出“NA”.

代码:

#include <cstdio>
struct students{
	char name[11],gender,ID[11];
	int grade;
}student[10010];
int main(){
	int N;
	scanf("%d",&N);
	int max=-1,maxi=-1,min=101,mini=-1;
	for(int i=0;i<N;i++){
		scanf("%s %c %s %d",student[i].name,&student[i].gender,student[i].ID,&student[i].grade);
		if(student[i].gender=='F'&&student[i].grade>max){
			max=student[i].grade;
			maxi=i;
		}
		if(student[i].gender=='M'&&student[i].grade<min){
			min=student[i].grade;
			mini=i;
		}
	}
	bool output=true;
	if(max!=-1){
		printf("%s %s\n",student[maxi].name,student[maxi].ID);
	}else{
		output=false;
		printf("Absent\n");
	}
	if(min!=101){
		printf("%s %s\n",student[mini].name,student[mini].ID);
	}else{
		output=false;
		printf("Absent\n");
	}
	if(output==true){
		printf("%d\n",student[maxi].grade-student[mini].grade);
	}else{
		printf("NA\n");
	}
	return 0;
}

词汇:

. . .

ps:

围棋?( *︾▽︾)

发布了26 篇原创文章 · 获赞 0 · 访问量 486

猜你喜欢

转载自blog.csdn.net/PanYiAn9/article/details/102503450