我是靠谱客的博主 开心月亮,这篇文章主要介绍1028 List Sorting(排序),现在分享给大家,希望可以做个参考。

这类题目归结于常用技巧与算法,有很鲜明的套路,重在理解其规则,通常写起来不算太复杂。

题目描述:
在这里插入图片描述
在这里插入图片描述
题目大致意思:
给出N个学生的学号,姓名和成绩,按其中的某个列,对其进行排序,在按姓名或者分数进行排列时,如果遇到了相同的数据,则按学号递增进行排序。
大致思路:
这道题相比前两道来说要简单很多,但核心思想是一样的。使用结构体来存储学生的学号,姓名和分数信息,进而用一个结构体数组来存储所有学生的信息。根据条件使用sort函数对结构体数组进行排序即可。
提交结果:
第七个测试用例差点就超过了时间限制。
在这里插入图片描述
提交代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; struct Student { string id; string name; int score; }; bool cmp1(Student stu1, Student stu2) { return stu1.id < stu2.id; } bool cmp2(Student stu1, Student stu2) { if (stu1.name != stu2.name) return stu1.name < stu2.name; else { return stu1.id < stu2.id; } } bool cmp3(Student stu1, Student stu2) { if (stu1.score != stu2.score) return stu1.score < stu2.score; else { return stu1.id < stu2.id; } } vector<Student> arr; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { Student stu; cin >> stu.id >> stu.name >> stu.score; arr.push_back(stu); } if (m == 1) { sort(arr.begin(), arr.end(), cmp1); for (int i = 0; i < n; i++) { cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl; } } if (m == 2) { sort(arr.begin(), arr.end(), cmp2); for (int i = 0; i < n; i++) { cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl; } } if (m == 3) { sort(arr.begin(), arr.end(), cmp3); for (int i = 0; i < n; i++) { cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl; } } }

本次提交后累计得分523,排名为14290。

最后

以上就是开心月亮最近收集整理的关于1028 List Sorting(排序)的全部内容,更多相关1028内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部