List Sorting

  Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:

  Each input file contains one test case. For each case, the first line contains two integers N (≤) 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 Specification:

  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

题目解析
  本题给出两个整数,n为学生数量,c为排序指令,之后n行为学生信息,每个学生的信息包括学生号码ID,学生姓名name与学生成绩grade,排序指令为1时要求按照id升序排序,指令为2时要求按照学生姓名name字典序升序排序,若两个学生姓名字典序相同则按id升序排序,指令为3时按成绩grade升序排序,成绩相同时按id升序排序。

  只需要将所有学生信息记录在一个容器vector中,根据题意对应的cmp函数,根据指令调用cmp函数即可。

  AC代码

 #include <bits/stdc++.h>
using namespace std;
struct student{ //结构体student代表一个学生的信息
int id; //学号
string name; //姓名
int grade; //成绩
};
vector<student> V; //容器V记录所有学生信息
bool cmp1(student a, student b){
//指令为1时按学号升序排序
return a.id < b.id;
}
bool cmp2(student a, student b){
//指令为2时按姓名字典序升序排序
if(a.name != b.name)
return a.name < b.name;
else
//姓名字典序相同时按照学号升序排序
return a.id < b.id;
}
bool cmp3(student a, student b){
//指令为3时按照成绩升序排序
if(a.grade != b.grade)
return a.grade < b.grade;
else
//成绩相同时按照学号升序排序
return a.id < b.id;
}
int n, c;
int main()
{
scanf("%d%d", &n, &c); //输入学生数量与排序指令
student temp;
for(int i = ; i < n; i++){
cin >> temp.id >> temp.name >> temp.grade;
//输入学生信息加入容器V
V.push_back(temp);
}
//根据排序指令进行排序
if(c == )
sort(V.begin(), V.end(), cmp1);
else if(c == )
sort(V.begin(), V.end(), cmp2);
else
sort(V.begin(), V.end(), cmp3);
//输出时使用cout会超时
for(auto i : V){
printf("%06d %s %2d\n", i.id, (i.name).c_str(), i.grade);
//格式化输出
}
return ;
}

PTA (Advanced Level) 1028 List Sorting的更多相关文章

  1. PAT (Advanced Level) 1028. List Sorting (25)

    时间卡的比较死,用string会超时. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  2. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  5. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  6. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  7. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. PTA (Advanced Level) 1006 Sign In and Sign Out

    Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...

随机推荐

  1. Git小技巧:VIM中如何填写注释信息

    使用Git命令行工具的时候,经常一不小心就进入VIM界面,例如git commit没有填写任何描述信息.对于习惯了Windows可视化操作界面的用户,可能一下子会觉得无所适从,只能在键盘上一顿短按.下 ...

  2. C#数组的定义,不定长的数组?

    首先,在这里我要说明的是,C#中,定义了数组,那么就必须为其指定长度,且他的长度确定,不能够更改.一旦定义一个数组,那么操作系统就在内存中给这个数组指定了一块内存,他是不支持动态分配存储空间的.能够动 ...

  3. 终结篇:RemoteWebDriver与Grid简介-----Selenium快速入门(十五)

    Selenium的基本使用,已经介绍得差不多了,今天来简单说说RemoteWebDriver与Grid,也是本系列的最后一篇. 还记得本系列第一章(Selenium简介与环境搭配)的配置中,提到我们下 ...

  4. js获取select下拉框的value值和text文本值

    介绍一种取下拉框值以及绑定下拉框数据的方法    这里用到的jquery-ui-multiselect插件 1.前台html代码 <span class="ModuleFormFiel ...

  5. .net下使用最小堆实现TopN算法

    测试代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  6. 如何优化代码中大量的if/else,switch/case?

    前言 随着项目的迭代,代码中存在的分支判断可能会越来越多,当里面涉及到的逻辑比较复杂或者分支数量实在是多的难以维护的时候,我们就要考虑下,有办法能让这些代码变得更优雅吗? 正文 使用枚举 这里我们简单 ...

  7. netty网络通信中的tcp拆包问题

    工作中的一个项目,我们的一个应用与银行系统进行tcp通信的时候,银行下送的报文有时会分多次返回.在tcp中这种数据包分多次小数据包发送的情况成为拆包问题. 其中一个,也是最常见的思路就是在报文的报文头 ...

  8. Greedy- 621. Task Scheduler

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  9. udid iphone6 获取

    http://www.udidregistration.org/how-to-find-udid-of-iphone-6.html

  10. win7下oracle的安装

    1.参考地址1:http://www.cnblogs.com/libiao/archive/2008/08/24/1275000.html 2.参考地址2:http://www.server110.c ...