现在的位置: 首页 > 综合 > 正文

递归和迭代的区别之我见

2018年03月20日 ⁄ 综合 ⁄ 共 256字 ⁄ 字号 评论关闭

递归:当一个函数包含着对它自身调用时就称这个函数式递归的

如典型的阶乘函数

int func(int n)
{
	//error detect

	if (n==1)
	{
		return 1;
	}
	return func(n-1)*n;
}

迭代就是不断的使用计算出来的结果来代替旧值

int func(int n)
{
	//error detect

	int result = 1;
	for (int i = 1;i <= n;i++)
	{
		result *= i;
	}
	return result;
}


一起学习,一起进步,欢迎访问我的博客:http://blog.csdn.net/wanghao109

抱歉!评论已关闭.