PAT_A 1028. List Sorting (25)
来源:互联网 发布:linux用cp复制文件 编辑:程序博客网 时间:2024/06/03 16:26
1028. List Sorting (25)
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input
Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
Output
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.
Sample Input 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
分析:本题就是排序。
- 利用vector/list algotithm的sort进行排序,最开始用vector结果最后一个用例超时!换成list排序快点,仍是超时!最好查到原来是cin和cout的问题!
- 尽量少用cin cout ,换成scanf printf
code:
#include<iostream>#include<algorithm>#include<list>#include<cstdio>#include<cstring>int N;int C;using namespace std;struct IN{ int num; char name[30]; int score;};list<IN>d;bool comp1(IN a,IN b){ if(a.num<b.num) return true; else return false;}bool comp2(IN a,IN b){ int c=strcmp(a.name,b.name); if(c<0) return true; else if(c>0) return false; else { if(a.num<b.num) return true; else return false; }}bool comp3(IN a,IN b){ if(a.score<b.score) return true; else if(a.score>b.score) return false; else { if(a.num<b.num) return true; else return false; }}int main(){ cin>>N>>C; IN tmp; for(int i=0;i<N;i++) { /* 长见识了,cin cout太慢,并不是算法的问题 cin>>tmp.num>>tmp.name>>tmp.score; */ scanf("%d %s %d", &tmp.num, tmp.name, &tmp.score); d.push_back(tmp); } switch(C) { case 1: d.sort(comp1); break; case 2: d.sort(comp2); break; case 3: d.sort(comp3); break; default: break; }/* switch(C) { case 1: sort(d.begin(),d.end(),comp1); break; case 2: sort(d.begin(),d.end(),comp2); break; case 3: sort(d.begin(),d.end(),comp3); break; default: break; } */ list<IN>::iterator it; for(it=d.begin();it!=d.end();++it) { printf("%06d %s %d\n", it->num, it->name, it->score); } return 0;}
-
- 参考
- cin cout 太慢,改用scanf printf
- PAT_A 1028. List Sorting (25)
- PAT_A 1052. Linked List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 取数字问题2-动规
- 【AngularJS】自定义过滤器
- 补充装饰器
- eee
- win10 安装 Python 和 pip工具
- PAT_A 1028. List Sorting (25)
- ScrollView 继续上拉查看图文详情
- 《机器学习实战》——Logistic回归
- 城市交通
- Educational Codeforces Round 1 D. Igor In the Museum BFS,带权并查集
- BIT2013年上机第三题
- 【并发】java使用volatile的场景
- SpringMVC与JQuery EasyUI DataGrid传递JSON数据
- 冷光美白仪器 使用说明 美白牙齿