递归

1.递归的定义
递归是函数的自我调用。用于解决规模减小,但性质不变的问题。


2.递归的两大重要点
(1)结束点:递归是函数的自我调用。那么在什么时候才算自我调用的结束?
(2)递归过程:怎样拆解问题的规模,使问题的规模一步步的减小?
3.递归的实战
(1)输出一个十进制正整数(小于65536)的二进制代码。



#include <stdio.h>

void decimal_2_binary(int x)
{
	if(0 == x || 1 == x) //结束点
		printf("%d ", x);
	else //递归过程
	{
		decimal_2_binary(x/2);
		printf("%d ", x%2);
	}
}

int main()
{
	int num;

	printf("Please input the number:");
	scanf("%d", &num);
	decimal_2_binary(num);
	printf("
");

	return 0;
}

(2)逆向输出一个字符串的全部有效字符。

#include <stdio.h>

void pt_str(char *str)
{
	if(*str == '')
		return;
	else
	{
		pt_str(str+1);
		printf("%c", *str);
	}
}

int main()
{
	char str[] = "China.";

	pt_str(str);
	printf("
");

	return 0;
}

原文地址:https://www.cnblogs.com/blfbuaa/p/6821530.html