我是靠谱客的博主 沉默母鸡,这篇文章主要介绍PAT甲级 1028. List Sorting (25),现在分享给大家,希望可以做个参考。

题目链接:https://www.patest.cn/contests/pat-a-practise/1028


题目大意:根据输入要求将所给学生按照id、name、grade排序,按姓名和分数排序时若出现相同的情况,则将相同的按照id升序排列。


解题思路:使用sort()函数,自定义比较方式cmp1


代码如下:

复制代码
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
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int N,C; struct S { int no; char name[10]; int grade; }; bool cmp1(S a,S b) { if(C==1) return a.no < b.no; else if(C==2){ if(strcmp(a.name,b.name)==0) return a.no<b.no; else return strcmp(a.name,b.name)<0; } else{ if(a.grade==b.grade) return a.no<b.no; else return a.grade<b.grade; } } int main(){ cin>>N>>C; S st[100001]; for(int i=0;i<N;i++) scanf("%06d %s %d",&st[i].no,&st[i].name,&st[i].grade); sort(st,st+N,cmp1); for(int i=0;i<N;i++) printf("%06d %s %dn",st[i].no,st[i].name,st[i].grade); return 0; }

最后

以上就是沉默母鸡最近收集整理的关于PAT甲级 1028. List Sorting (25)的全部内容,更多相关PAT甲级内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部