题目描述
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个包括姓名、学号、成绩的学生记录组成的列表。你需要对记录根据成绩降序排序,并输出在给定区间内的学生记录。
INPUT FORMAT
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 FORMAT
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.
翻译:对于每组输入数据,你需要输出给定范围 [grade1, grade2] 内的学生记录,并且按照升序排列。每个学生记录占一行,包括学生姓名和ID,用空格隔开。如果该区间内没有学生记录,则输出“NONE”。
Sample Input 1:
4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100
Sample Output 1:
Mike CS991301
Mary EE990830
Joe Math990112
Sample Input 2:
2
Jean AA980920 60
Ann CS01 80
90 95
Sample Output 2:
NONE
解题思路
这道题考察了定义一个结构体,写一个简单排序函数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
struct Stu{
char id[15],name[15];
int grade;
Stu(char Name[],char ID[],int G):grade(G){
strcpy(id,ID);
strcpy(name,Name);
}
bool operator<(const Stu &a)const{
return grade>a.grade;
}
};
vector<Stu> stu;
int main(){
scanf("%d",&N);
char id[15],name[15];
int g;
for(int i=0;i<N;i++){
scanf("\n%s%s%d",name,id,&g);
stu.push_back(Stu(name,id,g));
}
int pre,pro;
scanf("%d%d",&pre,&pro);
sort(stu.begin(),stu.end());
int flag=0;
for(int i=0;i<stu.size();i++){
if(stu[i].grade>pro)continue;
if(stu[i].grade<pre)break;
printf("%s %s\n",stu[i].name,stu[i].id);
flag=1;
}
if(!flag)printf("NONE\n");
return 0;
}