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. http指南(2)--代理

    代理 单个客户端专用的代理称为私有代理,众多客户端共享的代理被称为公共代理 代理与网关的对比:代理连接的是两个或多个使用相同协议的应用程序,而网关连接的则是两个或多个使用不同协议的端点.网关扮演的是“ ...

  2. Linux df du 命令

    df 命令 检查磁盘空间占用情况(并不能查看某个目录占用的磁盘大小). 命令格式:df [option] -h 以容易理解的格式(给人看的格式)输出文件系统分区使用情况,例如 10kB.10MB.10 ...

  3. 002-打开文件管理规范-20190406.bat

    rem 002-打开文件管理规范-20190406.bat start /max https://www.cnblogs.com/delphixx/p/10652763.htmlcopy %~0 C: ...

  4. php7与之前的区别和更新【转】

    http://blog.csdn.net/u011957758/article/details/73320083 本文是一篇讲座听后+后续研究的总结. 话说当年追时髦,php7一出就给电脑立马装上了, ...

  5. 12.25daily_scrum

    今天是圣诞节,大家在度过了一个愉快的节日同时,同时也收到了最好的圣诞礼物,就是调试工作已经进入尾声,接下来我们组的主要任务就是M2阶段的总结了.为了更好的做好M2阶段的收官工作,我们组决定分配相当的一 ...

  6. Week 2 代码审查

    我的伙伴是6班的小伙子潘礼鹏,经过几天的相处我觉得真的是说话很有趣的人,性格非常好,我们很划得来. 以下为我对他的代码的审查结果: VS2012与VS2013的兼容性 在这里写一个工具集的问题,不同的 ...

  7. 【实践报告】Linux实践二

    3.编译并安装内核与模块 sudo make bzImage –j3         编译内核 sudo make modules –j3         编译模块 sudo make modules ...

  8. phpcms全站搜索

    这篇博客已经移至http://www.cnblogs.com/nuanai/p/8028562.html中~~~~~~

  9. (转)SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间

    原因: 出现这种问题多半是因为你插入或者更新数据库时,datetime字段值为空默认插入0001年01月01日造成datetime类型溢出. 传给数据库表的时间类型值是null值.这里的null指的是 ...

  10. format()函数用法

    基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数,位置可以不按顺序. 直接打印输出参数: 通过字典设置参数: 通过列表索引设置参数: