1080 MOOC期终成绩(C语言)

设计思路:
  • 似乎又是一道排序题,处理好排序即可
编译器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct score {
        char name[21];
        int program;
        int mid;
        int final;
        int total;
};

int cmp_sort_total(const void *a, const void *b)
{
        struct score *s1 = (struct score *)a;
        struct score *s2 = (struct score *)b;
        if (s2->total - s1->total)
                return s2->total - s1->total;
        return strcmp(s1->name, s2->name);
}

int cmp_sort_name(const void *a, const void *b)
{
        struct score *s1 = (struct score *)a;
        struct score *s2 = (struct score *)b;
        return strcmp(s1->name, s2->name);
}

int cmp_bsearch(const void *strptr, const void *scoreptr)
{
        struct score *s = (struct score *)scoreptr;
        char *name = (char *)strptr;
        return strcmp(name, s->name);
}

int main()
{
        struct score scores[10000], *s = scores;
        int p, m, n;
        int grade, sum = 0;
        int i;
        char name[21];

        scanf("%d %d %d", &p, &m, &n);

        for (i = 0; i < p; i++) {
                scanf("%s %d", name, &grade);
                if (grade >= 200) {
                        strcpy(s->name, name);
                        s->program = grade;
                        s->mid = -1;
                        s->final = -1;
                        s->total = 0;
                        sum++;
                        s++;
                }
        }

        qsort(scores, sum, sizeof(scores[0]), cmp_sort_name);

        for (i = 0; i < m; i++) {
                scanf("%s %d", name, &grade);
                s = bsearch(name, scores, sum, sizeof(scores[0]), cmp_bsearch);
                if (s != NULL)
                        ((struct score *)s)->mid = grade;
        }
        for (i = 0; i < n; i++) {
                scanf("%s %d", name, &grade);
                s = bsearch(name, scores, sum, sizeof(scores[0]), cmp_bsearch);
                if (s != NULL) {
                        s->final = grade;

                        if (s->final >= s->mid)
                                s->total = s->final;
                        else
                                s->total = s->final * 0.6 + s->mid * 0.4 + 0.5;
                }
        }

        qsort(scores, sum, sizeof(scores[0]), cmp_sort_total);

        for (s = scores, i = 0; i < sum && s->total >= 60; s++)
                printf("%s %d %d %d %d\n", s->name, s->program,
                        s->mid, s->final, s->total);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值