• 1141 PAT Ranking of Institutions[难]


     1141 PAT Ranking of Institutions (25 分)

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (105​​), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

    ID Score School
    

    where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, Athe advanced level and T the top level; Score is an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

    Output Specification:

    For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

    Rank School TWS Ns
    

    where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

    The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

    Sample Input:

    10
    A57908 85 Au
    B57908 54 LanX
    A37487 60 au
    T28374 67 CMU
    T32486 24 hypu
    A66734 92 cmu
    B76378 71 AU
    A47780 45 lanx
    A72809 100 pku
    A03274 45 hypu
    

    Sample Output:

    5
    1 cmu 192 2
    1 au 192 3
    3 pku 100 1
    4 hypu 81 2
    4 lanx 81 2

     题目大意:给出学生的id、分数、学校了,然后计算学校的相关排名,通过计算本考场内考生的等级分数,等最后按照一个规则排名。

     //这个题确实挺麻烦的。

    #include <iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    struct Sch{
        int sb,sa,st;
        int fin,stu;
        string name;
        Sch(string n){
            name=n;
            sb=0;sa=0;st=0;stu=1;}
        void cal(){//但是如何取整数部分呢?如果直接赋值那应该是4舍5入的。
            fin=sb/1.5+sa+st*1.5;
        }
    };
    
    string lower(string s){
        for(int i=0;i<s.size();i++){
            if(s[i]>='A'&&s[i]<='Z'){
                s[i]=s[i]-'A'+'a';
            }
        }
        return s;
    }
    
    map<string,int> name2id;
    vector<Sch> vsch;
    
    void add(string id,int index,int score){
        if(id[0]=='A')
            vsch[index].sa+=score;
        else if(id[0]=='B')
            vsch[index].sb+=score;
        else vsch[index].st+=score;
    }
    
    bool cmp(Sch& a,Sch& b){//排序函数。
        if(a.fin>b.fin)return true;
        else if(a.fin==b.fin&&a.stu<b.stu)return true;
        else if(a.fin==b.fin&&a.stu==b.stu&&a.name<b.name)return true;
    }
    
    int main() {
        int n;
        cin>>n;
        string id,sh;
        int sco,ct=0;//ct表示有多少个学校。
        for(int i=0;i<n;i++){
            cin>>id>>sco>>sh;
            sh=lower(sh);//函数是如何转换大小写?
            if(name2id.count(sh)==0){//如果当前机构不存在的话
                name2id[sh]=ct++;
                vsch.push_back(Sch(sh));//把机构的名字传进去。
                add(id,ct-1,sco);
            }else{//如果已经存在
                int temp=name2id[sh];
                add(id,temp,sco);
                vsch[temp].stu++;//又多了一个学生。
            }
        }
        int size=name2id.size();
        cout<<size<<'
    ';
        //计算得分;
        for(int i=0;i<vsch.size();i++){
            vsch[i].cal();
        }
        sort(vsch.begin(),vsch.end(),cmp);
        int rank=1;
        for(int i=0;i<size;i++){
            if(i!=0){
                if(vsch[i].fin!=vsch[i-1].fin)
                    rank++;//这时候排名才++;
                //其他情况排名不++
            }
            cout<<rank<<" "<<vsch[i].name<<" "<<vsch[i].fin<<" "<<vsch[i].stu<<'
    ';
    
        }
        return 0;
    }
    wrong

    //这个我写了将近一个小时,但是最终第一次提交结果如下: 

     真是心里凉凉,待会再看柳神的吧。

    AC了:

    #include <iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    struct Sch{
        int sb,sa,st;
        int fin,stu;
        string name;
        Sch(string n){
            name=n;
            sb=0;sa=0;st=0;stu=1;}
        void cal(){//但是如何取整数部分呢?如果直接赋值那应该是4舍5入的。
            fin=sb/1.5+sa+st*1.5;
        }
    };
    
    string lower(string s){
        for(int i=0;i<s.size();i++){
            if(s[i]>='A'&&s[i]<='Z'){
                s[i]=s[i]-'A'+'a';
            }
        }
        return s;
    }
    
    map<string,int> name2id;
    vector<Sch> vsch;
    
    void add(string id,int index,int score){
        if(id[0]=='A')
            vsch[index].sa+=score;//都是int型的操作。
        else if(id[0]=='B')
            vsch[index].sb+=score;
        else vsch[index].st+=score;
    }
    
    bool cmp(Sch& a,Sch& b){//排序函数。
        if(a.fin>b.fin)return true;
        else if(a.fin==b.fin&&a.stu<b.stu)return true;
        else if(a.fin==b.fin&&a.stu==b.stu&&a.name<b.name)return true;
        return false;
    }
    
    int main() {
        int n;
        cin>>n;
        string id,sh;
        int sco,ct=0;//ct表示有多少个学校。
        for(int i=0;i<n;i++){
            cin>>id>>sco>>sh;
            sh=lower(sh);//函数是如何转换大小写?
            if(name2id.count(sh)==0){//如果当前机构不存在的话
                name2id[sh]=ct++;
                vsch.push_back(Sch(sh));//把机构的名字传进去。
                add(id,ct-1,sco);
            }else{//如果已经存在
                int temp=name2id[sh];
                add(id,temp,sco);
                vsch[temp].stu++;//又多了一个学生。
            }
        }
        int sz=name2id.size();
        cout<<sz<<'
    ';
        //计算得分;
        for(int i=0;i<vsch.size();i++){
            vsch[i].cal();
        }
        sort(vsch.begin(),vsch.end(),cmp);
        int rank=1;
        for(int i=0;i<sz;i++){
            if(i!=0){
                if(vsch[i].fin!=vsch[i-1].fin)
                    rank=i+1;//这时候排名才++;
                //其他情况排名不++
            }
            cout<<rank<<" "<<vsch[i].name<<" "<<vsch[i].fin<<" "<<vsch[i].stu<<'
    ';
    
        }
        return 0;
    }

    1.在最终输出排名时,一开始写的rank++,明显不对,而应该是rank=i+1才对!

    2.另外非常重要的是这个cmp函数里,最后一定要有一个return false,不然会出大问题!

    3.tolower函数是对单个char来使用的。

  • 相关阅读:
    ps一寸照片
    作用域链词法分析
    ajax加上随机数可以强制刷新网页
    ajaxStar,ajaxStop开始时候加载图片,加载完成之后去掉图片
    布尔变量 转换
    将2016-11-02转化成二零一六年十一月二日
    日期函数
    js数据类型
    编写页面分析
    小三角
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9847857.html
Copyright © 2020-2023  润新知