码迷,mamicode.com
首页 > 其他好文 > 详细

7-9 抢红包

时间:2020-02-16 12:40:28      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:using   scan   bit   order   return   struct   std   double   while   

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

#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 抢红包

标签:using   scan   bit   order   return   struct   std   double   while   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!