PAT1028:List Sorting
1028. List Sorting (25)
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的更多相关文章
- PAT1028. List Sorting (25)---strcmp
题目链接为:https://www.patest.cn/contests/pat-a-practise/1028 1028. List Sorting (25) Excel can sort reco ...
- PAT1028. List Sorting (25)
id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...
- 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 ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
随机推荐
- AWR報告詳解
AWR是Oracle 10g 版本 推出的新特性, 全称叫Automatic Workload Repository-自动负载信息库, AWR 是通过对比两次快照(snapshot)收集到的统计信息 ...
- 目前调试移动设备程序只能通过USB线缆
就像iOS,转移(到设备上)并调试App不可能通过WiFi或蓝牙连接. 一个有线的USB线缆连接现今主要用来调试. 确保你直接将Android设备插入Mac的USB接口,避免使用USB hubs和扩展 ...
- OpenCV特征点提取----Fast特征
1.FAST(featuresfrom accelerated segment test)算法 http://blog.csdn.net/yang_xian521/article/details/74 ...
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
FROM :http://www.sufeinet.com/forum.php?mod=viewthread&tid=1736&extra=page%3D1%26filter%3Dty ...
- 典型分布式系统分析: GFS
本文是典型分布式系统分析系列的第二篇,关注的是GFS,一个分布式文件存储系统.在前面介绍MapReduce的时候也提到,MapReduce的原始输入文件和最终输出都是存放在GFS上的,GFS保证了数据 ...
- linux grep 和 sed使用
http://www.cnblogs.com/zhuyp1015/archive/2012/07/01/2572289.html 听说过sed 和 awk 比较强大,专门学习了一下. 使用这些shel ...
- rhel6.4 安装nodejs和Mysql DB服务
rhel6.4 安装nodejs和Mysql DB服务 安装好redhat6.4虚拟机后, 安装软件: # yum install gcc-c++ openssl-devel Loaded plugi ...
- Testbench(转)
本来还打算自己写下对Testbench的理解,后来发现百度百科名片解释得很好,所以就直接转了. 原文百度百科链接:http://baike.baidu.com/link?url=dxzsOAs32IE ...
- LeetCode(65)-Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- leetCode之旅(5)-博弈论中极为经典的尼姆游戏
题目介绍 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...