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. [翻译][HTML]CELLPADDING and CELLSPACING

    w3school手册:http://www.w3schools.com/tags/att_table_cellspacing.asp 一直以来都发现自己对cellpadding&cellspa ...

  2. [leetcode] 15. Plus One

    这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...

  3. memcached分布式缓存系统

    在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活.此时 ...

  4. 常用kubectl命令总结

    command kubectl kubectl 输出格式 显示Pod的更多信息 kubectl get pod <pod-name> -o wide 以yaml格式显示Pod的详细信息 k ...

  5. DBCC--LOG

    DBCC LOGTo retrieve the transaction log for a given database.对应日志文件较大的数据库,慎用该命令Uasge:DBCC LOG(<db ...

  6. openvswitch 驱动卸载失败(Module openvswitch is in use)

    现象: [root@ostack1 ~]# modprobe -r openvswitchmodprobe: FATAL: Module openvswitch is in use. 解决: [roo ...

  7. Exp6 信息搜集与漏洞扫描 20164323段钊阳

    20164323 Exp6 信息搜集与漏洞扫描 回答问题 1.哪些组织负责DNS,IP的管理. 全球根服务器均由美国政府授权的ICANN统一管理,负责全球的域名根服务器.DNS和IP地址管理.全球一共 ...

  8. rocketmq学习

    官网地址 安装name server和broker git clone https://github.com/apache/incubator-rocketmq.git cd incubator-ro ...

  9. Spring+Ehcache

    这里记录一下Spring+Ehcache的结合使用 1.添加依赖 <dependency> <groupId>org.springframework</groupId&g ...

  10. postgresql模糊匹配正则表达式性能问题

    postgresql 模糊匹配 目前建议使用like,~~,不建议使用正则表达式, 目前有性能问题 https://yq.aliyun.com/articles/405097   正则表达式效率比较低 ...