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