递归特点:
1、有基准情况(base case),则结束
2、其他情况继续调用自己
//0: 0
//1 : 1
//2 : 2 + 4 = 6
//3 : 12 + 9 = 21
//4 : 42 + 16 = 58
//5 : 116 + 5 * 5 = 141
unsigned int DoOpera(unsigned int x)
{
if (x <= 0)
return 0;
return (2 * DoOpera(x - 1) + x * x);
}
int main()
{
unsigned int result = DoOpera(5);
LOG_INFO("result:" << result);
return 0;
}
本文摘录于海阔天空的博客,作者: zjg555543,发布时间: 2015-06-12
网友评论