前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】List Grades

【PAT甲级】List Grades

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:32:33
2200
发布2019-11-08 14:32:33
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input 1:
  • Sample Output 1:
  • Sample Input 2:
  • Sample Output 2:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档