劍指offer 斐波那契數列

2021-08-07 06:36:35 字數 554 閱讀 4022

思路:斐波那契數列屬於非常經典的遞迴問題,但也可以用迴圈做。

/**

* @author tom qian

* @email [email protected]

* @github

* @date 2023年8月10日

*///大家都知道斐波那契數列,現在要求輸入乙個整數n,請你輸出斐波那契數列的第n項。

// n<=39

public class 斐波那契數列

//迴圈

public static int itefibonacci(int n)

if(n<3)

int count=0;

int count1=1;

int count2=1;

for(int i=3;i<=n;i++)

return count;

}/**

* @param args

*/public static void main(string args)

}

劍指offer 斐波那契數列

題目1描述 寫乙個函式,輸入n,求斐波那契數列的第n項。斐波那契數列的定義如下 f n 0 n 0 f n 1 n 1 f n f n 1 f n 2 n 1 分析描述 在大多數的c語言教科書中,一般會用遞迴求斐波那契數列。如下 long long fibonacci unsigned int n ...

劍指offer 斐波那契數列

記錄來自 劍指offer 的演算法題。題目如下 寫乙個函式,輸入n,實現斐波那契數列的第n項。斐波那契數列的定義如下 f n 01 f n 1 f n 2 n 0 n 1n 1 教科書上通常在介紹遞迴的時候都會使用斐波那契數列作為例子,然後給出下列解法 long long fibonacci uns...

劍指offer 斐波那契數列

現在要求輸入乙個整數n,請你輸出斐波那契 fibonacci 數列的第n項。此題易用遞迴來實現 public intfibonacci int n 但是上述的遞迴解法有很嚴重的效率問題。以求解 f 10 為例,想求得 f 10 需要先求得 f 9 和 f 8 同樣,想求得 f 9 需要先求得 f 8...