Transactions between mice and cats

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.