/**
 * 面试题9:菲波那切数列
 * 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
 n<=39
 */
public class _09_Fibonacci {
    public static void main(String[] args){
        Solution09 solution09 = new Solution09();
        System.out.println(solution09.Fibonacci(39));
    }
}
class Solution09 {
    public int Fibonacci(int n) {
        int[] result={0,1};
        if(n<2){
            return result[n];
        }
        long current=0;
        long firstNum=0;
        long secondNum=1;
        for(int i=2;i<=n;i++){
            current=firstNum+secondNum;
            firstNum=secondNum;
            secondNum=current;
        }
        return Integer.parseInt(String.valueOf(current));
    }
}

09:菲波那切数列09:菲波那切数列

相关文章:

  • 2021-08-02
  • 2022-12-23
  • 2021-08-20
  • 2021-05-19
  • 2021-09-21
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2021-10-01
  • 2022-02-27
相关资源
相似解决方案