A1028. List Sorting
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的更多相关文章
- PAT A1028 List Sorting (25 分)——排序,字符串输出用printf
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT甲级——A1028 List Sorting
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT_A1028#List Sorting
Source: PAT A1028 List Sorting (25 分) Description: Excel can sort records according to any column. N ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 算法:POJ1007 DNA sorting
这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...
- U3D sorting layer, sort order, order in layer, layer深入辨析
1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...
- WebGrid with filtering, paging and sorting 【转】
WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...
- 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 ...
随机推荐
- FFMPEG指令
FFmpeg是一个用于音视频处理的自由软件,被广泛用于音视频开发.FFmpeg功能强大,本文主要介绍如何使用FFmpeg命令行工具进行简单的视频处理. 安装FFmpeg可以在官网下载各平台软件包或者静 ...
- Jmeter(三十五)_精确实现网页爬虫
Jmeter实现了一个网站文章的爬虫,可以把所有文章分类保存到本地文件中,并以文章标题命名 它原理就是对网页提交一个请求,然后把返回的所有值提取出来,利用ForEach控制器去实现遍历.下面来介绍一下 ...
- 从Stampery到Chronicled,区块链公证业务的实践
Stampery就是这样一家利用比特币区块链技术代替公证人的创业公司,能为所有的敏感文件提供具有法律约束力的证明.可以用Stampery证明任何文件,它能很好地保护知识产权,证明遗嘱.宣誓.合同.家庭 ...
- 《坦克世界》1.0+:使用 CPU 优化的图形和物理丰富用户体验
本文以<坦克世界>为例,介绍 Wargaming 使用 CPU 多核和 CPU 单指令多数据 (SIMD) 功能显著提升游戏沉浸式体验的创新方法.我们以英特尔® 线程构建模块(英特尔® T ...
- maven 第一个Web项目——HelloWorld
1.安装Maven,具体步骤,参照博客[maven的安装与配置]http://www.cnblogs.com/dyh004/p/8523260.html 2.配置阿里云为Maven中央仓库,具体步骤, ...
- 在Java中执行Tomcat中startup.bat
问题:更改数据库时,需要重启Tomcat服务器,才能把更改后的数据加载到项目中.于是想每次更改数据库时,都调用Java方法,重启Tomcat 代码: Process process = Runtime ...
- shell脚本--权限分配
因为shell脚本内部是很多命令的集合,这些命令也许会涉及到操作某一个文件,而且shell脚本的运行,也是需要当前用户对脚本具有运行的权限,否则,会因为权限不够而失败. 首先最重要的一点:修改权限,只 ...
- 命令行批量修改IP并ping测试
@echo off set ip=0 :beginset /a ip=%ip%+1netsh interface ip set address "本地连接" static 172. ...
- PHP 4种输出的方式
<?php //测试用的数组 $info = array('11'=>'aaa', '22'=>'bbb', '33'=>'ccc'); //第一种,将整个数组作为一个对象输出 ...
- target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件
target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件