public class sumCnt {
public static void main(String[] args){
int[] a = {1, 2, 4, 3, 5};
System.out.println(f(a.length - 1, 4, a));
}
// n: 遍历数组a下标,从0到a.length-1;
public static int f(int n, int m, int[] a){
if(n == 0){
return a[n] == m ? 1: 0;
}
if(a[n] == m){
return f(n-1, m, a) + 1;
}else if(a[n] < m){
return f(n-1, m, a) + f(n-1, m - a[n], a);
}else{
return f(n-1, m, a);
}
}
}
仅供参考哈!