PAT甲级(Advanced|PAT甲级(Advanced Level)练习题——1003

题目描述
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.
输入描述:
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.
【PAT甲级(Advanced|PAT甲级(Advanced Level)练习题——1003】输出描述:
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.
输入例子:
4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100
输出例子:
Mike CS991301
Mary EE990830
Joe Math990112
代码:

#include #includeusing namespace std; struct student { string name; string ID; int grade; }; void swap(student *a, student *b)// 交换 { student *c = NULL; c=a; a=b; b=c; }int main() { // freopen("input.txt", "r", stdin); int number; // 输入数字以字符串形式记录 cin >> number; student *Stu = new student[number]; // 动态分配空间 for (int i=0; i> Stu[i].name >> Stu[i].ID >> Stu[i].grade; }int grade1, grade2; cin >> grade1 >> grade2; for (int i=0; i= grade1 && Stu[i].grade <= grade2) { cout << Stu[i].name << " " << Stu[i].ID << endl; counter ++; } if (i == number-1 && 0 == counter) { cout << "NONE" << endl; } }delete[] Stu; return 0; }

这一题比较简单,有很多种实现方法。
Tips:
  1. 动态分配空间或者vector.push_back都可以
  2. 自己写排序+swap,当然也可以调用algorithm库中的cmp+sort
  3. 先排序再筛选更耗时,反之更占内存。

    推荐阅读