AB编程题--世界杯小组赛

# 注：本题目自由设计，但必须使用类进行代码设计。

a、积分高者排名靠前
b、小组中总净胜球高者排名靠前
c、小组中总进球数高者排名靠前
d、不能确定

//以下是可供参考的代码
#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.showResult();
return 0;
}

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

``a2 a4``

``````#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.show();
GroupA.showResult();
return 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; //两队的比分