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 ...
随机推荐
- Java使用foreach语句对数组成员遍历输出
/** * 本程序使用foreach语句对数组成员进行遍历输出 * @author Lei * @version 2018-7-23 */ public class ForeachDemo { pub ...
- Java static 语句块
总结前一天学习,参考原文http://www.cnblogs.com/dolphin0520/p/3799052.html1: 对Static有了进一步的认识 这个地方重点是初始化各个变量顺序, ...
- iomanip的作用 C++
c++程序里面经常见到下面的头文件 #include <iomanip> 这里面iomanip的作用比较多: 主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,s ...
- 垂直水平居中总结css
水平居中:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 让绝对定位的div垂直水平居中一(大盒子设置个相对定位) ...
- 关于wsgi协议的理解
基础概念 首先要了解 WSGI 规范的概念,WSGI(Web Server Gateway Interface)规范描述了web server(Gunicorn,uWSGI等)如何与web appli ...
- 【读书笔记】iOS-音频设备访问
音频的输入是通过麦克风实现,音频的输出是通过扬声气实现的.在iOS系统中无法直接操控麦克风和扬声器,苹果提供了丰富的音频API. 一,音频API介绍 在iOS和Mac OS X上开发音频应用,主要有两 ...
- vue-cli脚手架目录一览
最近在学习vue,看的稀里糊涂.今天从头开始,把cli配置的vue项目目录和配置文件搞清楚. 先看看整个项目目录结构: 再看看build文件夹下相关文件及目录: config文件夹下目录和文件: 接下 ...
- Android基础之内容提供者的实现
内容提供者可以实现应用间查询数据库的需求 一.在提供数据库访问的应用设置内容提供者 public class AccountProvider extends ContentProvider { sta ...
- 【Java入门提高篇】Day23 Java容器类详解(六)HashMap源码分析(中)
上一篇中对HashMap中的基本内容做了详细的介绍,解析了其中的get和put方法,想必大家对于HashMap也有了更好的认识,本篇将从了算法的角度,来分析HashMap中的那些函数. HashCod ...
- (后台)java 读取excel 文件 Unable to recognize OLE stream 错误
原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls). 光修改文件后缀不行,需要文件另存(或者导出)为 .xls Excel 1997-2004 ...