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. C语言----数据类型(基础篇一)

    C语言的入门程序模板 #include <stdio.h> /*使用或者包含系统里面的程序*/ main() /*程序入口点*/ { /*起点*/ +; /*叫计算机执行的指令*/ } / ...

  2. LVM基础详细说明及动态扩容lvm逻辑卷的操作记录

    LVM概念:---------------------------------------------------------------------------------------------- ...

  3. js怎么将 base64转换成图片

    //获取数组最后一个元素 let hasFiles = files[Object.keys(files).pop()] // 参考上面的图片 let file = hasFiles.url let n ...

  4. 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花

    求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...

  5. Alpha冲刺随笔集

    作业地址 项目名称:高校实验室信息化管理系统 团队成员 学号 姓名 031602636 许舒玲 031602237 吴杰婷 031602220 雷博浩 031602634 吴志鸿 181600107 ...

  6. Centos7搭建LAMP+Typecho博客

    一.安装Apache的httpd服务 yum install httpd # 安装httpd服务 systemctl start httpd # 启动httpd服务 systemctl status ...

  7. Jquery 组 checkbox双向控制与tr变色

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  8. FICO基础知识(一)

    GL – 总账 (General Ledger) 总帐核算的中心任务是提供外部会计及其所涉及帐户的概貌. 总账会计主要用途:根据不同的会计准则(如欧洲的 IAS, 美国的GAAP, 中国国家会计准则) ...

  9. linux 单引号,双引号,反引号的小总结。

    还是老惯例说说事情的起因,由于最开始对linux下面的各种引号并不是特别敏感,导致有一天我在添加数据库字段的时候出现的错误,当时出现错误的原因是我在最外层使用了单引号进行包裹,然后一句话里面需要转意的 ...

  10. 使用 jstack 查询线程死锁错误日志 定位问题

    定位问题 (1) 首先 找到相应的进程 使用 ps -ef | grep 'com.sankuai.qcs.regulation.dispatch' 找到进程的ID;==>21980 (2) t ...