题目:
给你六种面额 1、5、10、20、50、100 元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0~10000的非负整数)的不同组合的个数。
输入:100
输出:333
package com.test;
import java.util.Scanner;
import java.util.Arrays;
/**
* 解题思路:只有1元钱时,只有1元和5元钱时;
* @author chengxin
*
*/
public class Test2 {
public static long count(int n) {
int coins[] = { 1, 5, 10, 20, 50, 100 };
int h = coins.length;
long dp[][] = new long[h][n + 1];
Arrays.fill(dp[0], 1);
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
System.out.print(dp[i][j]);
}
System.out.println();
}
for (int i = 1; i < h; i++) { //表示从1元到100元纸币 h=6
for (int j = 1; j <= n; j++) {//组成N元依次循环 n=5
int m = j / coins[i];//组成N元时最多可用多少种相应的纸币
for (int k = 0; k <= m; k++) {
dp[i][j] = dp[i][j] + dp[i - 1][j - k * coins[i]];//表示组合次数
}
//System.out.print(dp[i][j]+" ");
}
//System.out.println();
}
return dp[h - 1][n];
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
long res = count(n);
System.out.println(res);
}
}
}