1028. List Sorting (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct student
{
int id;
string name;
int grade;
};
bool cmp1(const student& a,const student& b)
{
return a.id < b.id;
}
bool cmp2(const student& a,const student& b)
{
if(a.name == b.name)
return a.id < b.id;
return a.name.compare(b.name) <= 0;
}
bool cmp3(const student& a,const student& b)
{
if(a.grade == b.grade)
return a.id < b.id;
return a.grade < b.grade;
}
int main()
{
int N,C;
while(cin >> N >> C)
{
vector<student> s(N);
for(int i = 0;i < N;i++)
{
cin >> s[i].id >> s[i].name >> s[i].grade;
}
if(C == 1)
sort(s.begin(),s.end(),cmp1);
else if(C == 2)
sort(s.begin(),s.end(),cmp2);
else
sort(s.begin(),s.end(),cmp3);
for(int i = 0;i < s.size();i++)
printf("%06d %s %d\n",s[i].id,s[i].name.c_str(),s[i].grade);
}
}

PAT1028:List Sorting的更多相关文章

  1. PAT1028. List Sorting (25)---strcmp

    题目链接为:https://www.patest.cn/contests/pat-a-practise/1028 1028. List Sorting (25) Excel can sort reco ...

  2. PAT1028. List Sorting (25)

    id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...

  3. HDU Cow Sorting (树状数组)

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

  4. 1306. Sorting Algorithm 2016 12 30

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

  5. 算法:POJ1007 DNA sorting

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

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

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

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

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

  8. 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 ...

  9. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

随机推荐

  1. android Titlebar一行代码实现沉浸式效果

    github地址 一个简单易用的导航栏TitleBar,可以轻松实现IOS导航栏的各种效果  整个代码全部集中在TitleBar.java中,所有控件都动态生成,动态布局.不需要引用任何资源文件,拷贝 ...

  2. Linux文件系统构成(第二版)

    Linux文件系统构成 /boot目录: 内核文件.系统自举程序文件保存位置,存放了系统当前的内核[一般128M即可] 如:引导文件grub的配置文件等 /etc目录: 系统常用的配置文件,所以备份系 ...

  3. ListView 与ContextMenu的关联管理

    <span style="font-family: Arial, Helvetica, sans-serif;">package com.example.listvie ...

  4. Android TV listView焦点平滑移动

    先上TV上效果图 Mark下思路: package com.test.ui; import java.lang.reflect.Method; import android.annotation.Su ...

  5. Aandroid 图片加载库Glide 实战(一),初始,加载进阶到实践

    原文: http://blog.csdn.net/sk719887916/article/details/39989293 skay 初识Glide 为何使用 Glide? 有经验的 Android ...

  6. javascript、ruby和C性能一瞥(1)

    测试一下本地js.浏览器中的js以及ruby对于类似算法的性能.结果有些意外:浏览器js最快,本地其次当相差很小:ruby最慢而且不是一个数量级的: 因为写的匆忙,可能有重大问题没能看出来,请各位高人 ...

  7. IOS Dev 需要常看的网站<转>

    英文系列 网站 http://Raywenderlich.com 这个不多说了吧,iOS界的百科全书.iOS By tutorial系列书从iOS7到8全买的正版别说499刀了,999刀也入手. ob ...

  8. 运行Applet程序

    [操作方法1:]① 编辑源程序welcome.java.② 编译程序 javac welcome.java③ 将Applet嵌入HTML网页.方法是,用记事本创建一个文件,文件内容如下:<app ...

  9. spring3.1文档目录翻译

    整理google共享磁盘找到了2014年翻译的spring官方文档的目录,分享出来可能会对英语不好的同学有些帮助吧. spring3.1官方文档目录-中文 spring3.1官方文档-英文 关于作者

  10. 使用mpvue开发微信小程序

    更多内容请查看 我的新博客 地址 : 前言 16年小程序刚出来的时候,就准备花点时间去学学.无奈现实中手上项目太多,一个接着一个,而且也没有开发小程序的需求,所以就一拖再拖. 直到上周,终于有一个小程 ...