题解 | #斐波那契数列#

斐波那契数列

https://www.nowcoder.com/practice/ee5d403c1172487f8c7915b3c3d924c6

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a=1,b=1;
        int c=1;
        for(int i=0;i<n-2;i++){
           c = a+b;
           a=b;
           b=c;
        }
        System.out.println(c);
    }
}

递归注意递归公式的边界条件和递归条件,只要明白了这两点,递归题目并不难

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务