PAT ~ L2-009. 抢红包 (模拟,结构体排序)

思路:开一个结构体数组去统计每个人(+收到/-发出)的红包,每个人的编号,每个人抢到的红包数量,然后按要求排序输出即可。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 5;
int N, k, n;
double p, sum;
struct People
{
    double money;
    int id, cnt;
}a[MAXN];
bool cmp(People a, People b)
{
    if (a.money != b.money) return a.money > b.money;
    if (a.cnt != b.cnt) return a.cnt > b.cnt;
    return a.id < b.id;
}
int main()
{
    memset(a, 0, sizeof(a));
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
    {
        a[i].id = i;
        scanf("%d", &k);
        sum = 0.0;
        while (k--)
        {
            scanf("%d%lf", &n, &p);
            a[n].money += p;
            a[n].cnt++;
            sum += p;
        }
        a[i].money -= sum;
    }
    sort(a + 1, a + N + 1, cmp);
    for (int i = 1; i <= N; i++)
    {
        printf("%d %.2f\n", a[i].id, a[i].money / 100.0);
    }
    return 0;
}
/*
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
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值