首页 > ACM题库 > HDU-杭电 > hdu 2129 Give the rank-模拟[解题报告]C++
2013
12-29

hdu 2129 Give the rank-模拟[解题报告]C++

Give the rank

问题描述 :

As we all know, after a series of international contests, the leaders are wild about ranking the schools to appraise the development of the ACM of our country. There are a lot of schools attend the contests, and each school has some teams or none, and each team may get some prizes of not. There are three kinds of prizes of the contests: gold, silver and copper, and gold is the best one and silver is better than copper. Now we get the result of all the schools, you should rank them, and print them according to the below rules:

    1) We define the ranks between any two schools (or two teams) as the follow rules: firstly we compare the number of gold prizes, and the school is better whose number of gold prizes is larger, and if the numbers of gold prize are the same then compare the silver prizes and then copper prizes. If all the numbers of gold prizes and silver prizes and copper prizes are the same, then we just say the two school (or two teams) are the same good, and their ranks are the same, you have to obey the lexicographic orders when you print them, though.

    2) We define the rank number as the following rule: if there are three schools (or three teams), A is as good as B, but better than C. So the rank number of A and B is 1 (the rank number starts from 1), and C is 3, we omit the rank number 2. If more, the rule goes on.

输入:

There are T cases come, and the first line contains just one integer T.
In each case of following T ones, there is an integer N indicates that there are N following lines describe the information of the encouragement.
Each line contains fours strings: the name of the school, the name of the team, the kind of the prize ("none" means the team gets no prize), and the contest hosting place. Any string is no longer than fifty characters. T<=10, N <=200, and the school number will not be beyond N, and the team number of each school will not exceed 100.

输出:

There are T cases come, and the first line contains just one integer T.
In each case of following T ones, there is an integer N indicates that there are N following lines describe the information of the encouragement.
Each line contains fours strings: the name of the school, the name of the team, the kind of the prize ("none" means the team gets no prize), and the contest hosting place. Any string is no longer than fifty characters. T<=10, N <=200, and the school number will not be beyond N, and the team number of each school will not exceed 100.

样例输入:

1
4
aaa mayday gold nanjing
bbb let's_go silver beijing
ccc how_do_you_do??? none xihua
aaa acm.hdu.edu.cn copper xihua

样例输出:

Case 1:
****************
school number: 3
aaa 1 1 0 1
     team number: 2
     mayday 1 1 0 0
     acm.hdu.edu.cn 2 0 0 1
bbb 2 0 1 0
     team number: 1
     let's_go 1 0 1 0
ccc 3 0 0 0
     team number: 1
     how_do_you_do??? 1 0 0 0
****************

contest number: 3
beijing 0 1 0
nanjing 1 0 0
xihua 0 0 1
****************

题目意思很明显 但是有点恶心额 还是硬着头皮写完了 由于题目没看清贡献了几次wa 但是最后还是解决了 这种题目用来练代码组织能力和结构规划是不错的
————————————————————————————-
代码如下:

