PAT甲级 1028
来源:互联网 发布:软件过程成熟度cmm 编辑:程序博客网 时间:2024/06/17 13:59
List Sorting
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
考点
- strcmp的使用。
- sort。
题解
- 按照题意3个来排序 。
- 注意对name排序。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=100000+10;struct node{ int id,grade; char name[8+1];}records[maxn];bool cmp1(node a,node b){ return a.id<b.id;}bool cmp2(node a,node b){ if(strcmp(a.name,b.name)==0){ return a.id<b.id; } return strcmp(a.name,b.name)<0;}bool cmp3(node a,node b){ if(b.grade==a.grade){ return a.id<b.id; } return a.grade<b.grade;}void input_data(int n){ for(int i=0;i<n;++i){ scanf("%d %s %d",&records[i].id,records[i].name,&records[i].grade); }}void output_data(int n){ for(int i=0;i<n;++i){ printf("%06d %s %d\n",records[i].id,records[i].name,records[i].grade); }}int main(){ //freopen("./in","r",stdin); int N,C; scanf("%d %d",&N,&C); input_data(N); if(C==1)sort(records,records+N,cmp1); else if(C==2)sort(records,records+N,cmp2); else if(C==3)sort(records,records+N,cmp3); output_data(N);}
- PAT(甲级)1028
- 浙大PAT甲级 1028
- pat甲级1028
- PAT甲级1028
- 浙大PAT甲级-1028
- PAT甲级 1028
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- STL学习笔记6— —容器forward_list
- 【DL--03】深度学习基本概念—张量
- android
- 值类型和引用类型
- Myeclipse2013下安装Axis2插件
- PAT甲级 1028
- 【F2C】elasitcsearch/ELK学习随笔——X-PACK身份验证
- 考研英语笔记——时文长难句二
- 使用jquery提交form表单-submit方法总结
- 安装apache 教程
- Leetcode Word Ladder
- 03 最长不重复子串Longest Substring Without Repeating Characters
- [Leetcode] 299. Bulls and Cows 解题报告
- 【Java生成二维码】java生成和解析二维码,只用一个jar包,QRCODE,可复制粘贴直接用