YTU-OJ-Problem E: AB编程题--世界杯小组赛

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;
}


你可能感兴趣的:(编程,C++)