public static void main(String[] args) {
System.out.println(function_2(100));
}
//求不死神兔
public static BigInteger function_2(int i){
if(i==1||i==2) {
BigInteger big=new BigInteger("1");
return big;
}
else{
BigInteger big1=function_2(i-2);
BigInteger big2=function_2(i-1);
BigInteger big=big1.add(big2);
return big;
}
}
用循环实现不死神兔(利用BigInteger完成),在i等于100就输出不了?还是说需要等待
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- SonOfWind0311 2017-07-24 01:35关注
差不多每增加5运行次数增长11-12倍,到50就得一天了(实际上我设置成50,跑了一天多才出结果)。
粗略算一下,i=50需要一天,每加5增长10倍,那么到100就是10的10次方,需要2000多万年才能执行完。10:
calling times:109, time elapsed:0.005s11
calling times:177, time elapsed:0.008s12:
calling times:287, time elapsed:0.010s15:
calling times:1219, time elapsed:0.025s20:
calling times:13529, time elapsed:0.134s25:
calling times:150049, time elapsed:0.781s30:
calling times:1664079, time elapsed:6.698s35:
calling times:18454929, time elapsed:64.653s本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报