hdu 2018 母牛的问题 #DP#类斐波那契#JAVA水大数

每头母牛到第四年生小牛,

f[i] = f[i-1] + f[i-3], i > 4


http://acm.hdu.edu.cn/showproblem.php?pid=2018

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
    public static void main(String []args){
        Scanner cin = new Scanner(System.in);
        Integer N = 55,i,n;
        BigInteger[] fm = new BigInteger[N],fx = new BigInteger[N];
        for(i = 1; i < 5; ++i)
        	fm[i] = BigInteger.valueOf(i);
        for(; i < N; ++i)
        	fm[i] = fm[i-1].add(fm[i-3]);

       
       while(true){
           n = cin.nextInt();
           if(n == 0)
               break;
           System.out.println(fm[n]);
       }
    }
}


你可能感兴趣的:(DP)