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();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章