#include<iostream>
#include<string>
using namespace std;
typedef struct team
{
    string name;
    int price[4];
}team;
typedef struct school
{
    string name;
    int price[4];
    team tm[101];
    int nowteamlen;
}school;
typedef struct place
{
    string name;
    int price[4];
}place;
school scl[201];
place plc[201];
int tellprice(string price)
{
    if(price=="gold")
        return 1;
    if(price=="silver")
        return 2;
    if(price=="copper")
        return 3;
    return 0;
}
int cmpbyscl(const void *a,const void *b)
{
    school *x=(school *)a;
    school *y=(school *)b;
    if(x->price[1]<y->price[1])
        return 1;
    if(x->price[1]>y->price[1])
        return -1;
    if(x->price[2]<y->price[2])
        return 1;
    if(x->price[2]>y->price[2])
        return -1;
    if(x->price[3]<y->price[3])
        return 1;
    if(x->price[3]>y->price[3])
        return -1;
    if(x->name>y->name)
        return 1;
    if(x->name<y->name)
        return -1;
    return 0;
}
int cmpbytm(const void *a,const void *b)
{
    team *x=(team *)a;
    team *y=(team *)b;
    if(x->price[1]<y->price[1])
        return 1;
    if(x->price[1]>y->price[1])
        return -1;
    if(x->price[2]<y->price[2])
        return 1;
    if(x->price[2]>y->price[2])
        return -1;
    if(x->price[3]<y->price[3])
        return 1;
    if(x->price[3]>y->price[3])
        return -1;
    if(x->name>y->name)
        return 1;
    if(x->name<y->name)
        return -1;
    return 0;
}
int cmpbyplc(const void *a,const void *b)
{
    place *x=(place *)a;
    place *y=(place *)b;
    if(x->name>y->name)
        return 1;
    if(x->name<y->name)
        return -1;
    return 0;
}
int main()
{
    long T,N,t;
    long nowplacelen,nowschoollen;
    string tschool,tteam,tprice,tplace;
    scanf("%d",&T);
    for(int pp=1;pp<=T;pp++)
    {
        scanf("%d",&N);
        nowplacelen=0;
        nowschoollen=0;
        for(int i=0;i<N;i++)
        {
            cin>>tschool>>tteam>>tprice>>tplace;
            for(int j=0;j<nowplacelen;j++)
            {
                if(plc[j].name==tplace)
                    break;
            }
            if(j==nowplacelen)
            {
                plc[j].name=tplace;
                plc[j].price[1]=0;
                plc[j].price[2]=0;
                plc[j].price[3]=0;
                plc[j].price[tellprice(tprice)]=1;
                nowplacelen++;
            }
            else
            {
                plc[j].price[tellprice(tprice)]++;
            }
            for(j=0;j<nowschoollen;j++)
            {
                if(scl[j].name==tschool)
                    break;
            }
            if(j==nowschoollen)
            {
                scl[j].name=tschool;
                scl[j].price[1]=0;
                scl[j].price[2]=0;
                scl[j].price[3]=0;
                scl[j].price[tellprice(tprice)]=1;
                nowschoollen++;
                scl[j].tm[0].name=tteam;
                scl[j].tm[0].price[1]=0;
                scl[j].tm[0].price[2]=0;
                scl[j].tm[0].price[3]=0;
                scl[j].tm[0].price[tellprice(tprice)]=1;
                scl[j].nowteamlen=1;
            }
            else
            {
                scl[j].price[tellprice(tprice)]++;
                for(int t=0;t<scl[j].nowteamlen;t++)
                {
                    if(tteam==scl[j].tm[t].name)
                        break;
                }
                if(t==scl[j].nowteamlen)
                {
                    scl[j].tm[t].name=tteam;
                    scl[j].tm[t].price[1]=0;
                    scl[j].tm[t].price[2]=0;
                    scl[j].tm[t].price[3]=0;
                    scl[j].tm[t].price[tellprice(tprice)]=1;
                    scl[j].nowteamlen++;
                }
                else
                {
                    scl[j].tm[t].price[tellprice(tprice)]++;
                }
            }
        }
        printf("Case %d:\n****************\n",pp);       
        printf("school number: %d\n",nowschoollen);
        qsort(scl,nowschoollen,sizeof(scl[0]),cmpbyscl);
        for(i=0;i<nowschoollen;i++)
        {
            qsort(scl[i].tm,scl[i].nowteamlen,sizeof(scl[i].tm[0]),cmpbytm);
        }
        qsort(plc,nowplacelen,sizeof(plc[0]),cmpbyplc);
        cout<<scl[0].name<<" "<<1<<" "<<scl[0].price[1]<<" "<<scl[0].price[2]<<" "<<scl[0].price[3]<<endl;
        printf("     team number: %d\n",scl[0].nowteamlen);
        cout<<"     "<<scl[0].tm[0].name<<" "<<1<<" "<<scl[0].tm[0].price[1]
            <<" "<<scl[0].tm[0].price[2]<<" "<<scl[0].tm[0].price[3]<<endl;
        for(int j=1,tt=1;j<scl[0].nowteamlen;j++)
        {
            if((scl[0].tm[j].price[1]!=scl[0].tm[j-1].price[1])||(scl[0].tm[j].price[2]!=scl[0].tm[j-1].price[2])||(scl[0].tm[j].price[3]!=scl[0].tm[j-1].price[3]))
                tt=j+1;
            cout<<"     "<<scl[0].tm[j].name<<" "<<tt<<" "<<scl[0].tm[j].price[1]
                <<" "<<scl[0].tm[j].price[2]<<" "<<scl[0].tm[j].price[3]<<endl;
        }
        for(i=1,t=1;i<nowschoollen;i++)
        {
            if((scl[i].price[1]!=scl[i-1].price[1])||(scl[i].price[2]!=scl[i-1].price[2])||(scl[i].price[3]!=scl[i-1].price[3]))
                t=i+1;
            cout<<scl[i].name<<" "<<t<<" "<<scl[i].price[1]<<" "<<scl[i].price[2]<<" "<<scl[i].price[3]<<endl;
            printf("     team number: %d\n",scl[i].nowteamlen);
            cout<<"     "<<scl[i].tm[0].name<<" "<<1<<" "<<scl[i].tm[0].price[1]
                <<" "<<scl[i].tm[0].price[2]<<" "<<scl[i].tm[0].price[3]<<endl;
            for(int j=1,tt=1;j<scl[i].nowteamlen;j++)
            {
                if((scl[i].tm[j].price[1]!=scl[i].tm[j-1].price[1])||(scl[i].tm[j].price[2]!=scl[i].tm[j-1].price[2])||(scl[i].tm[j].price[3]!=scl[i].tm[j-1].price[3]))
                    tt=j+1;
                cout<<"     "<<scl[i].tm[j].name<<" "<<tt<<" "<<scl[i].tm[j].price[1]
                    <<" "<<scl[i].tm[j].price[2]<<" "<<scl[i].tm[j].price[3]<<endl;
            }
        }
        printf("****************\n\n");
        printf("contest number: %d\n",nowplacelen);
        for(i=0;i<nowplacelen;i++)
        {
            cout<<plc[i].name<<" "<<plc[i].price[1]<<" "<<plc[i].price[2]<<" "<<plc[i].price[3]<<endl;
        }
        printf("****************\n");
    }
    return 0;
}

 


  1. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。