猴子偷桃问题:
方法一:
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