【算法笔记题解】PAT A1083.List Grades(25)

全文目录

前言

题目描述

输入描述

输出描述

输入示例

输出示例

解题思路

题目理解

代码实现

结果分析与总结


前言

昨天刷的那个被牛客网坑了一把,今天PAT恢复了,喜大普奔啊。今天这道题比较简单。 相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com


题目描述

1083 List Grades (25 分)https://pintia.cn/problem-sets/994805342720868352/problems/994805383929905152https://pintia.cn/problem-sets/994805342720868352/problems/994805383929905152

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

输出描述

For each test case you should output the student records of which the grades are in the given interval [grade1grade2] 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

解题思路

题目理解

PAT A级别的所有题目都是英文,其实题主作为一个六级都没考过的人都能看懂,相信大家都能看懂吧,看不懂也没关系,经常出现的单词也不多,多看就好了.

就是对所有的成绩进行排名,然后输出指定范围内的学生的姓名和学号就好了,25分的大水题。

相信代码你们看得懂👇

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct student{
    char name[11];  //姓名
    char id[11];    //准考证号
    int grade;      //成绩
};

bool cmp(student a, student b){//降序排列
    return a.grade > b.grade;
}

int main(){
    int n, low ,high;
    //读入所有的值
    scanf("%d",&n);
    student stu[n];
    for(int i = 0; i < n; ++i)  scanf("%s%s%d", stu[i].name, stu[i].id, &stu[i].grade);
    scanf("%d%d",&low,&high);
    sort(stu, stu + n, cmp);
    //初始化变量
    int i = 0;
    bool flag = true;

    while(stu[i].grade > high && i < n) i++;//找到第一个在范围内的元素
    while(stu[i].grade >= low && i < n){    //限制范围
        flag = false;
        printf("%s %s\n",stu[i].name, stu[i].id);
        i++;
    }

    if(flag) printf("NONE\n");
    
    return 0;
}

结果分析与总结

PAT官网所有测试点都通过了。

自己有个点没注意导致第2组数据一直没过。

就是在找元素在范围内的时候一定要注意i是否还在范围内,否则会越界。

边界的检查一定一定要特别注意。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值