HDU - 1250-大数加法

题目传送门

HDU - 1250
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646

Note:
No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.

package dashu;
import java.math.BigInteger;
import java.util.Scanner;
public class da{
   
	public static void main(String[] aegs)
	{
   
		int n;
		Scanner in=new Scanner(System.in);
		BigInteger th[]=new BigInteger[10010];
		BigInteger w=new BigInteger("1");
		th[1]=th[2]=th[3]=th[4]=w;
		for(int i=5;i<=10000;i++)
		{
   
			th[i]=th[i-1].add((th[i-2].add(th[i-3].add(th[i-4]))));
		}
		while(in.hasNext())
		{
   
			n=in.nextInt();
			System.out.println(th[n]);
		}
	}
}
全部评论

相关推荐

西南山:哥,你的技能是在报菜单吗
点赞 评论 收藏
分享
牛客279957775号:铁暗恋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务