HDU 5427 A problem of sorting 水题
来源:互联网 发布:java 定义泛型返回值 编辑:程序博客网 时间:2024/06/05 05:25
问题描述
给出一张许多人的年龄和生日表。你需要从年轻到年老输出人们的名字。(没有人年龄相同)
输入描述
第一行包含一个正整数T(T \leq 5)T(T≤5),表示数据组数。
对于每组数据,第一行包含一个正整数n(1 \leq n \leq 100)n(1≤n≤100),表示人数,接下来n行,每行包含一个姓名和生日年份(1900-2015),用一个空格隔开。姓名长度大于0且不大于100。注意姓名中只包含字母,数字和空格。
输出描述
对于每组数据,输出nn行姓名。
输入样例
2
1
FancyCoder 1996
2
FancyCoder 1996
xyz111 1997
输出样例
FancyCoder
xyz111
FancyCoder
解题思路:
开一个结构体,里面有名字和出生日期,读入一整个串,然后将后四位转化为出生日期给结构体中的日期,将名字给结构体中的名字,然后重定义运算符sort排序一下就好了。
这道题被坑在了string上,string的结尾并不是’\0’,我以为赋值成了’\0’就不会就输出,结果事实是我输出的是名字和5个空格,最蛋疼的是多输出空格给的不是PE而是WA。。。。。
第二天看了看,有两种方法解题:
第一种是使用迭代器:
#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<set>#include<string>#include<map>using namespace std;struct p{ string name; int year;};bool cmp(p a1,p a2){ return a1.year>a2.year;}int main(){ //freopen("in.txt","r",stdin); int t; scanf("%d",&t); getchar(); while(t--) { p a[105]; int n; scanf("%d",&n); getchar(); for(int i=0; i<n; i++) { getline(cin,a[i].name); string &b=a[i].name; a[i].year=0; for(string ::iterator j=b.end()-4;j!=b.end();) { a[i].year=a[i].year*10+*j-'0'; b.erase(j); } b.erase(b.end()-1); } sort(a,a+n,cmp); for(int i=0; i<n; i++) cout<<a[i].name<<endl; } return 0;}
第二种是用截取字符串的函数
string substr(int pos = 0,int n = npos) const;
//返回pos开始的n个字符组成的字符串
#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<set>#include<string>#include<map>using namespace std;struct p{ string name; int year; p() { year=0; }};bool cmp(p a1,p a2){ return a1.year>a2.year;}int main(){ int t; scanf("%d",&t); getchar(); while(t--) { p a[105]; int n; scanf("%d",&n); getchar(); for(int i=1; i<=n; i++) { getline(cin,a[i].name); string &b=a[i].name; for(int j=b.size()-4;j<b.size(); j++) a[i].year=a[i].year*10+b[j]-'0'; b=b.substr(0,b.size()-5); } sort(a+1,a+n+1,cmp); for(int i=1; i<=n; i++) cout<<a[i].name<<endl; } return 0;}
友情提示:记得getchar();
0 0
- HDU 5427 A problem of sorting 水题
- hdu 5427 A problem of sorting
- hdu 5427 A problem of sorting 排序
- hdu 5427 A problem of sorting
- hdu 5427 A problem of sorting
- HDU 5427 A problem of sorting
- HDU 5427 A problem of sorting
- hdu 5427 A problem of sorting 排序
- hdu 5427 A problem of sorting(水)
- hdu 5427 A problem of sorting 解题报告
- HDU 5427-A problem of sorting(字符串处理)
- 【map容器+string】hdu 5427 A problem of sorting
- HDU——5427 A problem of sorting
- A problem of sorting
- HDOJ 5427 A problem of sorting
- HDU 5431/BC 54A A problem of sorting
- HDU5429----A problem of sorting
- HDU 5427 A problem of sorting(字符串处理+排序)——BestCoder Round #54(div.2)
- 友善之臂smart210启动wifi热点
- 【高级算法】遗传,模拟退火,禁忌,Lasvegas等算法详解与实现
- JAVA_SE系列:17.扩展后的赋值运算符
- netty 流数据的传输处理
- Spark通过Java Web提交任务
- HDU 5427 A problem of sorting 水题
- 关于PHP引用(符号&)的用法
- Material Design之视图状态改变
- 基于共享内存多级hash设计
- 信息系统项目管理八项经典技术
- [iOS]#Swift#可选解析-optional
- JAVA_SE系列:18.比较运算符、逻辑运算符、三目运算符
- Android android:windowSoftInputMode 属性整理
- Android Studio上Git使用指南