Daily practice Blue Bridge Cup C/C++ Group B ~ What is a recursive algorithm?

What is recursion?

Recursion is when a function calls itself!

The so-called recursion, is to have recursion and return! Only recursion will cause the program to crash!

To terminate the recursion when appropriate (to avoid crashes, recursive functions must contain conditional statements)

Example:

#定义一个递归函数
def f(x):   //当x=3,调用函数
    return x + f(x-1);

Returns 3 + f(2), f(2) returns 2 + f(1), f(1) returns 1 + f(0)... When calling a function infinitely, it needs to occupy a memory space called stack, and put some The data is stored in the stack, the operation is over, the data is taken out from the stack, but only the call does not return, the stack will be full, and the stack will overflow. Program terminated!

#定义一个递归函数
def f(x):      //x=3
    if x > 0:
         return x + f(x-1);
    else:
        return 0;

Result: 6

#定义一个递归函数
def f(x):      //x=100
    if x > 0:
         return x + f(x-1);
    else:
        return 0;

1+2+3+4+…+100=5050

What is function nesting and recursion?

Calling from one function to another is called nested invocation of functions

There is another situation:

A recursive call that calls itself a function within the body of a function

A simple example of recursive function calls, run(3), run(2), run(1), substitute the conditions to determine whether they are met.

#include <stdio.h>
#include <stdlib.h>

void run(int num){
    
    
	if(num==0)
	    return;
	printf("蓝桥杯 我要获奖!\n"); 
	run(num-1);  //每递归一次,num减1,最终满足终止条件num==0,递归完成 

}

void main(){
    
    
	run(3); //先定义ー个函数,让它执行3次
} 
蓝桥杯 我要获奖!
蓝桥杯 我要获奖!
蓝桥杯 我要获奖!

Is it better to understand function nesting and recursion through these two examples, the go function calls the run function, and the run function calls the main function

#include <stdio.h>
#include <stdlib.h>
void go(){
    
    
	printf("你可以的\n"); 
}
void run(){
    
    
	printf("runA\n");
	go();
	printf("runB\n");
}
void main(){
    
    
	printf("helloworld\n");
	run();
	printf("欢迎来到蓝桥杯\n");
}
helloworld
runA
你可以的
runB
欢迎来到蓝桥杯

There is a little trick:Encountered recursive conditional statement!

recursive generation

The subproblems generated by divide and conquer are often smaller patterns of the original problem, which facilitates the use of recursive techniques. In this case, the repeated application of divide-and-conquer method can make the sub-problem the same type as the original problem, but its scale is continuously reduced, and finally the sub-problem is reduced to the point where it is easy to find its solution directly. This naturally leads to recursive processes.

recursive application

  • The definition of the problem is recursive, like the factorial problem.

  • The problem solving process is recursive, such as the Tower of Hanoi problem.

  • The data structure used in the problem is recursive, such as searching for elements in a linked list in a linked list.

recursive math formula

  • It is a set of equations or inequalities;

  • Its first formula gives the initial value of the function, which is called the boundary condition;

  • Boundary conditions and recursion equations are the two basic elements of recursion.

  • Its second formula is to use the function value of the smaller independent variable to describe the function value of the larger independent variable, which is called the recursive equation;

Write a program: output the first 30 items of the Fibonacci sequence, requiring 5 items per line.

#include <stdio.h> // C语言版本
int fib(int n){
    
    // 根据递归式写出函数
	if (n==1 || n==2)
         return 1;
    else
        return fib(n-1)+fib(n-2);
} 

int main(){
    
    
    for(int i=1;i<=30;i++) // 输出前30项
{
    
    
    printf("%-10d",fib(i));
    if (i%5==0) // 每行输出5项
        printf("\n");
}
    
}
1         1         2         3         5
8         13        21        34        55
89        144       233       377       610
987       1597      2584      4181      6765
10946     17711     28657     46368     75025
121393    196418    317811    514229    832040

4! recursion

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int factorial(int n){
    
    	
	if(n==0)
	 return 1;
	return n*factorial(n-1);
}
void main(){
    
    
	factorial(4);
	
}

There is also string reverse recursion!

def reverse_string(string s):
	
	if len(s) == 1:
		return s
	else:
		return s[-1] + reverse_string(s[:-1]);
reverse_string('abc'); //调用函数

insert image description here

Guess you like

Origin blog.csdn.net/A6_107/article/details/123274240