递推式参考:http://www.2cto.com/kf/201302/190324.html
数据较大,需要用大整数来写。
1 import java.math.*; 2 import java.util.Scanner; 3 4 public class Main 5 { 6 public static void main( String[] args ) 7 { 8 Scanner cin = new Scanner(System.in); 9 BigInteger[] f = new BigInteger[1001]; 10 f[0] = new BigInteger("1"); 11 f[1] = new BigInteger("1"); 12 f[2] = new BigInteger("2"); 13 f[3] = new BigInteger("4"); 14 for ( int i = 4; i < 1001; i++ ) 15 { 16 f[i] = f[i - 1].add( f[i - 2] ).add( f[i - 4] ); 17 } 18 while ( cin.hasNext() ) 19 { 20 int n = cin.nextInt(); 21 System.out.println(f[n].toString()); 22 } 23 } 24 }
时间: 2024-10-12 17:12:42