fork(1) download
  1. class Test {
  2.  
  3. public static int compute(int m[],int k) { // array is 1 based! (the 0 element is ignored)
  4. return compute(m,m.length-1,k);
  5. }
  6.  
  7. public static int compute(int m[],int n,int k) { // array is 1 based! (the 0 element is ignored)
  8. if(n<k) return 0;
  9. else if(k==0) return 1;
  10. else if(k==n) {
  11. int x=1;
  12. for(int i=1;i<=k;i++) x*=m[i];
  13. return x;
  14. }
  15. else return compute(m,n-1,k) + m[n]*compute(m,n-1,k-1);
  16. }
  17.  
  18. public static void main(String[] args) throws Exception {
  19. int[] m = new int[]{0,4,3,2,1,1};
  20. int k=2;
  21. int res = compute(m,k);
  22. System.out.println("k=" +k +" combin=" + res+ " m_j=" +java.util.Arrays.toString(m));
  23. }
  24.  
  25. }
Success #stdin #stdout 0.02s 245632KB
stdin
Standard input is empty
stdout
k=2 combin=45 m_j=[0, 4, 3, 2, 1, 1]