蓝桥杯 基础练习 Fibonacci数列

问题描述

蓝桥杯 基础练习 Fibonacci数列

Java 代码实现

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int n = in.nextInt();
        int a = 1;
        int b = 1;
        int sum = 1;
        
        if (n < 3) {
            System.out.println(1);
        } else {
            for (int i = 3; i <= n; ++i) {
                sum = (a+b)%10007;
                a = b;
                b = sum;
            }
            System.out.println(b);
        }
        
        in.close();
    }
}
上一篇:fibnacci数列递归实现


下一篇:java-面向对象-递归-斐波那契数