全文目录
前言
昨天刷的那个被牛客网坑了一把,今天PAT恢复了,喜大普奔啊。今天这道题比较简单。 相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com
题目描述
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 [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
解题思路
题目理解
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是否还在范围内,否则会越界。
边界的检查一定一定要特别注意。