Recursion in C language

1. Recursion
The programming technique that programmers call themselves is called recursion. Recursion is widely used in programming languages ​​as an algorithm.
1. A process or function has a method of directly or indirectly calling itself in its definition or description. It usually transforms a large and complex problem into a smaller problem similar to the original problem to solve, recursively The strategy only needs a small amount of programs to describe the multiple repetitive calculations required in the process of solving the problem, which greatly reduces the amount of code of the program.
2. The main way of thinking about recursion is to make things smaller
. 2. Two necessary conditions for recursion.
1. There are restrictions. When this restriction is met, the recursion will not continue
. 2. Each recursive call is getting closer and closer. This restriction is for
example:
Exercise 1:

//接收一个整形值(无符号),按照顺序打印它的每一位,例如:输入1234,输出1 2 3 4
//解题思路:接收了一个1234,然后你先%10得到个位数4,然后在/10去掉个位数4,得到123,然后不断重复这个过程,在把这几个数字4 3 2 1 存起来,倒着打印出来。方法是可以的但是太过麻烦
//Print(123)  4
//Print(12) 3  4
//Print(1) 2 3 4      一位数字就没有必要继续拆下去了
void Print(int n)
{
    
      
	if (n > 9)     //存在限制条件
	{
    
    
		print(n / 10);
	}
	printf("%d ", n % 10);  //无限接近结果
}
int mian()
{
    
    
	unsigned int num = 0;
	scanf("%d", num);
	Print(num);
	return 0;
}


Insert picture description here

Exercise 2:
Writing functions is not allowed to create temporary variables, find the length of the string (equivalent to writing a strlen function to solve the length of the string) The
normal solution method:

int main()
{
    
    
	char arr[] = "bit";
	int len = strlen(arr);
	printf("%d\n", len);
	return 0;
}

//你会发现这个还是无法满足题目所说的要求,因为你写的函数内部创建了一个count的变量
int my_strlen(char* str)
{
    
    
	int count = 0;
	while(*str != '\0')
	{
    
    
		count++;
		str++;
	}
	return count;
}
int main()
{
    
    
	char arr[] = "bit";
	int len = my_strlen(arr);
	printf("len = %d\n", len);
	return 0;
}

Use recursive method to find the length of the string

int my_strlen(char* str)
{
    
    
	if (*str != '\0')
		return 1 + my_strlen(str + 1);
	else
		return 0;
}

int main()
{
    
    
	char arr[] = "bit";
	int len = my_strlen(arr);
	printf("len = %d\n", len);
	return 0;
}

//大事化小
//my_strlen("bit")
//1 + my_strlen("it")
//1 + 1 + my_strlen("t")
//1 + 1 + 1 + my_strlen("")
//1 + 1 + 1 + 0
//3


//*str != '\0' 是限制条件,当不满足时就会不再进行递归,结果在不断地接近目标

Exercise 3: Find the factorial of n
1. Realize it in a loop

int Fac1(int n)
{
    
    
	int i = 0;
	int ret = 1;
	for (i = 1; i <= n; i++)  //n! 不好想我们可以倒着思考问题 1*2*3 ....*n
	{
    
    
		ret *= i;
	}
	return ret;
}
int main()
{
    
    
	int n = 0;
	int ret = 0;
	scanf("%d", &n);
	ret = Fac1(n); //循环的方式
	printf("%d\n", ret);
}

2. Recursive way to achieve
Insert picture description here

int Fac2(int n)
{
    
    
	if (n <= 1)
		return 1;
	else
		return n*Fac2(n - 1);
}
int main()
{
    
    
	int n = 0;
	int ret = 0;
	scanf("%d", &n);
	ret = Fac2(n); 
	printf("%d\n", ret);
}

Exercise 4
Find the nth Fibonacci number (the sum of the first two numbers is equal to the next number)
Insert picture description here

int Fib(int n)
{
    
    
	if (n <= 2)
		return 1;
	else
		return Fib(n - 1) + Fib(n - 2);
}

int main()
{
    
    
	int n = 0;
	int ret = 0;
	scanf("%d", &n);
	//TDD --- 测试驱动开发   先想这个函数怎么用,再去具体实现这个函数
	ret = Fib(n);
	printf("ret = %d\n", ret);
	return 0;
}

You will find that the function implemented in this recursive way can quickly and correctly get the result when n is relatively small, but when n becomes large, such as 50, you will find that the result will not appear for a long time!

                      50
              49             48
           48     47       47    46
          47 46  46 45   46 45  45 44  
你会发现在做很多重复的工作

So it is not appropriate to use recursive method to solve Fibonacci number here. It's not as simple as using recursion. So here we use a loop to achieve.

int Fib(int n)
{
    
    
	int a = 1;
	int b = 1;
	int c = 1;  // 这里在初始化的时候你可能会初始成0;但是当n等于1或者2的时候你进来不满足while循环直接返回c那就错了
	while (n > 2)
	{
    
    
		c = a + b;
		a = b;
		b = c;
		n--;
	}
	return c;
}
int main()
{
    
    
	int n = 0;
	int ret = 0;
	scanf("%d", &n);
	ret = Fib(n);
	printf("ret = %d\n", ret);
	return 0;
}

Summary: But when solving the problem in a recursive way, it is not correct to simultaneously meet the constraints and constantly approach the target value, because in this case there may also be stack overflow and extremely low operating efficiency.

void test(int n)
{
    
    
	if (n < 10000)
	{
    
    
		test(n + 1);
	}
}
int main()
{
    
    
	test(1);
	return 0;
}
//栈溢出了,程序崩溃

Improvement: 1. Hanoi Tower problem
2. Frog jumping problem

Guess you like

Origin blog.csdn.net/MEANSWER/article/details/109582753