7-9 抢红包

这题用到了浮点数的比较,如果直接比较的话会出错。

#include <bits/stdc++.h>
using namespace std;

const double EPS=1e-6;
struct Node {
    int id;
    double money=0;
    int cnt=0;
}node[1005];

bool cmp(Node& a,Node& b){
    if (fabs(a.money-b.money)>EPS) {
        return a.money>b.money;
    }
    if (a.cnt!=b.cnt){
        return a.cnt>b.cnt;
    }
    return a.id<b.id;
}

int main()
{
    int n,k;
    int id,p;
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        scanf("%d",&k);
        node[i].id=i;
        double sum=0;
        unordered_map<int,int> ump;
        while (k--) {
            scanf("%d%d",&id,&p);
            if (!ump[id]) {
                node[id].cnt++;
                node[id].money+=p/100.0;
                sum+=p/100.0;
                ump[id]=1;
            }
        }
        node[i].money-=sum;
    }
    sort(node+1,node+n+1,cmp);
    for (int i=1;i<=n;i++) {
        printf("%d %.2f\n",node[i].id,node[i].money);
    }
    return 0;
}

posted @ 2020-02-16 12:02  xyee  阅读(183)  评论(0编辑  收藏  举报