import java.math.BigInteger;
public class Main {
// public static long start=System.currentTimeMillis();
public static int maxn = 1000;
public static BigInteger[] fun = new BigInteger[maxn]; //全局的记忆数组
public static BigInteger Fi(int n) {
if (!fun[n].equals(BigInteger.ZERO))
return fun[n];
return fun[n] = Fi(n - 1).add(Fi(n - 2));
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < maxn; i++)
fun[i] = BigInteger.ZERO;
fun[0] = BigInteger.ZERO;
fun[1] = BigInteger.ONE;
fun[2] = BigInteger.ONE;
for (int i = 1; i < maxn; i++) //注意,i不能从0开始
System.out.println(Fi(i) + "\n\n");
long time = System.currentTimeMillis() - start;
System.out.println("程序运行时间: " + time + "ms");
}
}
算出maxn项斐波那契数。