PAT Brush Questions Grade B 1004 Ranking

PAT Brush Questions Grade B 1004 (cpp)

Title description

       Read in the names, student numbers, and grades of n (>0) students, and output the names and student numbers of the students with the highest and lowest grades respectively.

Input format

Each test input contains 1 test case, the format is
       line 1: positive integer n
       line 2: the first student’s name and student number score
       line 3: the second student’s name, student number and score
        ………
       nth Line +1: the
       name and student ID of the nth student. The name and student ID are both strings of no more than 10 characters. The score is an integer between 0 and 100. It is guaranteed that there are no two in a set of test cases. The grades of all students are the same.

Output format

       Output 2 lines for each test case, the first line is the name and student number of the student with the highest score, and the second line is the name and student number of the student with the lowest score. There is a space between the strings.

Input sample

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

Sample output

Mike CS991301
Joe Math990112


problem analysis

       Although this question is a 20-point question, the difficulty is basically the same as 15-point. Several key conditions are defined in the input format requirements: 1. The size of the name and student number is a char array or string of no more than 10; 2. The grade is an integer between 0-100; 3 一组测试用例中不存在相同的成绩.
       So for this question, only a for loop is needed to traverse the input and find the maximum and minimum scores. You can bind your name, student ID, and grades into a structure, but this will take up a lot of memory, just set a single variable.

Code

The core code is as follows:

for (int i = 1; i < n; i++) {
    
      //for循环输入信息并找出最高分,最低分
		cin >> na >> num >> sco;
		if (sco > maxSCO) {
    
    
			maxNA = na;     //最高分学生姓名
			maxNum = num;   //最高分课程编号
			maxSCO = sco;   //最高分课程成绩
		}
		if (sco < minSCO) {
    
    
			minNA = na;     //最低分学生姓名
			minNum = num;   //最低分课程编号
			minSCO = sco;   //最低分课程成绩
		}
	}

The complete code is implemented as follows: the

       code is here

Run implementation

Run implementation

Guess you like

Origin blog.csdn.net/ThunderF/article/details/90599220