杭电hdu 1009 FatMouse' Trade 贪心

来源:互联网 发布:js获取扫描枪 编辑:程序博客网 时间:2024/05/20 18:46

http://acm.hdu.edu.cn/showproblem.php?pid=1009

典型的贪心问题,很简单咯。

//贪心#include <iostream>using namespace std;#include <cstdio>#include <algorithm>#define MAX 1001typedef struct _node{int j;int f;double per;}node;node no[MAX];bool cmp(node a, node b){return a.per > b.per;}int main(){int n, m, j, f, i;double sum;while(scanf("%d%d", &n, &m)&&n!=-1&&m!=-1){sum = 0.0;for(i = 0; i < m; i ++){scanf("%d%d", &j, &f);no[i].j = j, no[i].f = f, no[i].per = j*1.0/f;}sort(no, no+m, cmp);for(i = 0; i < m; i ++){if(n - no[i].f >= 0){sum += no[i].j;n -= no[i].f;}else {sum += n*no[i].per;n = 0;break;}}printf("%.3lf\n", sum);}return 0;}