public class Fibonacci {
private
static Map<Long,Long> map = new
HashMap<Long,Long>();
static {
map.put(0L, 1L);
map.put(1L, 1L);
}
public
static void main(String[] args) {
long
t = System.currentTimeMillis();
Fibonacci fi = new
Fibonacci();
System.out.println(fi.cal(36L,map));
System.out.println(System.currentTimeMillis() - t);
System.out.println();
t = System.currentTimeMillis();
fi = new
Fibonacci();
System.out.println(fi.cal(36L));
System.out.println(System.currentTimeMillis() - t);
}
public
Long cal(Long n, Map<Long,Long> map){
if (map.get(n) != null ){
return
map.get(n);
} else {
long
i = cal(n- 1 ,map) + cal(n- 2 ,map);
map.put(n, i);
return
i;
}
}
|