猴子偷桃问题: 方法一: 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); }
猴子偷桃问题
原创
©著作权归作者所有:来自51CTO博客作者零点时光的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章