HDOJ---ACMSteps---1.3.1FatMouse' Trade

来源:互联网 发布:携程数据分析师笔试题 编辑:程序博客网 时间:2024/04/20 13:48

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2879 Accepted Submission(s): 889 
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
 
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
 
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 
Sample Input
5 37 24 35 220 325 1824 1515 10-1 -1
 
Sample Output
13.33331.500
 
#include<iostream>#include<iomanip>using namespace std;struct S{    int java;    int food;    double aver;} unit[1000], temp;int main(void){    int m, n, i, j;    double sum;    bool k;    while ( (cin>>m>>n) && m!= -1 && n != -1)    {        sum = 0;        for (i = 0; i< n; i ++)        {            cin>>unit[i].java>>unit[i].food;            unit[i].aver = static_cast<double>(unit[i].java)/unit[i].food;        }        for(i = n - 1, k = true; i >= 0 && k; i --)        {            k = false;            for (j = 0; j < i; j ++)            {                if (unit[j].aver < unit[j + 1].aver)                {                    temp = unit[j];                    unit[j] = unit[j + 1];                    unit[j + 1] = temp;                    k = true;                }            }        }        for (i = 0; i < n && m != 0; i ++)        {            if (m >= unit[i].food)            {                m -= unit[i].food;                sum += unit[i].java;            }            else            {                sum += unit[i].aver * m;                m = 0;            }        }        cout<<fixed<<setprecision(3)<<sum<<endl;    }    return 0;}


原创粉丝点击