AB编程题--世界杯小组赛
时间: 1ms 内存:128M
描述:
注:本题目自由设计,但必须使用类进行代码设计。
世界杯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
提示:
参考答案(内存最优[0]):
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
struct messa
{
int score=0;
int goaldiff=0;
int totalgoals=0;
string teamname;
};
bool cmp1(messa a,messa b)
{
return a.score<b.score;
}
bool cmp2(messa a,messa b)
{
return a.goaldiff<b.goaldiff;
}
bool cmp3(messa a,messa b)
{
return a.totalgoals<b.totalgoals;
}
class FootballGroup
{
private:
messa infor[4];
public:
FootballGroup(){}
void setTeamName(int teamindex,string name) //设置第teamindex+1个队的队名
{
infor[teamindex].teamname= name;
}
void addMatchresult(int teamindex1,int teamindex2,int score1,int score2);
void showResult();
void show()
{
for(int i=0; i<4; ++i)
{
cout<<infor[i].teamname<<" "<<infor[i].score<<" "<<infor[i].goaldiff<<" "<<infor[i].totalgoals<<endl;
}
}
};
void FootballGroup::addMatchresult(int teamindex1,int teamindex2,int score1,int score2)
{
if(score1>score2)
{
infor[teamindex1].score+=3;
}
else if(score1<score2)
{
infor[teamindex2].score+=3;
}
else
{
infor[teamindex1].score+=1;
infor[teamindex2].score+=1;
}
infor[teamindex1].goaldiff+=score1-score2;
infor[teamindex2].goaldiff+=score2-score1;
infor[teamindex1].totalgoals+=score1;
infor[teamindex2].totalgoals+=score2;
}
void FootballGroup::showResult()
{
sort(infor,infor+4,cmp1);
if(infor[1].score!=infor[2].score)
{
cout<<infor[2].teamname<<" "<<infor[3].teamname;
return ;
}
sort(infor,infor+4,cmp2);
if(infor[1].goaldiff!=infor[2].goaldiff)
{
cout<<infor[2].teamname<<" "<<infor[3].teamname;
return ;
}
sort(infor,infor+4,cmp3);
if(infor[1].totalgoals!=infor[2].totalgoals)
{
cout<<infor[2].teamname<<" "<<infor[3].teamname;
return ;
}
cout<<"NO\n";
return;
}
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.show();
GroupA.showResult();
return 0;
}
参考答案(时间最优[0]):
#include <stdio.h>
#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
struct Score
{
int score;
int goaldiff;
int totalgoals;
string teamname;
};
class FootballGroup
{
private:
//int score[4]; //总积分
//int goaldiff[4]; //总净胜球
//int totalgoals[4]; //总进球数
Score sc[4];
//string teamname[4] ; //球队的名称
public:
FootballGroup()
{
for(int i=0; i<4; i++)
{
sc[i].score=sc[i].goaldiff=sc[i].totalgoals=0;
}
}
void setTeamName(int teamindex,string name) //设置第teamindex+1个队的队名
{
sc[teamindex].teamname = 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)
{
sc[teamindex1].score+=3;
sc[teamindex2].score+=0;
}
else if(score1==score2)
{
sc[teamindex1].score+=1;
sc[teamindex2].score+=1;
}
else
{
sc[teamindex1].score+=0;
sc[teamindex2].score+=3;
}
sc[teamindex1].goaldiff+=(score1-score2);
sc[teamindex2].goaldiff+=(score2-score1);
sc[teamindex1].totalgoals+=score1;
sc[teamindex2].totalgoals+=score2;
}
int cmp(const void *a,const void *b)
{
Score *p1=(Score *)a,*p2=(Score *)b;
if(p1->score!=p2->score)
return p2->score-p1->score;
else
{
if(p1->goaldiff!=p2->goaldiff)
return p2->goaldiff-p1->goaldiff;
else
{
if(p1->totalgoals!=p2->totalgoals)
return p2->totalgoals-p1->totalgoals;
}
}
}
void FootballGroup::showResult()
{
qsort(sc,4,sizeof(sc[0]),cmp);
if(sc[1].score==sc[2].score&&sc[1].goaldiff==sc[2].goaldiff&&sc[1].totalgoals==sc[2].totalgoals)
cout<<"NO"<<endl;
else
{
cout<<sc[0].teamname<<" "<<sc[1].teamname<<endl;
}
/*int i,j;
for(i=0; i<4; i++)
{
max=score[i];
maxscore=i;
for(j=i+1; j<4; j++)
{
if(max<score[j])
{
int temp;
temp=score[i];
score[i]=score[j];
score[j]=temp;
maxscore=j;
max=score[j];
}
else if(max==score[j])
{
if(goaldiff[maxscore]<goaldiff[j])
{
int temp;
temp=score[i];
score[i]=score[j];
score[j]=temp;
maxscore=j;
max=score[j];
}
else if(goaldiff[maxscore]==goaldiff[j])
{
if(totalgoals[maxscore]<totalgoals[j])
{
int temp;
temp=score[i];
score[i]=score[j];
score[j]=temp;
maxscore=j;
max=score[j];
}
}
}
}
}*/
}
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;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。