程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU1009 FatMouse Trade

HDU1009 FatMouse Trade

編輯:C++入門知識

                FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33703    Accepted Submission(s): 10981

 

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 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1


Sample Output
13.333
31.500


Author
CHEN, Yue
 


Source
ZJCPC2004
 


Recommend
JGShining


解題思路:本題為典型貪心算法題目,由於物品可分割,不能用01背包做。
      先對給出的各個倉庫的信息(豆子量,所需貓糧量)進行分析,可知倉庫中豆子越多,所需貓糧越少,則越劃得來,兌換該房間的豆子可以得到最大的豆子量。
      因此,先求出各個倉庫的豆子量/所需貓糧量的比值(簡稱比值),比值大的應該考慮優先兌換。
      然後將所有倉庫信息按照比值從大到小排序,得出各倉庫的兌換先後順序,存儲在結構體數組food[]裡面,准備兌換。
      然後按排序結果對相應倉庫進行兌換,若當前所剩貓糧量不為0並且還有倉庫未進行兌換,則繼續兌換,
                (1)如果當前老鼠剩下的貓糧大於兌換當前倉庫所有的豆子的所需的貓糧量,則兌換該倉庫的所有豆子,豆子總量增加該倉庫總豆子量的值,所剩貓糧總量減去兌換當前倉庫所有豆子所需貓糧量;
                (2)如果當前老鼠剩下的貓糧小於兌換當前倉庫所有的豆子的所需的貓糧量,則兌換該倉庫的所有豆子*所剩貓糧/所需的貓糧量,豆子總量增加所有豆子*所剩貓糧/所需的貓糧量(注意精度,這裡的值可能會產生小數),所剩貓糧總量置0;
       最後,按題目要求輸出兌換所得豆子總量(保留3位小數)即可。


 

include<stdio.h>   
#include<algorithm>   
using namespace std;  
struct node  
{  
    int j;  
    int f;  
    double bi;  
}food[1005]; //兌換情況,豆子量,所需貓糧,豆/貓比   
bool cmp(node a,node b)    //排序,按比例從大到小排序   
{  
    return a.bi>b.bi;  
}  
int main()  
{  
    int m,n;  
    int i,j;  
    while(scanf("%d%d",&m,&n)&&(n!=-1||m!=-1))  
    {  
        for(i=0;i<n;i++)  
        {  
            scanf("%d%d",&food[i].j,&food[i].f);  
            food[i].bi=(double)food[i].j/food[i].f;  
        }  
        sort(food,food+n,cmp);  
        double sum=0;  
        i=0;  
        while(m&&i<n)   //當貓糧還有,豆子沒有兌換完時,繼續兌換   
        {  
            if(m>=food[i].f)  //若當前貓糧能兌換當前倉庫所有豆子,則全部兌換   
            {  
                sum+=food[i].j;  
                m-=food[i].f;  
            }  
            else   //若當前貓糧無法兌換當前倉庫所有貓糧,則按比例兌換   
            {  
                sum+=(double)m/food[i].f*food[i].j;  //注意精度哦   
                m=0;  //貓糧用完了   
            }  
            i++;  //下一個房間(已按豆/貓比排序)比例不大於已兌換房間,且不小於所有未兌換的房間   
        }  
        printf("%.3lf\n",sum);  
    }  
    return 0;  
}  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved