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

思路:

  模拟,注意采用cin输入的话最后一组数据会被卡到。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 struct Stu {
6 int id;
7 char name[10];
8 int grade;
9 } students[100005];
10
11 bool cmp1(Stu a, Stu b) { return a.id < b.id; }
12 bool cmp2(Stu a, Stu b) { return strcmp(a.name, b.name) <= 0; }
13 bool cmp3(Stu a, Stu b) { return a.grade <= b.grade; }
14
15 int main() {
16 int n, c;
17 scanf("%d%d", &n, &c);
18 for (int i = 0; i < n; ++i)
19 scanf("%d%s%d", &students[i].id, students[i].name, &students[i].grade);
20 if (c == 1)
21 sort(students, students + n, cmp1);
22 else if (c == 2)
23 sort(students, students + n, cmp2);
24 else
25 sort(students, students + n, cmp3);
26 for (int i = 0; i < n; ++i)
27 cout << setw(6) << setfill('0') << students[i].id << " "
28 << students[i].name << " " << students[i].grade << endl;
29 return 0;
30 }

1028 List Sorting的更多相关文章

  1. PAT 1028 List Sorting[排序][一般]

    1028 List Sorting (25)(25 分) Excel can sort records according to any column. Now you are supposed to ...

  2. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  3. 【PAT】1028. List Sorting (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...

  4. PTA (Advanced Level) 1028 List Sorting

    List Sorting Excel can sort records according to any column. Now you are supposed to imitate this fu ...

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

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

  6. PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)

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

  7. 1028 List Sorting (25 分)

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

  8. PAT 1028 List Sorting (25分) 用char[],不要用string

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

  9. 1028. List Sorting (25)

    #include <vector> #include <stdio.h> #include <string.h> #include <algorithm> ...

随机推荐

  1. MySQL注入 利用系统读、写文件

    目录 能读写文件的前提 Windows下的设置 Linux下的设置 没有读写权限的尝试 有SQL注入点,确认是否有读写权限 read load_file() load data infile() wr ...

  2. Android Studio|IntelliJ IDEA Git使用小技巧

    一 分支管理 1. 新建分支 在master的基础上创建新分支dev 2. 推送分支 将新建的分支dev推送到远程 3. 切换分支 4. 合并分支 当我们在dev分支完成代码修改并测试通过后 需要将d ...

  3. 9.Vue之webpack打包基础---模块化思维

    主要内容: 1. 什么是模块化思维? 2.  ES6包的封装思想 一.什么是模块化思维呢? 现实工作中, 一个项目可能会有多个人同时开发. 然后, 将所有人开发的内容, 合并到一个文件中. 比如: 1 ...

  4. HDOJ-1029(简单dp或者排序)

    Ignatius and the Princess IV hdoj-1029 这里主要是先排序,因为要找出现了一半以上的数字,所以出现的数字一定在中间 方法一: #include<iostrea ...

  5. 每日一题20201112(922. 按奇偶排序数组 II)

    题目链接: 922. 按奇偶排序数组 II 思路 很简单,搞懂问题的核心就行,假设现在有奇数在偶数位上,偶数在奇数位上. 那么我们要做的就是,找到分别在对方位置上的数字,然后交换他们就行. class ...

  6. External Libraries中没有Maven的jar包的原因(已解决)

    **深坑!** ## External Libraries中没有Maven的jar包的原因(已解决) 2021年3月1日 --- 搭建一个新项目 IDEA 从 Git 上拉 拉去Maven项目然后 m ...

  7. git的回滚与撤销【reset and revert】

    git的工作流程-- 3个区域 工作区:我们可以看到的文件内容 在操作 git add 之前的!! 缓存区:是不可见的  已经git add操作,还没git commit -m "" ...

  8. 从零开始编写一个BitTorrent下载器

    从零开始编写一个BitTorrent下载器 BT协议 简介 BT协议Bit Torrent(BT)是一种通信协议,又是一种应用程序,广泛用于对等网络通信(P2P).曾经风靡一时,由于它引起了巨大的流量 ...

  9. Nuxt.js vue服务端渲染

    一.为什么要用Nuxt.js 原因其实不用多说,就是利用Nuxt.js的服务端渲染能力来解决Vue项目的SEO问题. 二.Nuxt.js和纯Vue项目的简单对比 1. build后目标产物不同 vue ...

  10. 从零学脚手架(六)---production和development拆分

    development.production拆分 根据文件拆分 webpack打包时分为开发模式(development)和发布模式(production),在前面使用命令参数做了简单区分. 但这种方 ...