PAT A1028 List Sorting (25 分)——排序,字符串输出用printf
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then Nlines 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 Specification:
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
#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
const int maxn=;
struct stu{
string id;
string name;
int grade;
}students[maxn];
bool cmp1(stu s1,stu s2){
return s1.id<s2.id;
}
bool cmp2(stu s1,stu s2){
if(s1.name==s2.name) return s1.id<s2.id;
else return s1.name<s2.name;
}
bool cmp3(stu s1,stu s2){
if(s1.grade==s2.grade) return s1.id<s2.id;
else return s1.grade<s2.grade;
}
int main(){
int n,c;
scanf("%d %d",&n,&c);
for(int i=;i<n;i++){
cin>>students[i].id>>students[i].name>>students[i].grade;
}
if(c==){
sort(students,students+n,cmp1);
}
else if(c==){
sort(students,students+n,cmp2);
}
else if(c==){
sort(students,students+n,cmp3);
}
for(int i=;i<n;i++){
printf("%s %s %d\n",students[i].id.c_str(),students[i].name.c_str(),students[i].grade);
}
}
注意点:很简单的排序题,但是最后一个测试点会超时,原因是cout很慢,一开始以为是sort很慢,想是不是要用priority_queue,发现一样超时。然后把id改成int试了试还是超时,百度了一下,发现人家也都是sort做的唯一不同输出name也用printf。最后果然是cout速度太慢,printf输出string要用c_str。
PAT A1028 List Sorting (25 分)——排序,字符串输出用printf的更多相关文章
- PAT 1028 List Sorting (25分) 用char[],不要用string
题目 Excel can sort records according to any column. Now you are supposed to imitate this function. In ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
1039 Course List for Student (25 分) Zhejiang University has 40000 students and provides 2500 cours ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- 【PAT】B1085 PAT单位排行(25 分)(c++实现)
终于做的有点眉目了,今天学习了一点stl的皮毛,解题瞬间变容易了 下边开始分析本题 这道题如果用纯c解决实在太麻烦,试了半天两个超时,果断放弃,还是用map方便: 我的方法与柳神的方法是有区别的,我只 ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
随机推荐
- potplayer打开多个视频文件
选项-基本-多重处理方式-新开一个播放进程播放
- 2018-02-27 "Literate Programming"一书摘记之一
书到后才发现是Knuth的论文集, 第一篇就在网上: Computer programming as an art (1974). 其中"Taste and Style"(品味和风 ...
- 2017-10-29 用中文命名API的意义和途径
"中文编程"知乎专栏原链 在前文对在代码中使用中文命名的质疑与回应中阐述了在代码中使用中文命名的益处. 此文将从软件使用者的角度阐述对API中文化的意义并探讨实现途径. 当然, 文 ...
- win7x64上配置 vs2015
win7 x64配置vs2015成功关键 1. 首先查看win7版本是否是sp1或以上: [计算机]----->[属性] 2. 将IE浏览器升级到IE11: IE11[百度网盘下载],提取码:[ ...
- Linux 操作系统下为网卡配置ip
Linux操作系统下为网卡配置ip by:授客 QQ:1033553122 1. Linux单一网卡设置多IP的配置方法 在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,..... ...
- linux编写.sh脚本并赋权限
今日打包编译项目时,发现缺少一个git pull更新项目的脚本,所以在linux编写了一个gitpull.sh脚本,能够实现更新项目的需求. 1.首先vi gitpull.sh或者使用vim编辑器. ...
- maven(九),install安装到本地仓库
下载oracle驱动jar包 在maven中央仓库中,是没有oracle驱动jar包的.因为oracle是商业软件,其jar包不允许用作开源用途.从http://www.mvnrepository.c ...
- [20170824]11G备库启用DRCP连接.txt
[20170824]11G备库启用DRCP连接.txt --//参考链接:http://blog.itpub.net/267265/viewspace-2099397/blogs.oracle.com ...
- Postgresql_根据执行计划优化SQL
执行计划路径选择 postgresql查询规划过程中,查询请求的不同执行方案是通过建立不同的路径来表达的,在生成许多符合条件的路径之后,要从中选择出代价最小的路径,把它转化为一个计划,传递给执行器执行 ...
- webApi core2 DI通过代码来获取容器里面已注入的对象
请求服务 来自 HttpContext 的一次 ASP.NET 请求中可用的服务通过 RequestServices 集合公开的. 请求服务将你配置的服务和请求描述为应用程序的一部分.当你的对象指定依 ...