【Java大数BigInteger+递推】HDU-1250 Hat's Fibonacci

在这里插入图片描述

注解

1、Java大数BigInteger。
2、递推关系。

代码

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            BigInteger[] bi = new BigInteger[n+1];
            if(n>=5){
                bi[1] = BigInteger.ONE;
                bi[2] = BigInteger.ONE;
                bi[3] = BigInteger.ONE;
                bi[4] = BigInteger.ONE;
                for(int i=5; i<bi.length; i++){
                    bi[i] = bi[i-1].add(bi[i-2]).add(bi[i-3]).add(bi[i-4]);
                }
                System.out.println(bi[n]);
            }
            else{
                System.out.println("1");
            }
        }
    }
}

结果

在这里插入图片描述

发布了475 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/zhanggirlzhangboy/article/details/103739588