YT02-简单数学课后题-1001 FatMouse' Trade-(5.31日-烟台大学ACM预备队解题报告)

79 篇文章 0 订阅

FatMouse' Trade

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 126   Accepted Submission(s) : 30
Font: Times New Roman | Verdana | Georgia
Font Size:  

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



145- 李晓凯
大体意思:
•题目描述:
•有N个房间,你有M镑的猫粮,在第i个房间你最多可以花费F[i]的猫粮来交换J[i]的豆子,交换可以按比例来,不一定全部交换,能在多个房间交换。求M镑最多能交换多少豆子。
•输入描述:
•输入数据有多组,每行有两个整数M和N,接下有N行,每行有两个整数J[i]和F[i]。当M=-1和N=-1时结束。所有整数不会大于1000。
•输出描述:
•输出能还得最大最大豆子数,保留三位小数。

解析:

•这是一个简单的贪心算法,部分背包问题,用结构体数组,以J/F比值升序排序即可。然后判断每个房间能不能全拿,如果能就M=M-F[i],sum+=J[i],不能就按照比例来取,在不能的时候Mouse用尽M的时候,这时候break就ok了,注意百分比计算的时候的强制转换。
• 比如:5个猫食,3个房间,第1个房间2个猫食换7个JavaBean,第2个房间3个猫食换4个JavaBean,第3个房间2个猫食换5个JavaBean;最大量的JavaBean是7+5+1*(4/3)=13.333。
•简单的贪心策略:每次以最小量的猫食换最大量的JavaBean,即每次取(J[i]/F[i])最大的J[i]将使换得的JavaBean量最大。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct Fun{
    int  CatFood,JavaBean;
    double ratio;
};
Fun t[10000],temp;
bool c(Fun a,Fun b){
   return a.ratio>b.ratio;
}
int main(){
    int m,n,i,j;
    while(scanf("%d%d",&m,&n)&&(m!=-1||n!=-1)){
        i=0;
while(i<n){
       cin>>t[i].JavaBean>>t[i].CatFood;        t[i].ratio=(double)t[i].JavaBean/t[i++].CatFood;
        }
        sort(t,t+n,c);
        double sum=0;
        for(i=0;i<n&&m!=0;i++){
                if(m>=t[i].CatFood){
                sum+=t[i].JavaBean;
                m-=t[i].CatFood;
            }
            else {
           sum+=m*((double)t[i].JavaBean/t[i++].CatFood);
                 break;
            }
        }
        printf("%.3f\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值