PAT (Advanced Level) Practice 1083 List Grades (25 分) 凌宸1642
题目描述:
Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.
译:给定一个还有 N 个学生的姓名,ID,和成绩的记录列表。你应该将其按照成绩的非递增序列排序,并且输出成绩在给定成绩范围内的学生的记录。
Input Specification (输入说明):
Each input file contains one test case. Each case is given in the following format:
N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2
where name[i]
and ID[i]
are strings of no more than 10 characters with no space, grade[i]
is an integer in [0, 100], grade1
and grade2
are the boundaries of the grade's interval. It is guaranteed that all the grades are distinct.
译:每个输入文件包含一个测试。每个用例按如下形式给出:
N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2
name[i]
和ID[i]
是不包含空格的不超过10个字符的字符串 , grade[i]
是一个介于 [0, 100] 之间的整数, grade1
和grade2
是给定的成绩的边界范围。题目保证所有的成绩都是不同的。
output Specification (输出说明):
For each test case you should output the student records of which the grades are in the given interval [grade1
, grade2
] and are in non-increasing order. Each student record occupies a line with the student's name and ID, separated by one space. If there is no student's grade in that interval, output NONE
instead.
译:对于每个测试用例,你应该按照成绩的非递增序列输出成绩在给定成绩范围内的学生信息。每个学生的学生姓名和 ID 占一行,并用一个空格隔开。如果没有学生的成绩在给定范围内,则输出NONE
。
Sample Input1 (样例输入1):
4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100
Sample Output1 (样例输出1):
Mike CS991301
Mary EE990830
Joe Math990112
Sample Input2 (样例输入2):
2
Jean AA980920 60
Ann CS01 80
90 95
Sample Output2 (样例输出2):
NONE
The Idea:
首先对 N 个学生的信息按照成绩非递增顺序进行排序,然后遍历学生信息数组,满足要求的成绩输出即可。(很久没做到过只考单一关键字sort排序的题目了)
The Codes:
#include<bits/stdc++.h>
using namespace std ;
struct Stu{
string name , id ;
int grade ;
}stu[10010] ;
int n , low , high , ans = 0 ;
bool cmp(Stu a , Stu b){
return a.grade > b.grade ;
}
int main(){
cin >> n ;
for(int i = 0 ; i < n ; i ++)
cin >> stu[i].name >> stu[i].id >> stu[i].grade ;
cin >> low >> high ;
sort(stu , stu + n , cmp) ; // 将学生信息按照成绩非递增顺序进行排序
for(int i = 0 ; i < n ; i ++){
if(stu[i].grade < low ) break ; // 当发现一个不满足的时候,立即退出循环
else if( stu[i].grade <= high){
ans ++ ; // 成绩在给定范围内的学生人数
cout << stu[i].name << ' ' << stu[i].id << endl ;
}
}
if(ans == 0) cout << "NONE" << endl ; // 如果没有学生的成绩在给定范围内,输出 NONE
return 0 ;
}