算法初步——排序B1015/A1062.德才论
Posted jasonpeng1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法初步——排序B1015/A1062.德才论相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h> #include<math.h> using namespace std; const int MAX_LEN = 100005; //const int MAX_D = 31; struct student{ int number; int fir; int sec; int total; int grade; }; bool cmp(student a,student b){ if(a.grade != b.grade){ return a.grade < b.grade; } /*if(a.grade == b.grade){ return a.total > b.total; if(a.total == b.total && a.fir != b.fir){ return a.fir > b.fir; } if(a.total == b.total && a.fir == b.fir){ return a.number < b.number; } }*/ else if(a.total != b.total) return a.total > b.total; else if (a.fir != b.fir) return a.fir > b.fir; else return a.number < b.number; } int main(){ int n,firstand,secstand; cin>>n; cin>>firstand; cin>>secstand; student stu[n]; for(int i=0;i<n;++i){ cin>>stu[i].number; cin>>stu[i].fir; cin>>stu[i].sec; stu[i].total = stu[i].fir + stu[i].sec; if(stu[i].fir >= firstand && stu[i].sec >= firstand){ if(stu[i].fir >= secstand && stu[i].sec >= secstand){ stu[i].grade = 1; continue; } if(stu[i].fir >= secstand && stu[i].sec < secstand){ stu[i].grade = 2; continue; } if(stu[i].fir < secstand && stu[i].sec < secstand && stu[i].fir >= stu[i].sec){ stu[i].grade = 3; continue; }else{ stu[i].grade = 4; continue; } }else{ stu[i].grade = 5; } } sort(stu,stu+n,cmp); int count = 0; for(int i = 0;i<n;++i){ if(stu[i].grade != 5){ count++; } } cout<<count<<endl; for(int i = 0 ;i<n;++i){ if(stu[i].grade != 5){ cout<<stu[i].number<<" "<<stu[i].fir<<" "<<stu[i].sec<<endl; } } system("pause"); return 0; }
把 cin 替换为 scanf 就不会超时了。
#include <bits/stdc++.h> #include<math.h> using namespace std; const int MAX_LEN = 100005; //const int MAX_D = 31; struct student{ char id[10]; int fir; int sec; int total; int grade; }; bool cmp(student a,student b){ if(a.grade != b.grade){ return a.grade < b.grade; } /*if(a.grade == b.grade){ return a.total > b.total; if(a.total == b.total && a.fir != b.fir){ return a.fir > b.fir; } if(a.total == b.total && a.fir == b.fir){ return a.number < b.number; } }*/ else if(a.total != b.total) return a.total > b.total; else if (a.fir != b.fir) return a.fir > b.fir; else return strcmp(a.id,b.id) < 0; } int main(){ int n,firstand,secstand; //cin>>n; scanf("%d",&n); //cin>>firstand; scanf("%d",&firstand); //cin>>secstand; scanf("%d",&secstand); student stu[n]; for(int i=0;i<n;++i){ //cin>>stu[i].id; scanf("%s",&stu[i].id); //cin>>stu[i].fir; scanf("%d",&stu[i].fir); //cin>>stu[i].sec; scanf("%d",&stu[i].sec); stu[i].total = stu[i].fir + stu[i].sec; if(stu[i].fir >= firstand && stu[i].sec >= firstand){ if(stu[i].fir >= secstand && stu[i].sec >= secstand){ stu[i].grade = 1; continue; } if(stu[i].fir >= secstand && stu[i].sec < secstand){ stu[i].grade = 2; continue; } if(stu[i].fir < secstand && stu[i].sec < secstand && stu[i].fir >= stu[i].sec){ stu[i].grade = 3; continue; }else{ stu[i].grade = 4; continue; } }else{ stu[i].grade = 5; } } sort(stu,stu+n,cmp); int count = 0; for(int i = 0;i<n;++i){ if(stu[i].grade != 5){ count++; } } cout<<count<<endl; for(int i = 0 ;i<n;++i){ if(stu[i].grade != 5){ cout<<stu[i].id<<" "<<stu[i].fir<<" "<<stu[i].sec<<endl; } } system("pause"); return 0; }
以上是关于算法初步——排序B1015/A1062.德才论的主要内容,如果未能解决你的问题,请参考以下文章
快速排序-浙大版《数据结构学习与实验指导(第2版)》-基础实验7-2.3:德才论