YTU-OJ-Problem E: AB程式設計題--世界盃小組賽

kewlgrl發表於2015-07-21

Problem E: AB程式設計題--世界盃小組賽

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 85  Solved: 25
[Submit][Status][Web Board]

Description

注:本題目自由設計,但必須使用類進行程式碼設計。

世界盃32支參賽隊分為八個小組,每個小組分別有四支球隊進行比賽,每支球隊都必須和其他三支球隊進行且只進行一場比賽,每組4個隊迴圈比賽,共打6場(a1-a2;a1-a3;a1-a4;a2-a3;a2-a4;a3-a4),每場比賽90分鐘,勝平負分別積3、1、0分。每個小組積分的前兩名球隊出線進入淘汰賽階段的1/8決賽,共16支隊,即“16強”。

每個小組分別有四隻球隊,其排名按以下規則確定:
a、積分高者排名靠前
b、小組中總淨勝球高者排名靠前
c、小組中總進球數高者排名靠前
d、不能確定
淨勝球數是指進球數(正數)與失球數(正數)的差。
如,紅隊勝黃隊4:2,藍隊勝黃隊3:1,紅隊負藍隊2:3
紅隊進6球,失5球,淨勝球數為:6-5=1
黃隊進3球,失7球,淨勝球數為:3-7=-4
藍隊進6球,失3球,淨勝球數為:6-3=3
//以下是可供參考的程式碼
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

class FootballGroup
{
private:
    int score[4]; //總積分
    int goaldiff[4]; //總淨勝球
    int totalgoals[4]; //總進球數
    string teamname[4] ; //球隊的名稱
public:
    FootballGroup()
    {
        for(int i=0; i<4; i++)
        {
            score[i]=goaldiff[i]=totalgoals[i]=0;
        }
    }
    void setTeamName(int teamindex,string name)  //設定第teamindex+1個隊的隊名
    {
        teamname[teamindex] = name;
    }
    void addMatchresult(int teamindex1,int teamindex2,int score1,int score2);
    void showResult();
};
int main()
{
    FootballGroup GroupA;
    string name;
    int i,j;
    for(i=0; i<4; i++)
    {
        cin>>name;
        GroupA.setTeamName(i,name); //球隊a1,a2,a3,a4的名稱
    }
    int score1,score2;
    for(i=0; i<3; i++) //a1-a2;a1-a3;a1-a4;a2-a3;a2-a4;a3-a4
        for(j=i+1; j<4; j++)
        {
            cin>>score1>>score2; //兩隊的比分
            GroupA.addMatchresult(i,j,score1,score2);
        }
    GroupA.showResult();
    return 0;
}

Input

第一行 4個參賽隊伍的名稱
第二行開始到第七行,6場比賽的比分(a1-a2;a1-a3;a1-a4;a2-a3;a2-a4;a3-a4)

Output

進入淘汰賽階段的兩隻球隊,如果不能確定,則輸出"NO"

Sample Input

a1 a2 a3 a4
1 2
0 0
2 3
3 1
2 2
1 2

Sample Output

a2 a4

HINT

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

