`
幽佐0911
  • 浏览: 11970 次
  • 性别: Icon_minigender_1
  • 来自: 长春
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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+"个桃子");
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics