PTA 抢红包

这里写图片描述
输入样例:

10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10

输出样例:

1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32


Code:

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

struct Node {
    int on;
    int cnt;
    double num;
};

int cmp(const void *a, const void *b)
{
    if (((*(struct Node *)a).num < (*(struct Node *)b).num) ||
    (((*(struct Node *)a).num == (*(struct Node *)b).num) &&
    ((*(struct Node *)a).cnt < (*(struct Node *)b).cnt)) ||
    (((*(struct Node *)a).num == (*(struct Node *)b).num) &&
    ((*(struct Node *)a).cnt == (*(struct Node *)b).cnt) && 
    ((*(struct Node *)a).on > (*(struct Node *)b).on))) {
        return 1;
    }
    else {
        return -1;
    }
}

int main()
{
    int N, k, i, n, on;
    double num;
    scanf("%d", &N);
    struct Node *s = (struct Node *)malloc (sizeof(struct Node) * (N+1));
    memset(s, 0, sizeof(struct Node) * (N+1));
    for(k = 1; k <= N; k ++) {
        double sum = 0;
        scanf("%d", &n);
        for(i = 0; i < n; i ++) {
            scanf("%d %lf", &on, &num);
            sum += num;
            s[on].cnt ++;
            s[on].num += num/100;
        }
        s[k].on = k;
        s[k].num -= sum/100;
    }
    qsort(s + 1, N, sizeof(s[0]), cmp);
    for(i = 1; i <= N; i ++) {
        printf("%d %.2f\n", s[i].on, s[i].num);
    }

    return 0;
}

如果对C语言的快排函数不熟悉的,可以点击以下链接:
qsort()函数基本使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值