task_1
This commit is contained in:
14
lr9/task_1/e1.java
Normal file
14
lr9/task_1/e1.java
Normal file
@@ -0,0 +1,14 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Main {
|
||||
public static void m(int x){
|
||||
System.out.println("x = " + x);
|
||||
if ((2 * x + 1) < 20){
|
||||
m(2 * x + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
m(1);
|
||||
}
|
||||
}
|
||||
14
lr9/task_1/e2.java
Normal file
14
lr9/task_1/e2.java
Normal file
@@ -0,0 +1,14 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Main {
|
||||
public static void m(int x){
|
||||
if ((2 * x + 1) < 20){
|
||||
m(2 * x + 1);
|
||||
}
|
||||
System.out.println("x = " + x);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
m(1);
|
||||
}
|
||||
}
|
||||
25
lr9/task_1/e3.java
Normal file
25
lr9/task_1/e3.java
Normal file
@@ -0,0 +1,25 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Main {
|
||||
private static int step = 0;
|
||||
public static void m(int x){
|
||||
space();
|
||||
System.out.println("" + x + "-> ");
|
||||
step++;
|
||||
if ((2 * x + 1) < 20){
|
||||
m(2 * x + 1);
|
||||
}
|
||||
step--;
|
||||
space();
|
||||
System.out.println("" + x + " <-");
|
||||
}
|
||||
public static void space(){
|
||||
for (int i = 0; i < step; i++){
|
||||
System.out.println(" ");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
m(1);
|
||||
}
|
||||
}
|
||||
15
lr9/task_1/e4.java
Normal file
15
lr9/task_1/e4.java
Normal file
@@ -0,0 +1,15 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(fact(5));
|
||||
}
|
||||
public static int fact(int n){
|
||||
int res;
|
||||
if (n == 1) return 1;
|
||||
else{
|
||||
res = fact(n - 1) * n;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
lr9/task_1/e5.java
Normal file
32
lr9/task_1/e5.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
int result = fact(5);
|
||||
System.out.println("Результат: " + result);
|
||||
}
|
||||
|
||||
public static int fact(int n) {
|
||||
|
||||
System.out.println("Вход в fact(" + n + ")");
|
||||
|
||||
if (n == 0) {
|
||||
System.out.println("fact(0) -> 0");
|
||||
return 0;
|
||||
}
|
||||
else if (n == 1) {
|
||||
System.out.println("fact(1) -> 1");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int a = fact(n - 2);
|
||||
int b = fact(n - 1);
|
||||
|
||||
int result = a + b;
|
||||
|
||||
System.out.println("fact(" + n + ") = fact(" + (n-2) + ") + fact(" + (n-1) + ") = " + result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user