題目:寫一個函數,輸入n,求斐波那契(Fibonacci)數列的第n項。斐波那契數列的定義以下: java
package aigorithm; public class CodingInterviewsNine { public static void main(String[] args) { int result = FibonacciIterativelyOFSolution9(2); System.out.println(result); } public static int FibonacciIterativelyOFSolution9(int n ){ int result[] = {0,1}; if (n<2){ return result[n]; } int finbNminusOne = 1; int finbNnimusTwo = 0; int finbN = 0; for (int i = 2; i <=n ; i++) { finbN = finbNminusOne + finbNnimusTwo; finbNnimusTwo = finbNminusOne; finbNminusOne = finbN; } return finbN; } }
待補充單元測試