2010年中兴面试题
编程求解:
输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来.
public class c {
/*
* 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
*/
public static void main(String[] args) {
int m = 10;
int n = 3;
num(m, n);
System.out.println(numbers);
}
static int numbers = 0;
public static void num(int m, int n) {
if (n > 0 && m>0) {
int j = 0;
for (int i = m / n; i >= 0; i--) {
j = m - i * n;
if (j == 0)
numbers ++;
else
num(j, n - 1);
}
}
}
}
输出结果为14