本题要注意的几点
① 结构体中如果使用string类,那么输入数据时就需要使用cin,会导致最后一个测试点超时
②判断string类变量的大小,可以直接用“<”和“>”;
而判断char类变量的大小,需要用strcmp(a,b);
复制代码
1
2
3
4
5
6
7bool cmp(node a,node b){ if(c==1||(a.name==b.name&&c==2)||(a.sco==b.sco&&c==3)) return strcmp(a.id,b.id)<=0; else if(c==2) return strcmp(a.name,b.name)<=0; else return a.sco<=b.sco; }
复制代码
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#include<iostream> #include<cstdio> #include<string> #include<string.h> #include<algorithm> using namespace std; int n,c; struct node{ char id[10],name[10]; int sco; }p[100005]; bool cmp(node a,node b){ if(c==1||(a.name==b.name&&c==2)||(a.sco==b.sco&&c==3)) return strcmp(a.id,b.id)<=0; else if(c==2) return strcmp(a.name,b.name)<=0; else return a.sco<=b.sco; } int main() { cin>>n>>c; for(int i=0;i<n;i++){ scanf("%s %s %d",&p[i].id,&p[i].name,&p[i].sco); } sort(p,p+n,cmp); for(int i=0;i<n;i++) cout<<p[i].id<<" "<<p[i].name<<" "<<p[i].sco<<endl; return 0; }
最后
以上就是奋斗酸奶最近收集整理的关于1028 List Sorting (25分)的全部内容,更多相关1028内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复