java algorithm interview questions

Please write the java code, implement the method, and calculate how many combinations there are for the number 100.

(Assuming that the numbers that can be used in combination include 100, 50, 20, 10, 5, 2, 1), for example, if M is 1, there are 1 combination methods, that is, 1*1;

If M is 2, there are 2 combinations, 2*1 and 1*2;

Find the number of return combination methods.

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("There are " + count + " methods!");
	}

	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);
		}
	}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847572&siteId=291194637