69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define BIGGER 1
#define SMALLER 0
#define EQUAL 2
typedef struct Team
{
int team_num; //for 队伍编号
int score; //for 积分
int win_balls; //for 胜球
int lose_balls; //for 输球
}team;
void team_init(team *T)
{
int i;
for(i = 0;i<10;i++)
{
T[i].team_num = i;
}
}
void team_input_score(team *T)
{
int i,j;
for(i = 0; i < 9; i++)
{
printf("NO.%d round\n", i+1);//第几轮
for(j = i + 1; j < 10; j++)
{
printf("team%d vs team%d\n", i, j);
printf("input team[%d] score\n", i);
scanf("%d",&T[i].win_balls);
T[j].lose_balls = T[i].win_balls;
printf("input team[%d] score\n", j);
scanf("%d",&T[j].win_balls);
T[i].lose_balls = T[j].win_balls;
if(T[i].win_balls > T[j].win_balls)
{
T[i].score += 3;
}
else if(T[i].win_balls < T[j].win_balls)
{
T[j].score += 3;
}
else
{
T[i].score += 1;
T[j].score += 1;
}
}
}
}
//交换队伍排位 for排序
void swap_team(team *a, team *b)
{
team tmp;
memcpy(&tmp, a, sizeof(team));
memcpy(a, b, sizeof(team));
memcpy(b, &tmp, sizeof(team));
}
void print_team(team T)
{
printf("team[%d].score = %d\n", T.team_num, T.score);
printf("team[%d].winballs = %d\n", T.team_num, T.win_balls);
printf("team[%d].loseballs = %d\n", T.team_num, T.lose_balls);
printf("\n");
}
//比较队伍排名先后
int compare(team a, team b)
{
if(a.score > b.score)
{
return BIGGER;
}
else if(a.score < b.score)
{
return SMALLER;
}
else
{
if((a.win_balls - a.lose_balls) > (b.win_balls - b.lose_balls))
{
return BIGGER;
}
else if((a.win_balls - a.lose_balls) < (b.win_balls - b.lose_balls))
{
return SMALLER;
}
else
{
if(a.win_balls > b.win_balls)
{
return BIGGER;
}
if(a.win_balls > b.win_balls)
{
return SMALLER;
}
else
{
return EQUAL;
}
}
}
}
//给队伍冒泡排序
void Bubble_Sort(team *T)
{
int i, j;
for(i = 0;i < 9;i++)
{
for(j = 0;j < 9 - i; j++)
{
if(SMALLER == compare(T[j], T[j+1]))
{
swap_team(&T[i], &T[j]);
}
}
}
}
int main()
{
int i,j;
team teams[10] = {0};
team_init(teams);
team_input_score(teams);
Bubble_Sort(teams);
for(i =0;i<10;i++)
{
print_team(teams[i]);
}
return 0;
}