1028 List Sorting (25 分)
 

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

思路:

简单,三种排序可能,cmp写一下

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
string num;
string name;
int score;
}a[];
bool cmp1(node x,node y){
return x.num<y.num;
};
bool cmp2(node x,node y){
if(x.name==y.name){
return x.num<y.num;
}
else return x.name<y.name;
};
bool cmp3(node x,node y){
if(x.score==y.score){
return x.num<y.num;
}
else return x.score<y.score;
};
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>a[i].num>>a[i].name>>a[i].score;
}
if(m==){
sort(a+,a++n,cmp1);
}else if(m==){
sort(a+,a++n,cmp2);
}else{
sort(a+,a++n,cmp3);
}
for(int i=;i<=n;i++){
cout<<a[i].num<<" "<<a[i].name<<" "<<a[i].score<<endl;
}
return ;
}

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)的更多相关文章

  1. PAT 甲级 1028. List Sorting (25) 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...

  2. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  3. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  4. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  5. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  6. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  7. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  8. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  9. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

随机推荐

  1. 3.Vue过滤器

    1.概念: Vue.js 允许你自定义过滤器,可被用作一些常见文本的格式化,过滤器可以用在两个地方:mustache 插值和 v-bind 表达式.过滤器应该被添加在 JavaScript 表达式的尾 ...

  2. Canal的简单使用(监控数据库数据的变化)

    原文:https://www.cnblogs.com/java-spring/p/8930740.html canal可以用来监控数据库数据的变化,从而获得新增数据,或者修改的数据,用于实际工作中,比 ...

  3. 图像处理---视频<->图片

    图像处理---视频<->图片 // 该程序实现视频和图片的相互转换. // Image_to_video()函数将一组图片合成AVI视频文件. // Video_to_image()函数将 ...

  4. 1118 DOM

    目录 BOM与DOM window对象 navigator对象(了解即可) screen对象(了解即可) history对象(了解即可) location对象 弹出框 计时相关 DOM对象 DOM结构 ...

  5. Python 字节码bytecode

    字节码bytecode python把源码文件编译成字节码文件,存放在__pycahe子目录内,用.pyc结尾.之后如果不再修改源码文件,运行时则使用*.pyc文件编译成机器码,这样不但运行速度快,而 ...

  6. JS 自定义组件

    经常会用到JS插件,但从未研究过插件的写法 目前主流的写法有多种,各有各的优缺点,下面,我就以最常规的一种写法举例 // plugin.js ;(function(undefined) {//防止出现 ...

  7. C语言实现的文件交互

    计算机与外部设备的交互依靠文件完成 文件是记录在外部介质上的数据的集合:例如1.c 是源码 1.exe可执行的文件 文件的分类 按组织结构: 记录文件:有一定结构的文件,可以解析成字段值的文件: 流式 ...

  8. Luogu5369 [PKUSC2018]最大前缀和

    题目链接:洛谷 题目大意:给定一个长为$n$的整数序列,求全排列的最大前缀和(必须包含第一个数)之和. 数据范围:$1\leq n\leq 20,1\leq \sum_{i=1}^n|a_i|\leq ...

  9. (转)实验文档2:实战交付一套dubbo微服务到kubernetes集群

    基础架构 主机名 角色 ip HDSS7-11.host.com k8s代理节点1,zk1 10.4.7.11 HDSS7-12.host.com k8s代理节点2,zk2 10.4.7.12 HDS ...

  10. P1582 倒水,P2158 [SDOI2008]仪仗队——数学,二进制

    有n个瓶子,里面都有一升水,但是只想保留k个瓶子,只能两个瓶子里面的水体积相等时才能倒在一个瓶子里:不能丢弃有水的瓶子:瓶子容量无限: 问需要购买几个额外的瓶子才能满足条件: 因为每个瓶子一开始只有一 ...