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
 #include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct{
char id[];
char name[];
int grade;
}info;
bool cmp1(info a, info b){
return strcmp(a.id, b.id) < ;
}
bool cmp2(info a, info b){
if(strcmp(a.name, b.name) == )
return strcmp(a.id, b.id) < ;
else
return strcmp(a.name, b.name) < ;
}
bool cmp3(info a, info b){
if(a.grade == b.grade)
return strcmp(a.id, b.id) < ;
else
return a.grade < b.grade;
}
info stu[];
int main(){
int N, C;
scanf("%d%d", &N, &C);
for(int i = ; i < N; i++)
scanf("%s %s %d", stu[i].id, stu[i].name, &stu[i].grade);
if(C == )
sort(stu, stu + N, cmp1);
else if(C == )
sort(stu, stu + N, cmp2);
else
sort(stu, stu + N, cmp3);
for(int i = ; i < N; i++)
printf("%s %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
cin >> N;
return ;
}

总结:

1、简单题。只有一点以前没有注意到,在main函数里定义一个100000的数组时,竟然空间不足编译错误。其实只要把数组定义在main函数之外,作为全局变量即可。main函数内的变量在栈区,空间一般比较小,而全局变量一般不在栈区,空间较大。 当然也可以使用vector。

A1028. List Sorting的更多相关文章

  1. PAT A1028 List Sorting (25 分)——排序,字符串输出用printf

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

  2. PAT甲级——A1028 List Sorting

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

  3. PAT_A1028#List Sorting

    Source: PAT A1028 List Sorting (25 分) Description: Excel can sort records according to any column. N ...

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  6. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  7. U3D sorting layer, sort order, order in layer, layer深入辨析

    1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...

  8. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

  9. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

随机推荐

  1. 并行管理工具——pdsh

    1. pdsh安装2. pdsh常规使用2.1 pdsh2.2 pdcp 并行管理的方式有很多种: 命令行 一般是for循环 脚本 一般是expect+ssh等自编辑脚本 工具 pssh,pdsh,m ...

  2. Dijkstra及其堆优化

    朴素Dijkstra #include<bits/stdc++.h> using namespace std; const int inf=9999999; bool book[105]; ...

  3. 【CV】ICCV2015_Unsupervised Visual Representation Learning by Context Prediction

    Unsupervised Visual Representation Learning by Context Prediction Note here: it's a learning note on ...

  4. [BUAA软工]第二次博客作业---结对编程

    [BUAA软工]结对作业 项目 内容 这个作业属于哪个课程 北航软工 这个作业的要求在哪里 2019年软件工程基础-结对项目作业 我在这个课程的目标是 学习如何以团队的形式开发软件,提升个人软件开发能 ...

  5. LINUX内核分析第七周学习总结——可执行程序的装载

    LINUX内核分析第六周学习总结——进程的描述和进程的创建 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/cours ...

  6. “数学口袋精灵”第二个Sprint计划(第九天)

    第九天进度 任务分配: 冯美欣:欢迎界面背景音乐发现bug(一开始进入游戏可以播放音乐,进入游戏界面,再返回欢迎界面时,音乐播放不出来),仍在解决中: 吴舒婷:改进ui与音效 1.进度条.金黄色: 2 ...

  7. debug网页时小问题The source attachment does not contain the source for the file

    第一次debug总是出现下图问题,提示我没加源码... The source attachment does not contain the source for the file ... 解决方法: ...

  8. PHP使用Redis实现消息队列

    消息队列可以使用MySQL来实现,可以参考博客PHP使用MySQL实现消息队列,虽然用MySQL可以实现,但是一般不这么用,因为MySQL的数据都存在硬盘中,而从硬盘中对MySQL的操作,I/O花费的 ...

  9. Axios插件和loading的实现

    axios插件就是一个ajax插件 axios具有ajax的所有方法如 get post delete put等等的方法 使用时只需要引入即可 如import Axios form 'axios' 不 ...

  10. linux_查看磁盘与目录容量

    一.查看磁盘容量命令df(report file system disk space usage) 终端运行 $ df 输出结果 我的物理主机上的 /dev/sda5 是对应着主机硬盘的分区,字母 a ...