2615: AB编程题--世界杯小组赛
时间限制: 1 Sec 内存限制: 128 MB
提交: 100 解决: 35
题目描述
注:本题目*设计,但必须使用类进行代码设计。
世界杯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;
}
输入
第一行 4个参赛队伍的名称
第二行开始到第七行,6场比赛的比分(a1-a2;a1-a3;a1-a4;a2-a3;a2-a4;a3-a4)
输出
进入淘汰赛阶段的两只球队,如果不能确定,则输出"NO"
样例输入
a1 a2 a3 a4
1 2
0 0
2 3
3 1
2 2
1 2
样例输出
a2 a4
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#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;
}
#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;
}