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

HDUOJ 悼念512汶川大地震遇难同胞——老人是真饿了

时间:2018-09-15 10:35:35      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:algorithm   stream   include   weight   algo   nbsp   using   std   bsp   

贪心算法~

#include<iostream>
#include<stdio.h>
#include<algorithm> 
#include<string.h>
#include <iomanip>
using namespace std;

struct rice {
    int price;
    int weight;
}r[1001];

bool cmp(rice a, rice b) {
    return a.price < b.price;
}

int main() {
    int c = 0;
    while (cin >> c) {
        while (c--) {
            int m, n = 0;
            int k = 0;
            double sum = 0;
            cin >> n >> m;
            double money = (double)n;
            for (int i = 0; i < m; i++) {
                cin >> r[i].price >> r[i].weight;
            }
            sort(r, r + m, cmp);
            while (money > 0.0) {
                if ((double)r[k].price*r[k].weight >= money)
                {
                    sum += money / (double)r[k].price;
                    money = 0.0;
                }
                else {
                    sum += (double)r[k].weight;
                    money -= (double)(r[k].price * r[k].weight);
                    k++;
                }
            }
            cout << setprecision(2) << fixed << sum << endl;
        }
    }
}

 

HDUOJ 悼念512汶川大地震遇难同胞——老人是真饿了

标签:algorithm   stream   include   weight   algo   nbsp   using   std   bsp   

原文地址:https://www.cnblogs.com/DaiShuSs/p/9650111.html

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