class FootballGroup
{
private:
    int score[4]; //總積分
    int goaldiff[4]; //總淨勝球
    int totalgoals[4]; //總進球數
    string teamname[4] ; //球隊的名稱
public:
    FootballGroup()
    {
        for(int i=0; i<4; i++)
        {
            score[i]=goaldiff[i]=totalgoals[i]=0;
        }
    }
    void swap(int *s,int *ss,int *g,int *gg,int *t,int *tt,string *n,string *nn)
    {
        int m;
        string str;
        m=*s;
        *s=*ss;
        *ss=m;
        m=*g;
        *g=*gg;
        *gg=m;
        m=*t;
        *t=*tt;
        *tt=m;
        str=*n;
        *n=*nn;
        *nn=str;
    }
    void setTeamName(int teamindex,string name)  //設定第teamindex+1個隊的隊名
    {
        teamname[teamindex] = name;
    }
    void addMatchresult(int teamindex1,int teamindex2,int score1,int score2)
    {
        totalgoals[teamindex1]+=score1;
        totalgoals[teamindex2]+=score2;
        if(score1>score2)
        {
            goaldiff[teamindex1]+=(score1-score2);
            score[teamindex1]+=3;
        }
        else if(score1<score2)
        {
            goaldiff[teamindex2]+=(score2-score1);
            score[teamindex2]+=3;
        }
        else
        {
            score[teamindex1]+=1;
            score[teamindex2]+=1;
        }
    }
    void showResult()
    {
        int i,j;
        for(i=0; i<3; i++)
            for(j=i+1; j<4; j++)
                if(score[j]>score[i])
                    swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
        if(score[1]!=score[2])
            cout<<teamname[0]<<" "<<teamname[1]<<endl;
        else if (score[0]!=score[1]&&score[1]==score[2]&&score[2]!=score[3])
        {

            for(i=1; i<2; i++)
                for(j=i+1; j<3; j++)
                {
                    if(goaldiff[j]>goaldiff[i])
                        swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                }
            if (goaldiff[1]!=goaldiff[2])
            {
                cout<<teamname[0]<<" "<<teamname[1]<<endl;
            }
            else
            {
                if (totalgoals[1]>totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else if(totalgoals[1]<totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[2]<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        else if (score[0]!=score[1]&&score[1]==score[2]&&score[2]==score[3])
        {

            for(i=1; i<3; i++)
                for(j=i+1; j<4; j++)
                {
                    if(goaldiff[j]>goaldiff[i])
                        swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                }
            if (goaldiff[1]!=goaldiff[2])
            {
                cout<<teamname[0]<<" "<<teamname[1]<<endl;
            }
            else if (goaldiff[1]==goaldiff[2]&&goaldiff[2]!=goaldiff[3])
            {
                if (totalgoals[1]>totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else if(totalgoals[1]<totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[2]<<endl;
                else
                    cout<<"NO"<<endl;
            }
            else if (goaldiff[1]==goaldiff[2]&&goaldiff[2]==goaldiff[3])
            {
                for(i=1; i<3; i++)
                    for(j=i+1; j<4; j++)
                    {
                        if(totalgoals[j]>totalgoals[i])
                            swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                    }
                if (totalgoals[1]!=totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        else if(score[1]==score[2]&&score[0]==score[1]&&score[2]==score[3])
        {
            for(i=0; i<3; i++)
                for(j=i+1; j<4; j++)
                {
                    if(goaldiff[j]>goaldiff[i])
                        swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                }
            if(goaldiff[1]!=goaldiff[2])
            {
                cout<<teamname[0]<<" "<<teamname[1]<<endl;
            }
            else if (goaldiff[0]!=goaldiff[1]&&goaldiff[1]==goaldiff[2]&&goaldiff[2]!=goaldiff[3])
            {

                if (totalgoals[1]>totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else if(totalgoals[1]<totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[2]<<endl;
                else
                    cout<<"NO"<<endl;
            }
            else if (goaldiff[0]!=goaldiff[1]&&goaldiff[1]==goaldiff[2]&&goaldiff[2]==goaldiff[3])
            {
                for(i=1; i<3; i++)
                    for(j=i+1; j<4; j++)
                    {
                        if(totalgoals[j]>totalgoals[i])
                            swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                    }
                if (totalgoals[1]!=totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else
                    cout<<"NO"<<endl;
            }
            else if(goaldiff[0]==goaldiff[1]&&goaldiff[1]==goaldiff[2]&&goaldiff[2]!=goaldiff[3])
            {

                for(i=0; i<2; i++)
                    for(j=i+1; j<3; j++)
                    {
                        if(totalgoals[j]>totalgoals[i])
                            swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                    }
                if(totalgoals[0]!=totalgoals[1]&&totalgoals[1]!=totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else
                    cout<<"NO"<<endl;
            }
            else if(goaldiff[0]==goaldiff[1]&&goaldiff[1]==goaldiff[2]&&goaldiff[2]==goaldiff[3])
            {
                for(i=0; i<3; i++)
                    for(j=i+1; j<4; j++)
                    {
                        if(totalgoals[j]>totalgoals[i])
                            swap(&score[i],&score[j],&goaldiff[i],&goaldiff[j],&totalgoals[i],&totalgoals[j],&teamname[i],&teamname[j]);
                    }
                if(totalgoals[1]!=totalgoals[2])
                    cout<<teamname[0]<<" "<<teamname[1]<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
    }
};
int main()
{
    FootballGroup GroupA;
    string name;
    int i,j;
    for(i=0; i<4; i++)
    {
        cin>>name;
        GroupA.setTeamName(i,name); //球隊a1,a2,a3,a4的名稱
    }
    int score1,score2;
    for(i=0; i<3; i++) //a1-a2;a1-a3;a1-a4;a2-a3;a2-a4;a3-a4
        for(j=i+1; j<4; j++)
        {
            cin>>score1>>score2; //兩隊的比分
            GroupA.addMatchresult(i,j,score1,score2);
        }
    GroupA.showResult();
    return 0;
}


相關文章