【PAT】1028. List Sorting (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028
题目描述:
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
分析:
输入学生信息,按照指定项进行排序。当按照name或grade排序时如果存在一样的情况就按照学号递增的形式输出。
原本是想用c++做,num和name用string类型表示。但是用c++中的cin进行输入时会有一组数据在最终提交时超时。所以改用scanf输入。
由于scanf不能输入string类型,所以将string类型用char[]代替。
但是如果num和name都用char[]类型,在输出时又会出错,见“错误代码一”。
后面就将num为int型,为了满足num为6位的要求,要用printf("%06d",num) 这种形式输出。
错误代码一:
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std; typedef struct student
{
char num[6];
char name[20];
int grade;
}Student; int n;
bool comparison(Student a,Student b)
{
if(n==1)
{
return strcmp(a.num,b.num)<0;
}
else if(n==2)
{
if(strcmp(a.name,b.name) == 0)
return strcmp(a.num,b.num);
return strcmp(a.name,b.name)<0;
}
else if(n==3)
{
if(a.grade == b.grade)
return strcmp(a.num,b.num) < 0;
return a.grade < b.grade;
}
} int main()
{
int M;
cin>>M>>n;
vector<Student> s(M);
int i;
for(i=0; i<M; i++)
//cin>>s[i].num>>s[i].name>>s[i].grade;
scanf("%s%s%d",&s[i].num,&s[i].name,&s[i].grade); sort(s.begin(),s.end(),comparison);
for(i=0; i<M; i++)
cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].grade<<endl; return 0;
}
最终的输出不正确。但是
原因是???
正确代码:
参考:http://blog.csdn.net/sunbaigui/article/details/8657115
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std; typedef struct student
{
int num;
char name[20];
int grade;
}Student; int n;
bool comparison(Student a,Student b)
{
if(n==1)
{
return a.num<b.num;
}
else if(n==2)
{
if(strcmp(a.name,b.name) == 0)
return a.num<b.num;
return strcmp(a.name,b.name)<0;
}
else if(n==3)
{
if(a.grade == b.grade)
return a.num<b.num;
return a.grade < b.grade;
}
} int main()
{
int M;
cin>>M>>n;
vector<Student> s(M);
int i;
for(i=0; i<M; i++)
scanf("%d%s%d",&s[i].num,&s[i].name,&s[i].grade); sort(s.begin(),s.end(),comparison);
for(i=0; i<M; i++)
printf("%06d %s %d\n",s[i].num,s[i].name,s[i].grade); return 0;
}
【PAT】1028. List Sorting (25)的更多相关文章
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1028. List Sorting (25) 【结构体排序】
题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 【PAT】1015 德才论 (25)(25 分)
1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得 ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 【PAT】B1055 集体照(25 分)
很简单的two points问题 ##注意:K是行数 #include<stdio.h> #include<string.h> #include<map> #inc ...
- 【PAT】1051 Pop Sequence (25)(25 分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 【PAT】1063. Set Similarity (25) 待改进
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...
随机推荐
- T-sql 行转列,数据库查询分页
1 USE [APS_Future_FT] 2 GO 3 /****** Object: StoredProcedure [dbo].[A_CrudePrice] Script Date: 2013/ ...
- hiho1393二分图多重匹配
题目链接:[http://hihocoder.com/problemset/problem/1393] 题意:中文题意. 题解:二分图的多重匹配.主要是建图然后跑一个最带流,再判断一下就可以了. 建图 ...
- BZOJ1552/3506 [Cerc2007]robotic sort
Splay 与之前不同的是如果你仅仅是翻转左右区间的话可以在find里面做因为对他有影响的子树在做之前一定在他的上面从上到下搜索的过程可以把rever做了. 但这道题要求我们输出转换之前的,因此不能保 ...
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- [CC-ANUCBC]Cards, bags and coins
[CC-ANUCBC]Cards, bags and coins 题目大意: 给你\(n(n\le10^5)\)个数,\(q(q\le30)\)次询问,问从中选取若干个数使得这些数之和为\(m(m\l ...
- 74.Interesting Sequence(有趣的数列)(拓扑排序)
Interesting Sequence(有趣的数列)[Special judge] 题目概述:是否存在一个长度为n的整数数列,其任意连续p项之和为正数而任意连续q项之和为负数? 方法:连续项a[i] ...
- HDU 5644 King's Pilots 费用流
King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...
- 深入浅出JDBC-操作时间与大对象(Clob/Blob)
一.时间(Date.Time.Timestamp) java.sql.Date/java.sql.Time/java.sql.Timestamp extends java.util.Date publ ...
- 如果内容很长ueditor编辑辑器怎么出现滚动条
在开发网站的时候,有的页面需要加载ueditor编辑器,如果内容很长,默认设置的时候编辑器会根据内容拉长,而不是页面出现滚动条,如果拖动页面滚条,会比较麻烦,要拖动很长才能看到提交按钮. 如何才能让编 ...
- How to use transparent PNG icons with Delphi ImageList
http://www.aha-soft.com/faq/delphi-imagelist-png.htm Query: "Embarcadero Delphi ImageList does ...