首页 > 其他 > 详细

7-9 抢红包

时间:2020-02-16 13:42:03      阅读:63      评论:0      收藏:0      [点我收藏+]

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

#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;
}

7-9 抢红包

原文:https://www.cnblogs.com/xyqxyq/p/12316236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!