java递归法解决猴子吃桃子问题
class Eat{int eatPeach(int time,int peach){if(time==1){return peach;} else{time--; peach=(peach+1)*2; return eatPeach(time,peach); }}}public class Monkey{public static void main(String args[]){Eat test=new Eat(); int time=10,peach=1,count;count=test.eatPeach(time,peach);System.out.println("第一天这个猴子摘了"+count+"个桃子");}}
页:
[1]