算法初步——排序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;
} 
原文地址:https://www.cnblogs.com/JasonPeng1/p/12151577.html