2

我必须编写一个 Java 程序来告诉从 1 美分到 99 美分的任何数量的零钱应该派出什么硬币。例如,如果金额为 86 美分,则输出将类似于以下内容:

86 美分可以分为 3 个季度、1 毛钱和 1 便士。

使用面额为 25、10、5 和 1 的硬币。您的程序将使用以下方法(以及其他方法):

public static int computeCoin(int coinValue,);
// Precondition: 0 < coinValue < 100; 
// Postcondition: returned value has been set equal to the maximum 
//number of coins of the denomination coinValue cents that can be 
//obtained from amount (a different variable) cents. amount has been 
//decreased by the value of the coins, that is, decreased by     
//returnedValue*coinValue.

到目前为止,这就是我所拥有的,但我想我想念的更多,有人可以帮帮我吗?而且我也不应该使用双打代替int。

public class Assignment6{
   public static void main(String [] args){
   amount = (int)(Double.parseDouble(args[0])*100);

   System.out.println("Five: " + computeCoin(500));
   System.out.println("one: " + computeCoin(100) );
   System.out.println("Q : " + computeCoin(25) );
   System.out.println("D : " + computeCoin(10) );
   System.out.println("N : " + computeCoin(5) );
   System.out.println("P : " + computeCoin(1) );
}
4

3 回答 3

4
public class Assignment6 {
    private static int amount = 0;
    public static void main(String[] args) {
        amount = (int)(Double.parseDouble(args[0])*100);
        System.out.println("Five: " + computeCoin(500));
        System.out.println("one: " + computeCoin(100) );
        System.out.println("Q : " + computeCoin(25) );
        System.out.println("D : " + computeCoin(10) );
        System.out.println("N : " + computeCoin(5) );
        System.out.println("P : " + computeCoin(1) );
    }

    public static int computeCoin(int cointValue) {
        int val = amount / cointValue;
        amount -= val * cointValue;
        return val;
    }
}

这里的技巧在于computeCoin方法,事实上除法是整数除法,因此val将保存coinValue总价值不超过的给定值 ( )的硬币的“最大”数量amount

于 2012-06-21T17:24:10.543 回答
0

像这样?

public class Assignment6 {
    public static int amount;
    public static int amountPreserv;

    public static void main(String[] args) {
        amount = (int) (Double.parseDouble(args[0]) * 100);
                amountPreserv = amount;

        System.out.println("Five: " + computeCoin(500));
        System.out.println("one: " + computeCoin(100));
        System.out.println("Q : " + computeCoin(25));
        System.out.println("D : " + computeCoin(10));
        System.out.println("N : " + computeCoin(5));
        System.out.println("P : " + computeCoin(1));
        System.out.println("Value inputed : " + amountPreserv);
    }

    private static int computeCoin(int i) {
        int cont = 0;
        while (amount > i) {
            amount -= i;
            cont++;
        }
        return cont;
    }
}
于 2012-06-21T17:23:05.183 回答
0

最重要的一点是,做出改变是一种贪心算法,这意味着在任何给定时间让你最接近目标的选项是最有效的选择。因此,对于任何数量和任何面额,最有效的算法应该是这样的:

int total;
int[] denom = { w, x, y, z };
int[] count = new int[denom.length]
int i = 0;
while (i < denom.length && total > 0) {
  while ( total >= denom[i] )
  {
    total -= denom[i];
    count[i]++;
  }
  i++
}

编辑:任何教派实际上都有点野心。只有你有一个最低面额,才能保证每次都能做出改变。

于 2012-06-21T22:11:12.177 回答