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. HTML 5 拖放

    拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 拖放事件 1. ...

  2. HAOI2016 找相同字符 后缀自动机

    两个串,考虑一建一跑.枚举模式串的位置\(i\),考虑每次统计以\(i\)结尾的所有符合要求的串.在后缀自动机上走时记录当前匹配长度\(curlen\),则当前节点的贡献是\((curlen-len[ ...

  3. Peer Programming Project: 4 Elevators Scheduler 附加题 157 165

    1.改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. 每个电梯增加目标楼层数组,这样可以更好地进行任务的分配,在我们的电梯中,这个数 ...

  4. Linux内核期末总结

    20135316王剑桥<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC 1000029000 第一周 http://www.cn ...

  5. Git学习笔记 第二章

    文件相关操作 修改readme.txt文件,执行 git status 命令查看当前仓库状态 git status 位于分支 master 尚未暂存以备提交的变更: (使用 "git add ...

  6. Java设计模式之工厂模式(Factory模式)介绍(转载)

    原文见:http://www.jb51.net/article/62068.htm 这篇文章主要介绍了Java设计模式之工厂模式(Factory模式)介绍,本文讲解了为何使用工厂模式.工厂方法.抽象工 ...

  7. idHTTP 向网站发送json格式数据

    idHTTP 向网站发送json格式数据 var rbody:tstringstream; begin rbody:=tstringstream.Create('{"name":& ...

  8. 数据驱动测试之—— Excel+TestNG

    对于利用Webdriver做自动化的童鞋,对于如何将元素或者输入数据如何和编码分离都应该不会太陌生,本着一边学习一边分享的心态,大概总结了一下关于利用CSV.XML以及Excel来存放数据,然后在结合 ...

  9. [转帖]浅析Servlet执行原理

    浅析Servlet执行原理 原贴地址: https://www.cnblogs.com/wangjiming/p/10360327.html 原作者画的图挺好. 自己之前看过iis的一些配置文档 但是 ...

  10. [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护

    1.公司组织竞品分析, 选择了SAP的 SAP4HANA作为竞品 这边协助同事搭建了SAP4HANA的测试环境: 备注 这个环境 应该是同事通过一些渠道获取到的. 里面是基于这个虚拟机进行的说明:: ...