task_1
This commit is contained in:
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