1028 List Sorting (25分)

断了今生、忘了曾经 提交于 2020-03-24 16:55:07

 

这题不难,貌似乙级也有一样的题?简单的排序问题。有一点问题是,用cin,cout绝对超时,查了一下,用一下代码可以加速至与scanf,printf速度一样。

ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

好像确实可以加速,但是否能通过还是随缘,pat上最后一个测试点不是固定的,所以一下用c++写的代码有时候不超时有时候超时(以下代码在最后一个测试点最好情况下用时390ms,题目限制400ms,好严格)...主要是c++封装好的string的比较太方便了,c里面又没有这个东西。

超时与否看运气的代码:

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std; 
 7 struct student{
 8     string id,name;
 9     int grade;
10 };
11 bool cmp1(student a,student b){
12     return a.id<b.id;
13 }
14 bool cmp2(student a,student b){
15     if(a.name!=b.name)
16       return a.name<b.name;
17     else
18       return a.id<b.id;
19 }
20 bool cmp3(student a,student b){
21     if(a.grade!=b.grade)
22       return a.grade<b.grade;
23     else
24       return a.id<b.id;
25 }
26 int main()
27 {  ios::sync_with_stdio(false);
28    cin.tie(0);
29    cout.tie(0);
30    int n,c;
31    int grade;
32    string id,name;
33    cin>>n>>c;
34    vector<student> stu(n);
35    for(int i=0;i<n;i++){
36         cin>>stu[i].id>>stu[i].name>>stu[i].grade;
37    }
38    if(c==1)
39      sort(stu.begin(),stu.end(),cmp1);
40    else if(c==2)
41      sort(stu.begin(),stu.end(),cmp2);
42     else
43      sort(stu.begin(),stu.end(),cmp3); 
44     for(int i=0;i<n;i++){
45     
46         cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
47     }
48   return 0;
49 }
View Code

参考了柳神的代码,用c写了一遍,最后一个测试点只用了100多毫秒。c还是更快啊...其实c里面的<cstring>有strcmp(),用起来挺方便的。用c写需要注意,定义字符数组大小要比所接受的字符个数要大一点,比如id有6位,字符数组大小至少定为7,为空字符\0留下空间。同时,注意sort()中的比较函数只返回0,1。strcmp(a,b)有1,-1,0三种返回值。当a<b返回-1,a>b返回1,a=b返回0。升序排序时,比较函数return strcmp(a,b)<0。理解为按照" strcmp(a,b)<0"的方式排序,即按照"a<b"的方式比排序,就是升序。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std; 
 7 struct student{
 8     char id[7],name[10];
 9     int grade;
10 };
11 bool cmp1(student a,student b){
12     return strcmp(a.id,b.id)<0;
13 }
14 bool cmp2(student a,student b){
15     if(strcmp(a.name,b.name)!=0)
16       return strcmp(a.name,b.name)<0;
17     else
18       return strcmp(a.id,b.id)<0;
19 }
20 bool cmp3(student a,student b){
21     if(a.grade!=b.grade)
22       return a.grade<b.grade;
23     else
24       return strcmp(a.id,b.id)<0;
25 }
26 int main()
27 {  
28    int n,c;
29    int grade;
30    char id[7],name[10];
31    cin>>n>>c;
32    vector<student> stu(n);
33    for(int i=0;i<n;i++){
34          scanf("%s %s %d",&stu[i].id,&stu[i].name,&stu[i].grade);
35    }
36    if(c==1)
37      sort(stu.begin(),stu.end(),cmp1);
38    else if(c==2)
39      sort(stu.begin(),stu.end(),cmp2);
40     else
41      sort(stu.begin(),stu.end(),cmp3); 
42     for(int i=0;i<n;i++){
43         printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
44     }
45   return 0;
46 }

参考链接:https://www.liuchuo.net/archives/2120

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!