给你六种面额 1、5、10、20、50、100 元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0~10000的非负整数)的不同组合的个数。
输入描述:
输入包括一个整数n(1 ≤ n ≤ 10000)
输出描述:
输出一个整数,表示不同的组合方案数
这道题的思路是用动态规划思想,先考虑只用面额1表示有多少种组合,然后考虑只用面额1和5表示有多少种组合,这时只需要把前面计得的数加上用面额为5表示的组合数即可得到,依此类推。时间复杂度为O(n),空间复杂度也为O(n)。
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static long count(int n){
if(n <= 0)return 0;
int[] coins = new int[]{1,5,10,20,50,100};
long[] dp = new long[n+1];
dp[0] = 1;
for(int i = 0; i < coins.length; i++) {
for(int j = coins[i]; j <= n; j++) {
dp[j] = dp[j] + dp[j - coins[i]];//类似斐波那契 后者的种类数基于前者
}
}
return dp[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);
}
}
}`