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 ...
随机推荐
- 超全面的JavaWeb笔记day23<AJAX>
AJAX AJAX概述 1 什么是AJAX AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言 ...
- python2.0_s12_day12_css样式详解
CSScss是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. CSS 存放方式有三种: 一种写法:在<body></body>内部 ...
- ASP代码审计学习笔记 -2.XSS跨站脚本
XSS漏洞: 漏洞代码: <% xss=request("xss") response.write(xss) %> 漏洞利用: 漏洞修复: Server.HTMLEnc ...
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
- K-mean和k-mean++
(1)k-mean聚类 k-mean聚类比较容易理解就是一个计算距离,找中心点,计算距离,找中心点反复迭代的过程, 给定样本集D={x1,x2,...,xm},k均值算法针对聚类所得簇划分C={C1, ...
- IE8及以下的数组处理与其它浏览器的不同
在解决search-box的bug时,由于IE8-的数组处理与其它浏览器的不同,而导致报错. 示例:arr=[1,3,3,]; 当数组的最后是一个逗号时: IE9+默认 arr=[1,3,3];也就是 ...
- exports和module.exports区别
参考:module.exports与exports的区别.关于exports的总结 exports 和 module.exports 的区别 module.exports是真正的模块接口,而expor ...
- 动态设置progressBar的进度
progressDrawable = this.getResources().getDrawable(R.drawable.image); progressDrawable.setBounds(mSe ...
- mysql日期处在某两个时间段之间的between比较
where SYSDATE() between '2018-08-28 09:21:48' and '2018-08-28 09:25:48' sysdate()等于2018-08-28 09:23: ...
- mobiscroll的例子
官网:https://docs.mobiscroll.com/4-3-2/jquery/datetime#options ............. <!DOCTYPE html>< ...