zoukankan      html  css  js  c++  java
  • 算法初步——排序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;
    } 
  • 相关阅读:
    php根据时间显示刚刚,几分钟前,几小时前的实现代码
    PHP中获取当前页面的完整URL
    PhpExcel中文帮助手册|PhpExcel使用方法
    洛谷P1781 宇宙总统【排序+字符串】
    洛谷P1579 哥德巴赫猜想(升级版)【水题+素数】
    洛谷P1478 陶陶摘苹果(升级版)【水题】
    洛谷P1002 过河卒【dp】
    51Nod
    排序算法总结(C++)
    UVA1339
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12151577.html
Copyright © 2011-2022 走看看