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

Meaning of the questions:

Given the student's name, sex, ID, and results, obtained the highest score of the female student's name, the difference ID, the lowest score male student's name, ID, and their results.

Input style:

N number of students
Name Sex ID achievement
...

Output Style:

The highest score of female students in the name of the highest scores of female students ID
lowest achievement scores lowest male student's name ID of male students
female students of the highest score - the lowest score of male students

Ideas:

(1) students to set a structure to store the student's name, sex, ID and achievements, open an array of structures to store student information for all students;
(2) set the max and array highest score and the corresponding maxi stored female students subscript, set min and mini storage lowest score of male students and a corresponding array index, every time you have entered a student's information to determine whether it needs to be updated max, maxi or min, the value of the mini;
(3) set bool variable output, output == true max and min represent are present, it outputs the difference, and otherwise outputs "NA".

Code:

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

vocabulary:

. . .

ps:

Go? (* ︾ ▽ ︾)

Published 26 original articles · won praise 0 · Views 486

Guess you like

Origin blog.csdn.net/PanYiAn9/article/details/102503450