Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions mainPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.geekbrains;

//
// N ->
// / F(N-1) , N mod 2 != 0
// F(N) = { F(N-1) + F(N/2), N mod 2 = 0
// \ 1, N = 2
//
//


// static int F(int a, int b) {
// if (b == a)
// return 1;
// else if (b < a) {
// return 0;
// } else if (b % 2 != 0)
// return F(a, b - 1);
// else
// return F(a, b - 1) + F(a, b / 2);
// }
//} РЕКУРСИЯ

public class mainPart {
public static void main(String[] args) {
int startNum = 2;
int finishNum = 7;

int[][] routes = methods.createRoutes(startNum, finishNum);
methods.show2dArray(routes);

int numberRoutes = methods.getNumberRoutes(routes);

System.out.printf("Количество возможных способов получить из числа %d число %d,\n", startNum, finishNum);
System.out.printf("используя операции прибавления 1 и умножение на 2 равно %d", numberRoutes);
}

}
37 changes: 37 additions & 0 deletions methods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.geekbrains;

public class methods {
static int[][] createRoutes(int from, int to) {
int[][] routes = new int[2][to - from + 1];
routes[0][0] = from;
routes[1][0] = 1;

for (int i = 0; i < routes.length; i++) {
for (int j = 1; j < routes[i].length; j++) {
if (i == 0)
routes[i][j] = routes[i][j - 1] + 1;
else {
routes[i][j] = routes[i][j - 1];
// если число в первой строке четное и его половина больше стартового значения
if (routes[i - 1][j] % 2 == 0 && routes[i - 1][j] / 2 >= from)
routes[i][j] += routes[i][routes[i - 1][j] / 2 - from];
}
}
}
return routes;
}

static int getNumberRoutes(int[][] array) {
return array[1][array[0].length - 1];
}

static void show2dArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("%d \t", array[i][j]);
}
System.out.println();
}
}

}