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. 使用vs code开发纸壳CMS并启用Razor智能提示

    关于纸壳CMS 纸壳CMS是一个开源免费的,可视化设计,在线编辑的内容管理系统.基于ASP .Net Core开发,插件式设计: 下载代码 GitHub:https://github.com/Seri ...

  2. C#操作Xml树的扩展类

    本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...

  3. UWP开发入门(九)——简单界面的布局技巧及屏幕适应

    嘿嘿嘿,题目比较绕哈.本篇主要讨论一般情况下,页面的布局技巧,怎么将元素的展现尽量做到分辨率无关.基本的思路仍然是尽量少的标定具体的数字,而是用比列来标注各元素占据的空间. 这里我打算用易信的名片页来 ...

  4. RabbitMQ广播模式

    广播模式:1对多,produce发送一则消息多个consumer同时收到.注意:广播是实时的,produce只负责发出去,不会管对端是否收到,若发送的时刻没有对端接收,那消息就没了,因此在广播模式下设 ...

  5. JavaScript中的类数组对象

    在javascript中,对象与数组都是这门语言的原生规范中的基本数据类型,处于并列的位置. 一般来说,如果我们有一个对象obj和一个数组a: obj["attr1"];    / ...

  6. Tomcat 服务器开启失败故障

    Server Tomcat v8.0 Server at localhost failed to start.故障 原因:1.可能是web.xml中的filter-mapping中url-patter ...

  7. 纯文本-FileInputStream的编码与解码方式

    前言:以下分析只针对纯文本 1.FileInputStream默认的编码方式就是文件的编码方式 即:源文件是什么编码方式,则利用FileInputStream默认读取的字节数组,就是什么编码方式. 例 ...

  8. iOS - 安全

    1. CheckList http://www.jianshu.com/p/d3cc2d5c177d a 数据安全.分为数据传输安全和数据存储安全 数据存储安全为保存在App中的数据安全.不允许明文存 ...

  9. [bug]微信小程序使用 <scroll-view> 和 box-shadow 引起页面抖动

    背景 为了实现点点点动态loading效果,并且方便使用(只需要给一个空元素加一个.loading),有如下代码: .loader { background-color: #fff; font-siz ...

  10. JAVA并发编程学习笔记------结构化并发应用程序

    1. Executor基于生产者-消费者模式,提交任务的操作相当于生产者,执行任务的线程相当于消费者,如果要在程序中实现一个生产者-消费者的设计,最简单的方式通常就是使用Executor 2. Exe ...