fin des tp
This commit is contained in:
38
DEV3.2/Recursiviote/Fibonacci.java
Normal file
38
DEV3.2/Recursiviote/Fibonacci.java
Normal file
@@ -0,0 +1,38 @@
|
||||
public class Fibonacci{
|
||||
public static int fibonacci(int x0, int x1, int val, int indentation) {
|
||||
int i;
|
||||
for (i = 0; i < indentation; i++)
|
||||
System.out.print(" ");
|
||||
System.out.println("u0 ="+x0+"u1 ="+x1);
|
||||
if (val == -2) {
|
||||
return x0;
|
||||
} else if (val == -1) {
|
||||
return x1;
|
||||
} else if (val == 0) {
|
||||
return x1;
|
||||
}else {
|
||||
int v = x0;
|
||||
x0 = x1;
|
||||
x1 = v + x1;
|
||||
val--;
|
||||
int res = fibonacci(x0, x1, val, indentation+1);
|
||||
for (i = 0; i < indentation; i++)
|
||||
System.out.print(" ");
|
||||
System.out.println("resultat ="+x1);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] argv){
|
||||
if (argv.length < 1){
|
||||
System.err.println("manque un nombre pour le calcul de la suite");
|
||||
} else {
|
||||
int x0 = 0;
|
||||
int x1 = 1;
|
||||
int ind = 0;
|
||||
int val = Integer.parseInt(argv[argv.length-1]) - 2;
|
||||
int res = fibonacci(x0, x1, val, ind);
|
||||
System.out.println("Fibonacci de "+(val+2)+"ième terme = "+res);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user