算法初步——排序B1015/A1062.德才论

算法初步——排序B1015/A1062.德才论_第1张图片

 

#include 
#include
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;ii){
        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;ii){
        if(stu[i].grade != 5){
            count++;
        }
    }
    cout<endl;
    for(int i = 0 ;ii){
        if(stu[i].grade != 5){
            cout<" "<" "<endl;
        }
    }
    system("pause");
    return 0;
} 

把 cin 替换为 scanf 就不会超时了。

算法初步——排序B1015/A1062.德才论_第2张图片

 

 

#include 
#include
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;ii){
        //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;ii){
        if(stu[i].grade != 5){
            count++;
        }
    }
    cout<endl;
    for(int i = 0 ;ii){
        if(stu[i].grade != 5){
            cout<" "<" "<endl;
        }
    }
    system("pause");
    return 0;
} 

你可能感兴趣的:(算法初步——排序B1015/A1062.德才论)