Hat's Fibonacci

4人阅读 评论(0) 收藏 举报
分类:

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.**

代码

import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        BigInteger f[]=new BigInteger[10001];
        f[1]=new BigInteger("1");
        f[2]=new BigInteger("1");
        f[3]=new BigInteger("1");
        f[4]=new BigInteger("1");
        for(int i=5;i<=10000;i++)
        {
            f[i]=f[i-1].add(f[i-2]).add(f[i-3]).add(f[i-4]);
            //f[i]=f[i-1].add(f[i-2].add(f[i-3].add(f[i-4])));
        }
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int m=sc.nextInt();
            System.out.println(f[m]);
        }   
    }
}
//注意打表的数组的大小。note中表明,不会生成超过2005位数的斐波那契数字。
查看评论

Hat's Fibonacci(杭电1250)

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • hdd871532887
  • hdd871532887
  • 2014-10-20 16:24:05
  • 664

hdu 1250 Hat's Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • u011721440
  • u011721440
  • 2014-03-01 18:27:29
  • 823

杭电1250Hat's Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • z8110
  • z8110
  • 2015-08-06 16:02:13
  • 309

HDU 1250: Hat's Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • u013487051
  • u013487051
  • 2014-07-05 20:53:17
  • 743

HDOJ 1250 Hat's Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • muxin365
  • muxin365
  • 2014-04-07 20:40:25
  • 388

[大数] HDU 1250 - Hat's Fibonacci

一开始用int二维数组,MLE了。。然后改成char数组过了。
  • x_iaow_ei
  • x_iaow_ei
  • 2014-11-16 16:10:29
  • 529

hdu1250-Hat's Fibonacci

http://acm.hdu.edu.cn/showproblem.php?pid=1250 二维大数处理 #include #include #include #include using n...
  • liujie619406439
  • liujie619406439
  • 2013-06-08 16:51:11
  • 898

HDU 1250 Hat's Fibonacci(Java大数相加)+讲解

HDU 1250 Hat's Fibonacci(Java大数相加)+讲解
  • u012860063
  • u012860063
  • 2014-09-03 19:42:34
  • 1065

HDOJ 1250 Hat's Fibonacci(大数,错了11次!!)

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • zwj1452267376
  • zwj1452267376
  • 2015-07-29 19:40:01
  • 366

HDU 1250 Hat's Fibonacci (Java大数,类似Fib数,有个坑!!)

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • hurmishine
  • hurmishine
  • 2016-05-15 23:34:47
  • 3457
    个人资料
    持之以恒
    等级:
    访问量: 846
    积分: 275
    排名: 32万+