public class Fibonacci {
static long[] cach = new long[51];
public static void main(String[] args) {
long a = System.currentTimeMillis();
System.out.println( fd( 50 ) );
long l = System.currentTimeMillis();
System.out.println( l - a );
long l1 = System.currentTimeMillis();
fd1( 50 );
long l2 = System.currentTimeMillis();
System.out.println( l2 - l1 );
}
private static int fd1(int n) {
if (n == 1 || n == 2) {
return 1;
}
int a = fd1( n - 1 ) + fd1( n - 2 );
return n;
}
private static long fd(int n) {
if (n == 1 || n == 2) {
return 1;
}
if (cach[n] != 0) {
return cach[n];
}
long x = fd( n - 1 ) + fd( n - 2 );
cach[n] = x;
return x;
}
}
12586269025
0
35868
转载请注明原文地址:https://ipadbbs.8miu.com/read-17939.html