请写出java代码,实现一下方法,计算对于数字100,共有多少种组合方法。
(假设组合时可以使用的数字包括100,50,20,10,5,2,1),比如如果M为1,共有1中组合方法,即1*1个;
如果M为2,则共有2种组合方式,2*1个 和1*2个;
求出返回组合方法的个数。
package org.son; public class Mytest { private static int count; public static void main(String args[]) { int max = 10; int[] cents = { 100, 50, 20, 10, 5, 2, 1 }; collectMoney(cents, 0, max, 0); System.out.println("总共有" + count + "种搭配方法!"); } public static void collectMoney(int[] cents, int beginIndex, int max, int result) { if (result >= max) { if (result == max) { count++; } return; } for (int i = beginIndex; i < cents.length; i++) { int cent = cents[i]; collectMoney(cents, i, max, result + cent); } } }