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 (≤10​5​​) 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的更多相关文章

  1. PAT 1028 List Sorting (25分) 用char[],不要用string

    题目 Excel can sort records according to any column. Now you are supposed to imitate this function. In ...

  2. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  3. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  4. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  5. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  6. PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*

    1039 Course List for Student (25 分)   Zhejiang University has 40000 students and provides 2500 cours ...

  7. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  8. 【PAT】B1085 PAT单位排行(25 分)(c++实现)

    终于做的有点眉目了,今天学习了一点stl的皮毛,解题瞬间变容易了 下边开始分析本题 这道题如果用纯c解决实在太麻烦,试了半天两个超时,果断放弃,还是用map方便: 我的方法与柳神的方法是有区别的,我只 ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  10. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

随机推荐

  1. CSS--使用Animate.css制作动画效果

    一 使用Animate.css动画 // 通过@import引入外部CSS资源; // 引入线上图片及JS文件; // 通过更改CSS类名生成不同类型的CSS3动画;   <!DOCTYPE h ...

  2. linux学习笔记-开机流程与主引导分区(MBR)

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 读鸟哥的linux私房菜-基础学习篇(第三版)3.2.4章节作此笔记 一.术语介绍: Bios:写入到主板上的一个程序,计算机开 ...

  3. Python 映射

    python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删除成员. ...

  4. python爬虫入门---第三篇:自动下载图片

    适用的图片网站:美桌 源代码: import requests import re import urllib from bs4 import BeautifulSoup def get_html_t ...

  5. MySQL chartset

    -- # https://dev.mysql.com/doc/refman/8.0/en/charset-database.html -- create database aixinyz; -- 默認 ...

  6. 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)

    题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...

  7. ios开发GCD(2)-dispatch_semaphore_t信号量计数器

    思考:现在有多个线程异步执行,我们想要同时最多只能执行2个或n个,该怎么办? dispatch_semaphore_t 看代码解析: NSLog(@"开始"); dispatch_ ...

  8. android一个倾斜的TextView,适用于标签效果

    描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...

  9. java 按字节读写二进制文件(Base64编码解码)

    最近在做项目时遇到这样一个需求:依次读取本地文件夹里所有文件的内容,转为JSON,发送到ActiveMQ的消息队列, 然后从MQ的消息队列上获取文件的信息,依次写到本地.常见的文件类型,比如.txt ...

  10. js,ajax,layer笔记(弹出层,在弹出一个弹框)

    整体认识: 因为作用域的问题,js 在页面初次加载时已近加载好了,所以要有第二次弹窗的效果,必须得在第一次成功之后再次让他加载js 代码: /*shaun*/showdetailsPag: funct ...