Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1.  
  2. public class rec {
  3.     final static int N = 3;// max depth
  4.  
  5.     public static void main(String[] args) {
  6.         rec13(13, 1, 0);
  7.         // expected output 3+1+1+1+1;3+3+1+;5+1+1;7;
  8.     }
  9.  
  10.     public static int rec13(int sum, int k, int depth) {
  11.         if (depth > N)// проверка на колличество цифр в сумме
  12.             return -1;
  13.         if (sum < 0)// если залезли в минус
  14.             return -1;
  15.         if (sum == 0) {// желанная последовательность найдена
  16.             System.out.print("\n" + k);
  17.             return 0;
  18.         }
  19.         for (int i = k; i <= sum; i = i + 2) {// Развлетляет рекурсию
  20.             int res = rec13(sum - i, i, depth + 1);// строим дерево с текущим i
  21.             if (res == 0 && depth > 1)// ловим последнее число
  22.             {
  23.                 System.out.print(" " + k);
  24.                 return 0;
  25.             } else if (res == 0 && depth == 1)// споймали
  26.                 System.out.print(" " + k);
  27.         }
  28.         return -1;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement