PAT 1083 List Grades[简单]
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 [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:
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[简单]的更多相关文章
- PAT 1083 List Grades
#include <cstdio> #include <cstdlib> using namespace std; class Stu { public: ]; ]; }; i ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT (Advanced Level) 1083. List Grades (25)
简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT 甲级 1083 List Grades
https://pintia.cn/problem-sets/994805342720868352/problems/994805383929905152 Given a list of N stud ...
- 【PAT甲级】1083 List Grades (25 分)
题意: 输入一个正整数N(<=101),接着输入N个学生的姓名,id和成绩.接着输入两个正整数X,Y(0<=X,Y<=100),逆序输出成绩在x,y之间的学生的姓名和id. tric ...
- 1083. List Grades (25)-简单的排序
给定区间[L,R],给出在这区间之内的学生,并且按照他们的成绩非升序的顺序输出. #include <iostream> #include <cstdio> #include ...
- 1083. List Grades (25)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1083 and the ...
- PAT 1089 狼人杀-简单版(20 分)(代码+测试点分析)
1089 狼人杀-简单版(20 分) 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局"狼人杀"游戏中,1 号玩家 ...
- [PAT]A+B Format[简单]
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
随机推荐
- mysql读写分离实战
一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
- unity 打包apk安装失败
Unity 打包Apk安装失败,错误提示:安卓未完成. 解决方案:检查BundleID是否一致
- PHP5 session 详解【经典】
原文:http://www.pkwind.com/php5-session-xiang-jie-jing-dian http协议是WEB服务器与客户端 (浏览器)相互通信的协议,它是一种无状态协议.所 ...
- nested exception is org.springframework.beans.factory.BeanCreationException: 不能注入对象 创建对象失败 spring
[出现错误的背景] 在使用Spring+SpringMVC+Mybatis SSM集成框架时,服务器启动就会报错. [错误根源] XML配置错误. [解决方案] 第一步.查找springmvc.xml ...
- c++ 利用new动态的定义二维数组
#include <iostream> using namespace std; int main() { , col = ; // key code: 申请空间 int **edge = ...
- 关于Java中按值传递和按引用传递的问题详解
写了两个方法,一个是多关键字的快速排序,一个是基于多关键字的基速排序.两个方法的参数列表是一样一样的,但是快速排序正常工作,但是基数排序传出来的参数一点没有改变,苦思冥想了半天也没想通是怎么回事,于是 ...
- maven项目使用SOLR时报 previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" 错的解决方法
环境:Apache solr4.8,maven3,IntellijIDEA 想在项目中使用solr 在pom.xml文件中添加了solr的依赖 solr-core,solrj 和solr-dataim ...
- 【BZOJ2314】士兵的放置 树形DP
[BZOJ2314]士兵的放置 Description 八中有N个房间和N-1双向通道,任意两个房间均可到达.现在出了一件极BT的事,就是八中开始闹鬼了.老大决定加强安保,现在如果在某个房间中放一个士 ...
- linux的ls命令输出结果的逐条解释
转自:http://blog.csdn.net/god123209/article/details/7193485 ls 命令的含义是list显示当前目录中的文件名字.注意不加参数它显示除隐藏文件外的 ...
- 修改MySQL命令提示符
当前session可以直接用prompt修改 mysql> prompt \u@\h \d \r:\m:\s>PROMPT set to '\u@\h \d \r:\m:\s>'ro ...