蓝桥杯2019年真题:数列求值

题目

【问题描述】 给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,
每项都是前 3 项的和。求 第 20190324 项的最后 4 位数字。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。
本题的结果为一 个 4 位整数(提示:答案的千位不为 0),
在提交答案时只填写这个整数,填写 多余的内容将无法得分。

答案

4659

package competition3;

public class SumArray
{
    
    
	public static void main(String[] args)
	{
    
    
		int index=20190324;
//		int index=7;
		int x=1;
		int y=1;
		int z=1;
		int temp=-1;
		for(int targe=3;targe<index;targe++)
		{
    
    
			temp=(x+y+z)%10000;
			x=y;
			y=z;
			z=temp;
		}
		System.out.println(temp);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/108901435