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. centos7 systemctl

    下机为systemctl指令systemctl enable *.service #开机运行服务systemctl disable *.service #取消开机运行systemctl start * ...

  2. ch4-持久存储

    1.处理数据和打印 man = [] other = [] try: data = open('sketch.txt') for each_line in data: try: (role, line ...

  3. Python 进阶(二)模块

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW4AAABpCAIAAACVsl7UAAAgAElEQVR4nO2993vUxho2/P4r33XwSr

  4. 安装postgresql之后为什么找不到postgresql service

    没有正常启动 postgresql service.可以 在运行里面 输入 services.msc 找到 postgresql 的服务.启动他.或者也可以用postgres 自带的 工具pg_ctl ...

  5. 使用synchronized(非this对象)同步代码块解决脏读问题

    首先通过示例来学习验证多个线程调用同一个方法时随机的. package syn_out_asyn; import java.util.ArrayList; import java.util.List; ...

  6. Android长截屏-- ScrollView,ListView及RecyclerView截屏

    http://blog.csdn.net/wbwjx/article/details/46674157       Android长截屏-- ScrollView,ListView及RecyclerV ...

  7. ansible的携带密码访问

    author:head森  chen date: 2018-08-13  10:28:34 1,ansible的安装 yum -y install epel-release yum -y instal ...

  8. Android 仿微信朋友圈发动态功能(相册图片多选)

    代码分享 代码名称: 仿微信朋友圈发动态功能(相册图片多选) 代码描述: 仿微信朋友圈发动态功能(相册图片多选) 代码托管地址: http://www.apkbus.com/android-15276 ...

  9. Mysql limit offset用法举例

    转自:http://blog.csdn.net/iastro/article/details/53037600 Mysql limit offset示例 例1,假设数据库表student存在13条数据 ...

  10. centos7.2启动级别

    systemctl set-default multi-user.target      设定默认为字符界面,也就是3 systemctl set-default graphical.target  ...