码迷,mamicode.com
首页 > 其他好文 > 详细

YTUOJ-世界杯小组赛

时间:2015-06-30 10:36:22      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:c++   博客   阅读   编程      

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 <cstring>
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();
};
void FootballGroup::addMatchresult(int teamindex1,int teamindex2,int score1,int score2)
{

    if(score1>score2)
    {
        score[teamindex1]+=3;
        goaldiff[teamindex1]+=score1-score2;
        goaldiff[teamindex2]+=score2-score1;
        totalgoals[teamindex1]+=score1;
        totalgoals[teamindex2]+=score2;
    }
    else if(score1<score2)
    {
        score[teamindex2]+=3;
        goaldiff[teamindex1]+=score1-score2;
        goaldiff[teamindex2]+=score2-score1;
        totalgoals[teamindex1]+=score1;
        totalgoals[teamindex2]+=score2;
    }
    else
    {
        score[teamindex1]+=1;
        totalgoals[teamindex1]+=score1;
        score[teamindex2]+=1;
        totalgoals[teamindex2]+=score2;
    }

}
void FootballGroup::showResult()
{

    int i,j,t1,t2,t3;
    string c;
    for(i=0; i<3; i++)
        for(j=i+1; j<4; j++)
        {
            if(score[j]>score[i])
            {
                t1=score[i];
                score[i]=score[j];
                score[j]=t1;
                t2=goaldiff[i];
                goaldiff[i]=goaldiff[j];
                goaldiff[j]=t2;
                t3=totalgoals[i];
                totalgoals[i]=totalgoals[j];
                totalgoals[j]=t3;
                c=teamname[i];
                teamname[i]=teamname[j];
                teamname[j]=c;
            }
        }
    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])
                {
                    t1=score[i];
                    score[i]=score[j];
                    score[j]=t1;
                    t2=goaldiff[i];
                    goaldiff[i]=goaldiff[j];
                    goaldiff[j]=t2;
                    t3=totalgoals[i];
                    totalgoals[i]=totalgoals[j];
                    totalgoals[j]=t3;
                    c=teamname[i];
                    teamname[i]=teamname[j];
                    teamname[j]=c;
                }
            }
        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])
                {
                    t1=score[i];
                    score[i]=score[j];
                    score[j]=t1;
                    t2=goaldiff[i];
                    goaldiff[i]=goaldiff[j];
                    goaldiff[j]=t2;
                    t3=totalgoals[i];
                    totalgoals[i]=totalgoals[j];
                    totalgoals[j]=t3;
                    c=teamname[i];
                    teamname[i]=teamname[j];
                    teamname[j]=c;
                }
            }
        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])
                    {
                        t1=score[i];
                        score[i]=score[j];
                        score[j]=t1;
                        t2=goaldiff[i];
                        goaldiff[i]=goaldiff[j];
                        goaldiff[j]=t2;
                        t3=totalgoals[i];
                        totalgoals[i]=totalgoals[j];
                        totalgoals[j]=t3;
                        c=teamname[i];
                        teamname[i]=teamname[j];
                        teamname[j]=c;
                    }
                }
            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])
                {
                    t1=score[i];
                    score[i]=score[j];
                    score[j]=t1;
                    t2=goaldiff[i];
                    goaldiff[i]=goaldiff[j];
                    goaldiff[j]=t2;
                    t3=totalgoals[i];
                    totalgoals[i]=totalgoals[j];
                    totalgoals[j]=t3;
                    c=teamname[i];
                    teamname[i]=teamname[j];
                    teamname[j]=c;
                }
            }
        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])
                    {
                        t1=score[i];
                        score[i]=score[j];
                        score[j]=t1;
                        t2=goaldiff[i];
                        goaldiff[i]=goaldiff[j];
                        goaldiff[j]=t2;
                        t3=totalgoals[i];
                        totalgoals[i]=totalgoals[j];
                        totalgoals[j]=t3;
                        c=teamname[i];
                        teamname[i]=teamname[j];
                        teamname[j]=c;
                    }
                }
            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])
                    {
                        t1=score[i];
                        score[i]=score[j];
                        score[j]=t1;
                        t2=goaldiff[i];
                        goaldiff[i]=goaldiff[j];
                        goaldiff[j]=t2;
                        t3=totalgoals[i];
                        totalgoals[i]=totalgoals[j];
                        totalgoals[j]=t3;
                        c=teamname[i];
                        teamname[i]=teamname[j];
                        teamname[j]=c;
                    }
                }
            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])
                    {
                        t1=score[i];
                        score[i]=score[j];
                        score[j]=t1;
                        t2=goaldiff[i];
                        goaldiff[i]=goaldiff[j];
                        goaldiff[j]=t2;
                        t3=totalgoals[i];
                        totalgoals[i]=totalgoals[j];
                        totalgoals[j]=t3;
                        c=teamname[i];
                        teamname[i]=teamname[j];
                        teamname[j]=c;
                    }
                }
            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;
}

运行结果:

技术分享


学习心得:

好久没有在刷题上这么激动过了,,,这道题分析得,,,可真是辛苦,,,用时一个多小时终究还是做出来了,听说是去年13届的考题,当时只有少数几个人做出来了哈哈~


题目代码有些冗长,,,是一步一步分析来的,可能有优化方法,但是我已经不想去想了,有个地方可以改正一下,将所有的比较交换归纳成一个函数,其实开始的时候也是这么做的,但是当时出现了一个失误,只交换了比较的那个值和名字,另外两项并没有交换,所以之前做错了。可以在CLASS中添加一个函数sort排序,但是然后交换CLass中所有的数据成员,有意向的可以自行修改,我这里就不再修改,自接提交了。


版权声明:本文为博主原创文章,未经博主允许不得转载。

YTUOJ-世界杯小组赛

标签:c++   博客   阅读   编程      

原文地址:http://blog.csdn.net/liuchang54/article/details/46688919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!