版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/89459439
Problem Description:
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.
Input Specification:
Each input file contains one test case. Each case is given in the following format:
代码语言:javascript复制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.
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.
Sample Input 1:
代码语言:javascript复制4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100
Sample Output 1:
代码语言:javascript复制Mike CS991301
Mary EE990830
Joe Math990112
Sample Input 2:
代码语言:javascript复制2
Jean AA980920 60
Ann CS01 80
90 95
Sample Output 2:
代码语言:javascript复制NONE
解题思路:
建立一个学生结构体stu,里面有学生的姓名name、学号ID、分数grade,给定一个分数区间[grade1,grade2],输入完毕后,根据学生的分数来降序排列,然后无脑for-each 要是学生的成绩在给定的分数区间内就输出该学生的姓名、学号,要是没有一个学生的分数在给定区间内就输出"NONE"。
AC代码:
代码语言:javascript复制#include <bits/stdc .h>
using namespace std;
struct stu
{
string name,ID; //学生的姓名和学号
int grade; //学生的分数
};
bool cmp(stu a,stu b) //按照分数降序排列
{
return a.grade > b.grade;
}
int main()
{
int N;
cin >> N;
vector<stu> v;
for(int i = 0; i < N; i )
{
string name,ID;
int grade;
cin >> name >> ID >> grade;
v.push_back({name,ID,grade});
}
int grade1,grade12; //输入一个分数区间[grade1,grade2]
cin >> grade1 >> grade12;
sort(v.begin(),v.end(),cmp); //将学生按照分数降序排列
bool flag = false; //判断有没有学生的分数在[grade1,grade2]这个区间内
for(auto it : v)
{
if(it.grade >= grade1 && it.grade <= grade12) //若学生的分数在区间内就输出他的姓名、学号
{
flag = true;
cout << it.name << " " << it.ID << endl;
}
}
if(!flag) //若没有学生的分数在给定的区间内就输出"NONE"
{
cout << "NONE" << endl;
}
return 0;
}