您的位置:首页 > 其它

猴子偷桃问题

2016-05-27 17:46 330 查看
猴子偷桃问题:
方法一:
int main()
{
int i = 0;
int allpeach = 1;
for (i = 9; i > 0; i--)
{
allpeach = allpeach*2+1;
}

cout << "猴子第一天公摘" << allpeach << "个桃子" << endl;

system("pause");
return 0;

}

方法二:用递归实现,代码面如下:
int main()
{
int MonkeyStealPeach(int day, int allpeach);
int allpeach=MonkeyStealPeach(10,1);
cout << allpeach << endl;
system("pause");
return 0;

}

int MonkeyStealPeach(int day, int allpeach)
{

if (day == 1)
{
return allpeach;
}
allpeach =allpeach*2+1;
MonkeyStealPeach(day - 1, allpeach);

}


本文出自 “零点时光” 博客,请务必保留此出处http://10741764.blog.51cto.com/10731764/1708440
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: