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 ...
随机推荐
- elasticsearch6.7 05. Document APIs(6)UPDATE API
5. UPDATE API 更新操作可以使用脚本来更新.更新的时候会先从索引中获取文档数据(在每个分片中的集合),然后运行脚本(使用可选的脚本语言和参数),再果进行索引(还允许删除或忽略该操作).它使 ...
- 20个Chrome DevTools调试技巧
译者按: Chrome DevTools很强大,甚至可以替代IDE了! 原文: Art of debugging with Chrome DevTools 译者: Fundebug 为了保证可读性,本 ...
- JavaScript数组学习总结
数组 数组 1.数组:数组是一组数据(数据类型不限,任意)的有序集合===>我们写代码,一般一个数组只放一种数据类型的数据 2.我们写代码,一般一个数组只放一种类型的数据 3.注意: 大多数 ...
- docker 搭建maven 私服
# 搜索镜像 docker search nexus; #拉取nexus镜像docker pull sonatype/nexus; #运行 -id 创建守护式容器--privileged=true 授 ...
- 双网卡设置(转:https://www.cnblogs.com/visionfeng/p/5825078.html)
双网卡一般情况不能有两个网关 特别是别设内网网关,这样有段时间过后会出现访问不了外网情况 支持两个网关的方法倒是有 让双网卡一台电脑拥有两个网关(转) 首先要声明一点,双网卡一台电脑拥有两个网关是不可 ...
- Code::Blocks + GDAL
[root@server20 ~]# yum install cmake cmake-gui [root@server20 ~]# yum install cairo-devel libcurl-de ...
- Tomcat 部署外部系统
/usr/local/tomcat/conf/Catalina/localhost/resource.xml <?xml version='1.0' encoding='utf-8' ?> ...
- 国网SGCC_UAP 反编译.class文件源代码
SGCC_UAP和eclipse操作方式差不多,对于用惯了IDEA和Android Studio的人来说非常不方便,按住Ctrl点击类名不能查看源码. 因为jar包下都是.class文件,所以需要安装 ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- [20171031]markhot.txt
[20171031]markhot.txt --//昨天看了https://jonathanlewis.wordpress.com/2017/10/02/markhot/,测试看看这样时候可以减少争用 ...