A mouse liked cheese, but it was put in N rooms, and there was a cat guard in these rooms. Now it is ready to trade with the cats. It has m-pound cat food and wants to use this m-pound cat food for cheese. In every room guarded by a cat, there is a cheese J [I] pound, and the cat needs f [I] pound food. If the teacher gives the cat f [I] * a % of cat food, then it can get J [I] * A % cheese. Now we know the demand for cat food and the number of cheese in each room for each cat. How can a mouse get the most cheese?
Input
Enter M and N in the first line, followed by N rows (the number of cheeses and the demand for cat food in each room ). The program is terminated when m and n are-1 and-1.
Output
The branch outputs the maximum number of cheese obtained by the mouse and retains three decimal places.
Sample Input
5 37 24 35 220 325 1824 1515 10-1 -1
Sample output
13.33331.500
#include <stdlib.h>#include <stdio.h>int main(){ int m; int n; int k; int i; int a[1001],b[1001]; double sum=0; double p[1001]; // double max = 0; while(scanf("%d %d",&m,&n)&&m!=-1&&n!=-1) { sum=0; // for(i=0;i<n;i++) { scanf("%d %d",&a[i],&b[i]); p[i]=(double)a[i]/b[i]; } while(m!=0&&n!=0) { max=0; for(i=0;i<n;i++) { if(p[i]>max) { max=p[i]; k=i; } } if(m-b[k]>=0) { m=m-b[k]; sum+=a[k]; p[k]=0; } else { sum+=(double)p[k]*m; m=0; } } printf("%.3lf\n",sum); } return 0;}
Transactions between mice and cats