uva 10194 Football (字符结构体排序)

读懂题目以后就不难,相关的题目也做了很多了。

将一个结构体按照题目要求的各种要求来排序,最后输出。


需要注意的一个地方是memset()那里,开始死活不对,竟然是那个地方无法赋初值。。。


与快排区分开!!!

qsort(title, count, sizeof(   title[0]    ),cmp);

memset(team, 0, sizeof(    team   ));


代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Team
{
    char name[50];
    int b, c, d, e, f, g, h, i;
} team[50];

int nteams;
int ngames;

int find(char str[])
{
    for (int i = 0; i < nteams; i++)
        if (strcmp(team[i].name, str) == 0)
            return i;
    return 0;
}

void uptolow(char str[])
{
    int len = strlen(str);
    for (int i = 0; i < len; i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')
        {
            str[i] += 32;
        }
    }
}

int cmp(const void *va, const void *vb)
{
    struct Team *a = (Team*)va;
    struct Team *b = (Team*)vb;
    if (a -> b != b -> b)
        return b -> b - a -> b;
    if (a -> d != b -> d)
        return b -> d - a -> d;
    if (a -> g != b -> g)
        return b -> g - a -> g;
    if (a -> h != b -> h)
        return b -> h - a -> h;
    if (a -> c != b -> c)
        return a -> c - b -> c;
    char ta[50], tb[50];
    strcpy(ta, a -> name);
    strcpy(tb, b -> name);
    uptolow(ta);
    uptolow(tb);
    return strcmp(ta, tb);
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int n;
    scanf("%d", &n);
    getchar();
    while (n--)
    {

        char cup[105];
        gets(cup);

        scanf("%d", &nteams);
        getchar();
        memset(team, 0, sizeof(team));//!!sizeof(team[0]),是错的
        for (int i = 0; i < nteams; i++)
            gets(team[i].name);

        scanf("%d", &ngames);
        getchar();
        for (int i = 0; i < ngames; i++)
        {
            char team1[50], team2[50];
            int ag, bg;//a, b 进的球
            char ch;
            int tmp = 0;
            while (ch = getchar())
            {
                if (ch == '#')
                    break;
                team1[tmp++] = ch;
            }
            team1[tmp] = '\0';
            scanf("%d", &ag);
            getchar();
            scanf("%d", &bg);
            getchar();
            gets(team2);
            //printf("----%s#%d@%d#%s\n", team1, w, l, team2);
            //找到对应队伍的下标
            int ateam = find(team1);
            int bteam = find(team2);
            team[ateam].c += 1;
            team[bteam].c += 1;

            team[ateam].h += ag;
            team[bteam].h += bg;

            team[ateam].i += bg;
            team[bteam].i += ag;

            team[ateam].g = team[ateam].h - team[ateam].i;
            team[bteam].g = team[bteam].h - team[bteam].i;

            if (ag > bg)
            {
                team[ateam].b += 3;
                team[ateam].d += 1;
                team[bteam].f += 1;
            }
            else if (ag == bg)
            {
                team[ateam].b += 1;
                team[bteam].b += 1;

                team[ateam].e += 1;
                team[bteam].e += 1;
            }
            else
            {
                team[bteam].b += 3;
                team[bteam].d += 1;
                team[ateam].f += 1;
            }
        }

        qsort(team, nteams, sizeof(team[0]), cmp);

        printf("%s\n", cup);
        for (int i = 0; i < nteams; i++)
        {
            printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",
                   i+1, team[i].name, team[i].b, team[i].c, team[i].d,
                   team[i].e, team[i].f, team[i].g, team[i].h, team[i].i);
        }
        if (n > 0)
            printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值