当前位置:网站首页>Using recursive functions to solve the inverse problem of strings
Using recursive functions to solve the inverse problem of strings
2022-07-02 08:47:00 【Programmers on the road】
One 、 Overview of recursive functions
In the process of programming with process oriented programming language , Generally, it is based on structured programming ideas 、 Modular programming method to write programs and organize code . Familiar to us C Language is such a kind of programming language , It usually carries out the modular organization of the program in the unit of function ,C The source program is composed of a main function and several non main functions . The computer is executing C The program , Is in order from the main function main() Start execution , If you call other functions , Then the main function pauses execution , Instead, execute the corresponding function , After the function is executed , Return the main function , Main function continues to execute , This is called function call . Not only the main function can call other functions , Various functions can also be called each other , If a function calls itself , We call it recursive call of function . In the solution of some special problems , Using recursive functions can sometimes be very efficient .( Hanoi The problem is also a typical application of recursive algorithm )( Better reading experience , Please visit Programmers are on the road )
recursive (Recursion) In computer science, it refers to a method to solve a problem by repeatedly decomposing a problem into a similar subproblem , Its core idea is the strategy of divide and rule . Recursive algorithm is suitable for solving the following three kinds of problems :
- Data is defined recursively . Such as Fibonacci function .
- The solution of the problem is implemented by recursive algorithm . Such as Hanoi problem .
- The structural form of data is defined recursively . Two fork tree 、 Generalized table, etc .
Two 、 String inverse example solution
2.1 Title Description
Write a function to reverse the string ( Such as a string "abcde" become "edcba").
2.2 Analysis and solution
Reverse the string , You can swap the first string with the last string , Then reverse the rest of the string , The length of the remaining string is subtracted from the original length 2, In order to reduce the scale , The rest of the string inversion method is the same as the first time , If the string length ≤1, Then the recursive condition ends . The procedure is as follows :
#include<stdio.h>
//start,end Subscript for the beginning and end of the string
void convert_str(char *str, int start, int end){
char ch;
if((end - start) < 1){
// <1 Description string There is only one character , No need to exchange , Recursion end condition
return ;
}else{
ch = str[start];
str[start] = str[end];
str[end] = ch;
convert_str(str,start+1,end-1); // Call this function recursively , Swap the remaining strings
}
}
int main(){
char x[] = "abcdefgh";
convert_str(x,0,7);
printf(" The inverse of the string is : %s \n", x);
return 0;
}
3、 ... and 、 summary
1) The essence of recursion is how to transform large-scale 、 The more difficult problems become smaller 、 Easy to solve the same problem , Smaller problems become smaller problems , And to a certain extent, its solution can be obtained directly , So we can get the solution of the original problem . Besides , Also pay attention to the termination conditions of recursion , Which is the exit of recursion , If the export conditions are set incorrectly , The program cannot be terminated , This will cause the program to crash .
2) The implementation of recursion benefits from the data structure of stack , Every call to the function , Stack will be used to save function parameters 、 local variable 、 Return address and other data , therefore , When using recursion , We must consider the depth of recursion , If it's too deep , It is easy to cause stack overflow , Cause problem solving failure .
边栏推荐
猜你喜欢
IP protocol and IP address
Service de groupe minecraft
ARP及ARP欺骗
Openfeign facile à utiliser
Installing Oracle database 19C RAC on Linux
commands out of sync. did you run multiple statements at once
Web security -- Logical ultra vires
kubeadm部署kubernetes v1.23.5集群
Solution of Xiaomi TV's inability to access computer shared files
Zipkin is easy to use
随机推荐
HCIA—数据链路层
Openfeign is easy to use
Minecraft安装资源包
sqli-labs第1关
一个经典约瑟夫问题的分析与解答
Chrome debugging
Pclpy projection filter -- projection of point cloud to cylinder
Qt——如何在QWidget中设置阴影效果
Pointer initialization
ICMP协议
Web security -- Logical ultra vires
Kubernetes deploys Loki logging system
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
Nacos download, start and configure MySQL database
C# 高德地图 根据经纬度获取地址
选择排序和插入排序
路由基础—动态路由
寻找链表中值域最小的节点并移到链表的最前面
IP protocol and IP address
Hcia - Application Layer