PAT level B test record-1004 ranking (20 points)

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 in the format

1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

The 姓名sum 学号is a string of no more than 10 characters, and the score is an integer between 0 and 100. Here, it is ensured that no two students in the set of test cases have the same score.

Output format:

Two lines are output for each test case, the first line is the name and student number of the student with the highest grade, the second line is the name and student number of the student with the lowest grade, and there is 1 space between the strings.

Sample input:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

Sample output:

Mike CS991301
Joe Math990112

Ideas

This question is very simple, define a structure to store the student's name, student number, grades, and then define a structure array S [maxn] and cmp function sorted by grade from largest to smallest I wrote maxn = 1010, it turned out to be AC). Then after reading the data, use the sort function to sort [0, n-1] (here is a closed interval, the parameter of the sort function is left closed and right open, so it is sort (S, S + n, cmp) ), In this case, S [0] is the one with the highest score, S [n-1] is the one with the lowest score, just output the two.

Code

#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1010;
struct student{
	string name;
	string number;
	int score;
}S[maxn];
bool cmp(student a, student b){
	return a.score>b.score;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		string tmpname, tmpnumber;
		int tmpscore;
		cin>>tmpname>>tmpnumber>>tmpscore;
		S[i].name = tmpname;
		S[i].number = tmpnumber;
		S[i].score = tmpscore;
	}
	sort(S, S+n, cmp);
	cout<<S[0].name<<" "<<S[0].number<<'\n';
	cout<<S[n-1].name<<" "<<S[n-1].number;
	return 0;
}
Published 54 original articles · won 27 · views 4968

Guess you like

Origin blog.csdn.net/weixin_42257812/article/details/105602202