1028 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
思路:
模拟,注意采用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的更多相关文章
- PAT 1028 List Sorting[排序][一般]
1028 List Sorting (25)(25 分) Excel can sort records according to any column. Now you are supposed to ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- 【PAT】1028. List Sorting (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...
- PTA (Advanced Level) 1028 List Sorting
List Sorting Excel can sort records according to any column. Now you are supposed to imitate this fu ...
- PAT 甲级 1028. List Sorting (25) 【结构体排序】
题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...
- 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 ...
- 1028 List Sorting (25 分)
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT 1028 List Sorting (25分) 用char[],不要用string
题目 Excel can sort records according to any column. Now you are supposed to imitate this function. In ...
- 1028. List Sorting (25)
#include <vector> #include <stdio.h> #include <string.h> #include <algorithm> ...
随机推荐
- js 浮点运算bug
js几个浮点运算的bug,比如6.9-1.1,7*0.8,2.1/0.3,2.2+2.1 实现思路 通过将浮点数放大倍数到整型(最后再除以相应倍数),再进行运算操作,这样就能得到正确的结果了 比如:1 ...
- Shell脚本控制docker容器启动顺序
1.遇到的问题 在分布式项目部署的过程中,经常要求服务器重启之后,应用(包括数据库)能够自动恢复使用.虽然使用docker update --restart=always containerid能够让 ...
- Redis单机数据库的实现原理
本文主要介绍Redis的数据库结构,Redis两种持久化的原理:RDB持久化.AOF持久化,以及Redis事件分类及执行原理.最后,分别介绍了单机班Redid客户端和Redis服务器的使用和实现原理. ...
- 学习版pytest内核测试平台开发万字长文入门篇
前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...
- 自己挖的坑自己填--Mybatis mapper文件if标签中number类型及String类型的坑
1.现象描述 (1)使用 Mybatis 在进行数据更新时,大部分时候update语句都需要通过动态SQL进行拼接.在其中,if标签中经常会有 xxx !='' 这种判断,若 number 类型的字段 ...
- python基础学习之类的继承、魔法方法
什么是继承 即类A可以使用类B的方法,即B是A的父类,A是B的子类,AB之间是继承关系 class Father(): # 父类 def __init__(self,name,age): self. ...
- python-实现链式栈
7 """ 8 用一个类来实现一个节点 9 """ 10 class Node(object): 11 def __init__(self, ...
- 第四单元总结&期末总结
OO第四单元总结&期末总结 一.第四单元总结 第一次作业 在第四单元的作业中,我的架构是逐步演进的.设计第一次作业的架构时并没有考虑到后续作业会增加新的图,所以直接把类图的实现放在UmlInt ...
- 仅仅使用Google就完成了人生第一次破解
2021年2月6日21:17:09 begin 起因 在异乡的打工人,不善言谈,幸有一老同学,周末常邀吃饭,感恩之心铭记于心.她结婚时,为表心意欲做视频,视频需要制作字幕,搜索之,偶遇一字幕软件,但是 ...
- LamPiao靶机work_through
前言 oscp靶机系列的第二篇.只追求做出来的话,这靶机蛮简单的.但是为了提升难度,尽量避免使用msf--毕竟考试只准用一次嘛,自己写了个exp. 正文 主机发现 nmap -sP 192.168.2 ...