蓝桥 Fibonacci数列的递推公式 JAVA

Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。
当n比较大时,Fn也非常大,现在我们想知道,
Fn除以10007的余数是多少。
Input
多组测试数据
输入包含一个整数n。1 <= n <= 1,000,000。
Output
每组输出一行,包含一个整数,表示Fn除以10007的余数。
Sample Input
10
22
Sample Output
55
7704

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int F1 = 1;
		int F2 = 1;
		int temp = 0;
		for (int i = 3; i <= n; i++) {     //前两位强给1,下面满足题目规律
			temp = F1+F2;
			F1 = F2;
			F2 = temp;
		}
		System.out.println(temp % 10007);
	}

小剧场:在现在与永远之间,有无数可能。

发布了108 篇原创文章 · 获赞 113 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43771695/article/details/104654917
今日推荐