HDU 1009 FatMouse' Trade Java解决

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

问题分析:解题思想就是先建立仓库类,类成员包括所拥有的Javabean的数量即j,还有兑换这个仓库的Javabean所需要的猫粮的数量即f,还有记录在兑换过程中这个仓库已经兑换了的Javabean数量。然后通过冒泡排序法按照每个仓库的Javabean的性价比进行排序,因为要想兑换的Javabean最多,必须优先兑换性价比最高的。然后每次兑换性价比最高的,直至Javabean或者猫粮兑换完为止,最后输出结果。其实核心思想就是贪心法,每次兑换都兑换能兑换到最多的优先。

import java.util.Scanner;
class Test{//仓库类,用来保存这个仓库的数据
    private int j;//所能兑换到的Javabean数量
    private int f;//所需的猫粮数
    private double r;//这个仓库已经兑换的Javabean数量
    public void setr(double r) {
        this.r=r;
    }
    public double getr() {
        return this.r;
    }
    public Test(int j,int f) {
        this.j=j;
        this.f=f;
    }
    public int getj() {
        return this.j;
    }
    public int getf() {
        return this.f;
    }
}
public class Main {
    public void BubbleSort(Test test[]) {//冒泡排序,按性价比从低到高排序
        for(int i=0;i<test.length-1;i++) {
            for(int j=0;j<test.length-1-i;j++) {
                double x=0;
                x=(double) test[j].getj()/test[j].getf();
                double y=0;
                y=(double) test[j+1].getj()/test[j+1].getf();
                    if(x>y) {//比较性价比
                    int J=test[j].getj(),F=test[j].getf();
                    test[j]=new Test(test[j+1].getj(),test[j+1].getf());
                    test[j+1]=new Test(J,F);
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner in=new Scanner (System.in);
        int m,n;
        while(in.hasNext()) {
            m=in.nextInt();  n=in.nextInt();
            if(m==-1&&n==-1)
                break;
            int total=0;//用来保存已经兑换了的猫粮数量
            Test test[]=new Test[n];
            for(int i=0;i<n;i++) {
                int j=in.nextInt();
                int f=in.nextInt();
                test[i]=new Test(j,f);
            }
            Main fat=new Main();
            fat.BubbleSort(test);
            int temp=test.length-1;
            int y=m;
            while(total<y&&m>0) {
                if(temp<0)
                    break;
                if(test[temp].getf()>=m) {
                    double w=0;
                    w=(double) (m*test[temp].getj())/test[temp].getf();
                    test[temp].setr(w);
                    total=y;
                }
                else {
                    total+=test[temp].getf();
                    test[temp].setr(test[temp].getj());
                    m=m-test[temp].getf();
                }
                temp--;
            }
            double r=0;//用来保存所兑换回来的Javabean总数
            for(int i=0;i<test.length;i++)//循环相加
            {
                r+=test[i].getr();
            }
            System.out.println(String.format("%.3f",r ));
        }
        in.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值