【PAT甲级 - C++题解】1083 List Grades

简介: 【PAT甲级 - C++题解】1083 List Grades

1083 List Grades


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:

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:

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 Output 1:

Mike CS991301
Mary EE990830
Joe Math990112

Sample Output 2:

NONE

题意

给定一个成绩范围 [g1,g2] ,要求找出在该成绩范围内的学生,并输出相应信息。


思路

思路如下:


定义学生结构体,存储学生信息,并重载比较运算符,排序时按照成绩降序排序。

输入学生的信息以及规定的成绩范围。

从前往后数组,将在成绩范围内的学生放到数组前面。

如果没有学生在成绩范围内,则输出 NONE ;反之,对在成绩范围内的学生按照成绩降序排序并输出其 name 和 id 。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
//定义学生结构体
struct Person
{
    string name, id;
    int g;
    bool operator<(const Person& t)const
    {
        return g > t.g;
    }
}p[N];
int main()
{
    //输入学生信息
    cin >> n;
    for (int i = 0; i < n; i++)    cin >> p[i].name >> p[i].id >> p[i].g;
    //给定成绩范围
    int g1, g2;
    cin >> g1 >> g2;
    //将在给定成绩范围内的学生找出来
    int m = 0;
    for (int i = 0; i < n; i++)
        if (p[i].g >= g1 && p[i].g <= g2)
            p[m++] = p[i];
    //打印结果
    if (!m)  puts("NONE");
    else
    {
        //按照成绩降序排序
        sort(p, p + m);
        for (int i = 0; i < m; i++)
            cout << p[i].name << " " << p[i].id << endl;
    }
    return 0;
}
目录
相关文章
|
2月前
|
存储 缓存 C语言
【C++】list介绍以及模拟实现(超级详细)
【C++】list介绍以及模拟实现(超级详细)
64 5
|
2月前
|
存储 缓存 算法
【C++】list的模拟实现
【C++】list的模拟实现
|
2月前
|
存储 C++ 容器
【C++】list的认识与使用
【C++】list的认识与使用
|
3月前
|
存储 Java C++
【c++】list详细讲解
【c++】list详细讲解
41 5
|
3月前
|
Java C++ Python
【c++】list 模拟
【c++】list 模拟
22 1
|
3月前
|
存储 编译器 C语言
【C++】list模拟实现
本文档介绍了C++ STL中`list`容器的模拟实现,包括`ListNode`节点类、迭代器类和`list`类的详细设计。`ListNode`模板类存储数据并维护前后指针;`ListIterator`是一个复杂的模板类,提供解引用、自增/自减以及比较操作。`list`类包含了链表的各种操作,如插入、删除、访问元素等,并使用迭代器作为访问接口。实现中,迭代器不再是简单的指针,而是拥有完整功能的对象。此外,文档还提到了迭代器的实现对C++语法的特殊处理,使得`it-&gt;_val`的写法成为可能。文章通过分步骤展示`list`的各个组件的实现,帮助读者深入理解STL容器的内部工作原理。
|
3月前
|
算法 搜索推荐 C++
【C++】list的使用(下)
`C++` 中 `std::list` 的 `merge()`、`sort()` 和 `reverse()` 操作: - `merge(x)` 和 `merge(x, comp)`: 合并两个已排序的`list`,将`x`的元素按顺序插入当前`list`,`x`清空。比较可自定义。 - `sort()` 和 `sort(comp)`: 对`list`元素排序,保持等价元素相对顺序。内置排序基于稳定排序算法,速度较慢。 -reverse(): 反转`list`中元素的顺序。 这些操作不涉及元素构造/销毁,直接移动元素。注意,`sort()`不适合`std::list`,因链表结构不利于快速排序
|
3月前
|
C++ 容器
【C++】list的使用(下)
这篇博客探讨了C++ STL中`list`容器的几个关键操作,包括`splice()`、`remove()`、`remove_if()`和`unique()`。`splice()`允许高效地合并或移动`list`中的元素,无需构造或销毁。`remove()`根据值删除元素,而`remove_if()`则基于谓词移除元素。`unique()`则去除连续重复的元素,可选地使用自定义比较函数。每个操作都附带了代码示例以说明其用法。
|
3月前
|
编译器 C++ 容器
【C++】list的使用(上)
迭代器在STL中统一了访问接口,如`list`的`begin()`和`end()`。示例展示了如何使用正向和反向迭代器遍历`list`。注意`list`的迭代器不支持加减操作,只能用`++`和`--`。容器的`empty()`和`size()`用于检查状态和获取元素数。`front()`和`back()`访问首尾元素,`assign()`重载函数用于替换内容,`push_*/pop_*`管理两端元素,`insert()`插入元素,`erase()`删除元素,`resize()`调整大小,`clear()`清空容器。这些接口与`vector`和`string`类似,方便使用。
|
3月前
|
存储 编译器 C语言
【C++】list的使用(上)
**C++ STL的list是一个基于双向循环链表的容器,支持常数时间内插入和删除,但不支持随机访问。默认构造函数、填充构造、迭代器范围构造和拷贝构造提供多种初始化方式。析构函数自动释放内存,赋值运算符重载用于内容替换。示例代码展示了构造和赋值操作。**