1083 List Grades (25 分)

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 [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.

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 Input 2:

2
Jean AA980920 60
Ann CS01 80
90 95

Sample Output 2:

NONE

题目大意:给出n个学生,姓名id分数,并给出分数区间,要求在分数区间内的学生按分数排序输出。

//这个就很简单啦,我的AC:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Stu{
string name,id;
int sco;
};
vector<Stu> allstu;
vector<Stu> stu;
bool cmp(Stu&a,Stu&b){
return a.sco>b.sco;
}
int main() {
int n;
cin>>n;
string name,id;
int sco,low,high;
for(int i=;i<n;i++){
cin>>name>>id>>sco;
allstu.push_back(Stu{name,id,sco});
}
cin>>low>>high;
for(auto it=allstu.begin();it!=allstu.end();){
if(it->sco<low||it->sco>high){
it=allstu.erase(it);//
}else it++;
}
if(allstu.size()==){
cout<<"NONE";
}else{
sort(allstu.begin(),allstu.end(),cmp);
for(int i=;i<allstu.size();i++){
cout<<allstu[i].name<<" "<<allstu[i].id<<'\n';
}
}
return ;
}

//以下是遇到的问题:

1,关于使用vector::erase函数。

参数是iterator迭代器,转自:https://www.cnblogs.com/zsq1993/p/5930229.html

for(vector<int>::iterator iter=veci.begin(); iter!=veci.end(); iter++)
{
if( *iter == )
veci.erase(iter);
}

乍一看这段代码,很正常。其实这里面隐藏着一个很严重的错误:当veci.erase(iter)之后,iter就变成了一个野指针,对一个野指针进行 iter++ 是肯定会出错的。

for(vector<int>::iterator iter=veci.begin(); iter!=veci.end(); iter++)
{
if( *iter == )
iter = veci.erase(iter);
}

这段代码也是错误的:1)无法删除两个连续的"3"; 2)当3位于vector最后位置的时候,也会出错(在veci.end()上执行 ++ 操作)

for(vector<int>::iterator iter=veci.begin(); iter!=veci.end(); )
{
if( *iter == )
iter = veci.erase(iter);
else
iter ++ ;
}

第三种是正确的写法,学习了!!

PAT 1083 List Grades[简单]的更多相关文章

  1. PAT 1083 List Grades

    #include <cstdio> #include <cstdlib> using namespace std; class Stu { public: ]; ]; }; i ...

  2. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  3. PAT (Advanced Level) 1083. List Grades (25)

    简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  4. PAT 甲级 1083 List Grades

    https://pintia.cn/problem-sets/994805342720868352/problems/994805383929905152 Given a list of N stud ...

  5. 【PAT甲级】1083 List Grades (25 分)

    题意: 输入一个正整数N(<=101),接着输入N个学生的姓名,id和成绩.接着输入两个正整数X,Y(0<=X,Y<=100),逆序输出成绩在x,y之间的学生的姓名和id. tric ...

  6. 1083. List Grades (25)-简单的排序

    给定区间[L,R],给出在这区间之内的学生,并且按照他们的成绩非升序的顺序输出. #include <iostream> #include <cstdio> #include ...

  7. 1083. List Grades (25)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1083 and the ...

  8. PAT 1089 狼人杀-简单版(20 分)(代码+测试点分析)

    1089 狼人杀-简单版(20 分) 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局"狼人杀"游戏中,1 号玩家 ...

  9. [PAT]A+B Format[简单]

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

随机推荐

  1. linux下更改vncserver的密码

    Linux下VNC配置多个桌面和修改密码 1:vncserver2:iptables -I INPUT -p tcp --dport 5901 -j ACCEPT   客户端方式3:iptables ...

  2. CMake添加debug版本后缀

  3. PyQt4菜单栏

    菜单栏是GUI程序最明显的组成部分.它由一组位于不同菜单中的命令组成.在控制台程序中,我们必须记住那些晦涩难懂的命令.但在GUI程序中,通过菜单栏我们将命令合理的放置在不同的菜单中来降低学习新应用程序 ...

  4. nginx服务器配置说明

    总结nginx的一些配置选项: nginx全局配置文件 # 定义nginx运行的用户和组//一个默认同时为用户和组 //没有则默认为nobody user www-data; # nginx进程数,建 ...

  5. java基础---->Reference的使用(一)

    这里记录一些java中关于引用的知识. java中的引用 一.java中关于引用的测试 public class ReferenceTest { public static void main(Str ...

  6. linux主机下的Vmware Workstation配置NAT设置 端口映射-Ubuntu为例

    最近折腾虚拟机,由于是在linux下进行的,而相关资料比较少,所以遇到了一些问题. 一个就是配置vmware workstation的NAT设置.因为一般来说,NAT可以共享主机的ip,从而能以主机身 ...

  7. diamond types are not supported at this language level

    在intellij导入git项目之后出现 diamond types are not supported at this language level错误 或者String等报错 File->P ...

  8. LeetCode——Missing Number

    Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...

  9. Egret打包App 短暂黑屏解决方案 (Egret4.1.0)

    论坛已经有人解决:http://bbs.egret.com/forum.php?mod=viewthread&tid=30288&highlight=app%2B%E9%BB%91%E ...

  10. Android自定义控件实战——仿淘宝商品浏览界面

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929 用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个Scr ...