分析:通过输出格式的描述,我们可以知道,这道题是对最后处理好的数据进行排序输出。在输出前需要记录每个人的编号,抢红包的次数和金额,所以此处需要一个结构体,每个人的收入金额有该人抢的红包金额减去该人发的红包金额。所以这就是一道结构体排序。
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct People {
int card;
int num;
double money;
};
bool comp(People a, People b) {
if (a.money != b.money)return a.money>b.money;
else {
if (a.num != b.num)return a.num>b.num;
else return a.card<b.card;
}
}
int main() {
int N, K;
scanf("%d",&N);
int p, mon;
vector<People> a(N);
for (int i = 0; i<N; i++) {
a[i].num = 0;
a[i].card = i + 1;
a[i].money = 0;
}
for (int i = 0; i<N; i++) {
scanf("%d", &K);
for (int j = 0; j<K; j++) {
scanf("%d%d",&p,&mon);
a[p - 1].num++;
a[p - 1].money = a[p - 1].money + mon;
a[i].money = a[i].money - mon;
}
}
sort(a.begin(), a.end(), comp);
for (int i = 0; i<N; i++) {
printf("%d %.2f\n", a[i].card,a[i].money/100);
}
system("pause");
return 0;
}