6.斐波那契数列

来源:互联网 发布:航天远景软件 编辑:程序博客网 时间:2024/06/11 00:36

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

public class Solution {    public int Fibonacci(int n) {        int a = 1;        int b = 1;        int c = 0;        if(n<0){            return 0;        }else if(n==1||n==2){            return 1;        }else{            for(int i =3;i<=n;i++){                c = a+b;                a = b;           b = c;            }            return c;        }    }}


0 0