菲波纳数

题目:

/**
 * 输入一个正整数N(N< 50),输出斐波那契数列前N项。
 * 
 * 5
 * 1 1 2 3 5
 */

解题:

class Test92 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // code here
        int number = input.nextInt();
        int n1, n2, n3;
        n1 = n2 = 1;
        System.out.println(n1 + " " + n2 + " ");
        for (int i = 3; i <= number; i++) {
            n3 = n2 + n1;
            n1 = n2;
            n2 = n3;
            System.out.println(n3 + " ");

        }
        input.close();
    }
}

posted on 2023-06-10 16:10  陈嘻嘻-  阅读(6)  评论(0编辑  收藏  举报

导航