• 斐波拉契数列
  • fn = f(n-1) + f(n-2) 其中 n 是正整数,且 n 大于等于 2
    代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class TestFibonacci {
public static void main(String[] args) {
int result = fibonacci(8);
System.out.println(result);
}
public static int fibonacci(int index){
if(index >= 0){
if(index == 0){
return 0;
}else if(index ==1){
return 1;
}else{
return fibonacci(index -1) +fibonacci(index -2);
}
}else{
System.out.println("请你重新输入");
return -1;
}
}
}