递归

递归的两个必要条件
存在限制条件,当满足这个限制条件的时候,递归便不再继续
每次递归调用之后越来越接近这个限制条件。

void print(int n)
{
	if (n > 9)
	{
		print(n / 10);
	}
	printf("%d", n % 10);
}
/*
int fib(int n)//求斐波那契数 效率低
{
	if (n <= 2)
	{
		return 1;
	}
	else
		return fib(n - 1) + fib(n - 2);
}
*/

int fib(int n)//求斐波那契数
{
	int result = 1;
	int pre_result = 1;
	int next_older_result = 1;
	while (n > 2)
	{
		next_older_result = result + pre_result;
		result = pre_result;
		pre_result = next_older_result;
		n--;
	}
	return next_older_result;
}
posted @ 2024-07-20 16:28  AuroraOvO  阅读(5)  评论(0)    收藏  举报