Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class rec {
- final static int N = 3;// max depth
- public static void main(String[] args) {
- rec13(13, 1, 0);
- // expected output 3+1+1+1+1;3+3+1+;5+1+1;7;
- }
- public static int rec13(int sum, int k, int depth) {
- if (depth > N)// проверка на колличество цифр в сумме
- return -1;
- if (sum < 0)// если залезли в минус
- return -1;
- if (sum == 0) {// желанная последовательность найдена
- System.out.print("\n" + k);
- return 0;
- }
- for (int i = k; i <= sum; i = i + 2) {// Развлетляет рекурсию
- int res = rec13(sum - i, i, depth + 1);// строим дерево с текущим i
- if (res == 0 && depth > 1)// ловим последнее число
- {
- System.out.print(" " + k);
- return 0;
- } else if (res == 0 && depth == 1)// споймали
- System.out.print(" " + k);
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement