题目大意:给出N名学生的姓名,id,和成绩。按照成绩降序输出成绩在给定范围内的学生姓名和Id。
先存放,后根据范围筛选,再排序输出即可。
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
struct student
{
char name[20];
char id[20];
int grade;
bool operator < (const student& another)
{
return this->grade > another.grade;
}
};
int main()
{
int N;
scanf("%d", &N);
vector<student> allStudent(N), v;
int lowBound, upBound;
for (int i = 0; i < N; ++i)
scanf("%s%s%d", allStudent[i].name, allStudent[i].id, &allStudent[i].grade);
scanf("%d%d", &lowBound, &upBound);
for (int i = 0; i < N; ++i)
if(allStudent[i].grade >= lowBound && allStudent[i].grade <= upBound) v.push_back(allStudent[i]);
sort(v.begin(), v.end());
if(v.size() > 0)
for (int i = 0; i < v.size(); ++i)
printf("%s %s\n", v[i].name, v[i].id);
else printf("NONE");
return 0;
}