PTA (Advanced Level) 1028 List Sorting
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的更多相关文章
- PAT (Advanced Level) 1028. List Sorting (25)
时间卡的比较死,用string会超时. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- 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 ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 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 ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 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 ...
随机推荐
- Redis之序列化POJO
redis存储方式有很多种,但是我个人觉得最好用的并非是String存储类型,而是Hash存储类型,如果在使用redis的时候单纯的只使用到String存储类型的话,我个人觉得完全体现不了redis的 ...
- .Net core,EFCore 入门
我在百度上搜了一下.net core和efcore 入门案例.好多博客都是大概说了一下做法,对于小白而言还是一头雾水,我今天就抽出一点时间,写一个详细的入门小案例,就一张表没有什么业务可言.主要是操 ...
- iOS 多Target, Other link Flag
在创建多个马甲包或者多个App间只有很小的差异是使用多Target是一种很好的方法 https://www.jianshu.com/p/18db54655246 1:选中原始的Target, 点击右键 ...
- JavaScript基础DOM介绍和常用操作(5)
day53 参考:https://www.cnblogs.com/liwenzhou/p/8011504.html JavaScript引入方式 location对象 window.location ...
- [JavaScript] 获取昨日前天的日期
var day = new Date(); day.setDate(day.getDate()-1); console(day.pattern('yyyy-MM-dd'));//昨天的日期 day.s ...
- DOM操作复选框
DOM操作复选框 实现一个效果如下图: 点击"全选/全不选"复选框之后,足球.篮球.游泳和唱歌均被选取.取消"全选/全不选"复选框之后,被选中的所有都变成未选中 ...
- eclispe中使用python库 pyswip 进行prolog编程
from pyswip import Prolog prolog = Prolog() prolog.assertz("father(michael,john)") prolog. ...
- mysql优化器在统计全表扫描的代价时的方法
innodb 的聚集索引 的叶子结点 存放的 是 索引值以及数据页的偏移量 那么在计算全表扫描的代价是怎么计算的呢? 我们知道代价 为 cpu代价+io代价 cpu代价 就是 每5条记录比对 计算一个 ...
- 多线程学习:Volatile与Synchronized的区别、什么是重排序
java线程的内存模型 java的线程内存模型中定义了每个线程都有一份自己的共享变量副本(本地内存),里面存放自己私有的数据,其他线程不能直接访问,而一些共享变量则存在主内存中,供所有线程访问. 上图 ...
- django 的ajax 请求,使用form的验证机制。
所有的form都需要在后台验证,前台验证是不可靠的,django的验证是后台验证,前台提示错误信息. js验证是在前台的,无需发送消息给后台,但安全性不可靠,强调的是用户体验. 要求,使用弹出框,弹出 ...