目录
1.List Sorting
2.The World's Richest
3.Pat Judge
我是用了一个cmp写的比较复杂,书里写了三个cmp,我感觉我这个因为判断得多,可能后面会超时。
#include <cstdio> #include <cctype> #include <cstring> #include <math.h> #include <algorithm> using namespace std; struct student{ int id; char name[10]; int grade; }stu[100002]; int c; bool cmp(student a,student b){ if(c==1){ return a.id<b.id; }else if(c==2){ if(strcmp(a.name,b.name)!=0){ return strcmp(a.name,b.name)<0; }else{ return a.id<b.id; } }else{ if(a.grade!=b.grade){ return a.grade<b.grade; }else{ return a.id<b.id; } } } int main(){ int n; scanf("%d %d\n",&n,&c); for(int i=0;i<n;i++){ scanf("%d %s %d",&stu[i].id,&stu[i].name,&stu[i].grade); } sort(stu,stu+n,cmp); for(int i=0;i<n;i++){ printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].grade); } return 0; }
这道题目的核心是先按财富排序,我一开始弄错了,搞成了按年龄排序。
#include <cstdio> #include <cctype> #include <cstring> #include <math.h> #include <algorithm> using namespace std; struct person{ char name[10]; int age; int worth; }per[100002],valid[100002]; int age[100002]; bool cmp(person a,person b){ if(a.worth!=b.worth){ return a.worth>b.worth; }else if(a.age!=b.age){ return a.age<b.age; }else{ return strcmp(a.name,b.name)<0; } } int main(){ int n,k; scanf("%d %d",&n,&k); for(int i=0;i<n;i++){ scanf("%s %d %d",&per[i].name,&per[i].age,&per[i].worth); } sort(per,per+n,cmp); int num,min,max; int validnum=0; for(int i=0;i<n;i++){ if(age[per[i].age]<100){ age[per[i].age]++; valid[validnum++] = per[i]; } } for(int i=1;i<=k;i++){ scanf("%d %d %d",&num,&min,&max); int k=0; printf("Case #%d:\n",i); for(int j=0;j<validnum&&k<num;j++){ if(valid[j].age>=min&&valid[j].age<=max){ printf("%s %d %d\n",valid[j].name,valid[j].age,valid[j].worth); k++; } } if(k==0){ printf("None\n"); } } return 0; }
这道题目主要是输入时要把各种情况考虑清楚,后面也就是排序,排名,输出。
虽然不难,但真的复杂啊。
#include <cstdio> #include <cctype> #include <cstring> #include <math.h> #include <algorithm> using namespace std; int score[7]; int k; struct student{ int id; int get_score[7]={-1,-1,-1,-1,-1,-1,-1}; int sum; bool flag; int solve; }stu[10004]; bool cmp(student a,student b){ if(a.sum!=b.sum){ return a.sum>b.sum; }else if(a.solve!=b.solve){ return a.solve>b.solve; }else{ return a.id<b.id; } } int main(){ int n,m; scanf("%d %d %d",&n,&k,&m); for(int i=0;i<k;i++){ scanf("%d",&score[i]); } int id,num,point; for(int i=0;i<m;i++){ scanf("%d %d %d",&id,&num,&point); num-=1; if(point!=-1){ stu[id].flag=true; } if(point==-1&&stu[id].get_score[num]==-1){ stu[id].get_score[num]=0; } if(point == score[num]&&stu[id].get_score[num]<score[num]){ stu[id].solve++; } stu[id].id=id; if(stu[id].get_score[num]<point){ stu[id].get_score[num] = point; } } for(int i=1;i<=n;i++){ for(int j=0;j<k;j++){ if(stu[i].get_score[j]!=-1){ stu[i].sum+=stu[i].get_score[j]; } } } sort(stu+1,stu+n+1,cmp); int r=1; for(int i=1;i<=n&&stu[i].flag==true;i++){ if(i>1&&stu[i].sum!=stu[i-1].sum){ r=i; } printf("%d %05d %d",r,stu[i].id,stu[i].sum); for(int j=1;j<=k;j++){ if(stu[i].get_score[j-1]==-1){ printf(" -"); }else{ printf(" %d",stu[i].get_score[j-1]); } } printf("\n"); } return 0; }