算兔子:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
import java.util.HashMap;
public class a{
private static HashMap<Integer,Double> map = new HashMap<Integer,Double>();
public static double f(int month){
//System.out.println("month:"+month+ " 0");
if (month < 3){
// System.out.println("1");
return 2;
}
if ( map.get(month-1) == null){
//System.out.println("month:"+(month-1));
map.put(new Integer(month-1), f(month-1));
}
if ( map.get(month-2) == null){
//System.out.println("month:"+(month-2));
map.put(new Integer(month-2), f(month-2));
}
double p = map.get(new Integer(month-1)).doubleValue()+map.get(new Integer((month-2))).doubleValue();
//System.out.println("month : "+month+" p:"+p+" 2");
return p;
}
public static long ff(long month){
// System.out.println("month:"+month+ " 0");
if (month < 3){
// System.out.println("1");
return 2;
}
long p = ff(month-1)+ff(month-2);
//System.out.println("month : "+month+" p:"+p+" 2");
return p;
}
public static void main(String args[]){
System.out.println(""+" "+f(700));
}
}