题目如下:
FatMouse prepared M pounds of cat food, ready to trade with thecats guarding the warehouse containing his favorite food, JavaBean.
Thewarehouse has N rooms. The i-th room contains Jii pounds of JavaBeans and
requires Fii pounds of cat food. FatMouse does not have to trade for all theJavaBeans
in the room, instead, he may get Jii*a% pounds of JavaBeans if he pays Fii 。
------------------------------------------------------------------------------------------------------------------------------------------------
输入如下:
The input consists of multiple test cases. Each test casebegins with a line containing two non-negative integers M and N. Then N linesfollow, each contains two non-negative integers Jii and Fii respectively. The last test case isfollowed by two -1's. All integers are not greater than 1000.
------------------------------------------------------------------------------------------------------------------------------------------------
输出如下:
For each test case, print in a single linea real number accurate up to 3 decimal places, which is the maximum amount ofJavaBeans that FatMouse can obtain.
------------------------------------------------------------------------------------------------------------------------------------------------
思路就是简单的贪心算法就可以了。
------------------------------------------------------------------------------------------------------------------------------------------------
代码如下:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct node { int m; int n; }; bool cmp(node n,node m) { return n.n*1.0/n.m>m.n*1.0/m.m; } struct node a[1010]; int n; int m; int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n<0 && m<0) break; for(int i=0;i<m;i++) { scanf("%d%d",&a[i].n,&a[i].m); } sort(a,a+m,cmp); double sum = 0; for(int i=0;i<m;i++) { if(n>=a[i].m) { n = n-a[i].m; sum+=a[i].n; } else { sum+=n*(a[i].n*1.0/a[i].m); break; } } printf("%.3f\n",sum); } return 0; }------------------------------------------------------------------------------------------------------------------------------------------------