朋友们,帮忙看个题啊
怎么计算每个队相应得分的呢?需要用指针数组来储存队名吗,队名和比赛记录需要分别用两个数组来储存吗?


朋友们,帮忙看个题啊
怎么计算每个队相应得分的呢?需要用指针数组来储存队名吗,队名和比赛记录需要分别用两个数组来储存吗?


实现步骤和详细代码实现如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_TEAM_NAME_LEN 50
#define MAX_TEAM_NUM 1000
typedef struct team_s
{
char name[MAX_TEAM_NAME_LEN+1];
int score;
int net_score;
}team_t;
int compare(const void *a, const void *b)
{
team_t *ta = (team_t *)a;
team_t *tb = (team_t *)b;
if (ta->score != tb->score)
return tb->score - ta->score;
if (ta->net_score != tb->net_score)
return tb->net_score - ta->net_score;
return strcmp(ta->name, tb->name);
}
int main()
{
int n;
scanf("%d", &n);
team_t teams[MAX_TEAM_NUM];
for (int i = 0; i < n; ++i)
scanf("%s", teams[i].name);
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
char name1[MAX_TEAM_NAME_LEN+1], name2[MAX_TEAM_NAME_LEN+1];
int score1, score2;
scanf("%s", name1);
char c = getchar();
while (c != '-') c = getchar();
scanf("%s", name2);
c = getchar();
while (c != ':') c = getchar();
scanf("%d-%d", &score1, &score2);
int index1 = -1, index2 = -1;
for (int k = 0; k < n; ++k)
{
if (strcmp(teams[k].name, name1) == 0)
index1 = k;
if (strcmp(teams[k].name, name2) == 0)
index2 = k;
if (index1 != -1 && index2 != -1)
break;
}
teams[index1].score += (score1 > score2 ? 3 : (score1 == score2 ? 1 : 0));
teams[index2].score += (score2 > score1 ? 3 : (score1 == score2 ? 1 : 0));
teams[index1].net_score += (score1 - score2);
teams[index2].net_score += (score2 - score1);
}
}
qsort(teams, n, sizeof(team_t), compare);
for (int i = 0; i < n / 2; ++i)
printf("%s\n", teams[i].name);
return 0;
}