1、给你六种面额 1、5、10、20、50、100 元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0~10000的非负整数)的不同组合的个数。
输入描述:
输入包括一个整数n(1 ≤ n ≤ 10000)
输出描述:
输出一个整数,表示不同的组合方案数
输入例子1:
1
输出例子1:
1
这是牛客网上某大佬给出的解析,感觉就是一个不断递归的问题。
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); System.out.println(count(n)); } } public static long count(int n){ if(n<=0) return 0; long[] dp = new long[n+1]; dp[0] = 1; //这里int[]千万不要少了[]这个括号.... int[]coins = new int[]{1,5,10,20,50,100}; 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]; } }