1.循环
1 public class T009 {
2 public static void main(String[] args){
3 System.out.println(fibonacci(3));
4 }
5 public static long fibonacci(int n){
6 int[] result = {0,1};
7 if(n<2)
8 return result[n];
9 long fibminOne=1;
10 long fibminTwo=0;
11 long fibN = 0;
12 for(int i =2;i<=n;i++){
13 fibN = fibminOne+fibminTwo;
14 fibminTwo=fibminOne;
15 fibminOne=fibN;
16 }
17 return fibN;
18 }
19 }
2.递归
java实现——009Fibonacci数列
时间: 2024-10-02 22:07:10