当前位置:网站首页>1028 List Sorting
1028 List Sorting
2022-06-27 19:50:00 【Brosto_ Cloud】
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 (≤105) 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 9
Sample Output 3:
000002 James 9
000001 Zoe 60
000007 James 85
000010 Amy 90#include <iostream>
#include <algorithm>
using namespace std;
struct Stu {
string id;
string name;
int grade;
} a[100010];
bool cmp1(Stu s1, Stu s2) {
return s1.id < s2.id;
}
bool cmp2(Stu s1, Stu s2) {
return s1.name == s2.name ? s1.id < s2.id : s1.name < s2.name;
}
bool cmp3(Stu s1, Stu s2) {
return s1.grade == s2.grade ? s1.id < s2.id : s1.grade < s2.grade;
}
int main() {
int n, c;
cin >> n >> c;
for (int i = 0; i < n; i++) {
cin >> a[i].id >> a[i].name >> a[i].grade;
}
if (c == 1) {
sort(a, a + n, cmp1);
} else if (c == 2) {
sort(a, a + n, cmp2);
} else if (c == 3) {
sort(a, a + n, cmp3);
}
for (int i = 0; i < n; i++) {
cout << a[i].id << ' ' << a[i].name << ' ' << a[i].grade << endl;
}
return 0;
}
边栏推荐
猜你喜欢

海底电缆探测技术总结

数组练习 后续补充

The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder

新中大冲刺科创板:年营收2.84亿 拟募资5.57亿

C# 二维码生成、识别,去除白边、任意颜色

A simple calculation method of vanishing point

《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践

binder hwbinder vndbinder

工作流自动化 低代码是关键

Mathematical derivation from perceptron to feedforward neural network
随机推荐
券商经理的开户二维码开户买股票安全吗?有谁知道啊
shell脚本常用命令(三)
Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
基于STM32F103ZET6库函数跑马灯实验
External interrupt experiment based on stm32f103zet6 library function
Array exercises follow up
1024 Palindromic Number
华大单片机KEIL报错_WEAK的解决方案
One week technical update express of substrate and Boca 20220425 - 20220501
Comment encapsuler un appel à une bibliothèque
PCB线路板蛇形布线要注意哪些问题?
Cucumber自动化测试框架使用
惊呆!原来 markdown 的画图功能如此强大!
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
一种朴素的消失点计算方法
网络上开户买股票是否安全呢?刚接触股票,不懂求指导
Memoirs of actual combat: breaking the border from webshell
1029 Median
基础数据类型和复杂数据类型