java编程题(求代码)

来源:百度知道 编辑:UC知道 时间:2024/06/11 01:03:08
编写一个程序,找出组成给定金额(无须用户输入)所需的货币的最小数量(假定货币的最小面值是500,100,50,20,5,2和1卢比)。
如果给定的输入为638,
则程序应打印:
500*1 = 500
100*1 = 100
20*1 = 20
10*1 = 10
5*1 = 5
2*1 = 2
1*1 = 1

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
calc(new Scanner(System.in).nextInt(), 0);
}

static int[] allFaceValues = { 500, 100, 50, 20, 10, 5, 2, 1 };

private static void calc(int amount, int times) {
if (amount > 0) {
int faceValue = allFaceValues[times];
int q = amount % faceValue;
int d = amount / faceValue;
if (d != 0)
System.out.println(faceValue + "*" + d + "=" + faceValue * d);
calc(q, ++times);
}
}
}