1 |' \& Q) d, b W8 s" z试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, ! Q+ K+ q/ ?9 X3 ~我推理机的核心算法应该是二叉树遍历的变种。* G9 L$ o6 v+ P& C! X; E8 m
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。作者: nanimarcus 时间: 2025-2-2 00:45
Recursion in programming is a technique where a function calls itself in order to solve a problem. It is a powerful concept that allows you to break down complex problems into smaller, more manageable subproblems. Here's a detailed explanation:- e" e: B0 o$ N& R( c) }& I2 [' W
Key Idea of Recursion3 O* I5 Q# ]6 R- b1 s4 y
7 B8 S+ L6 s" lA recursive function solves a problem by: - H2 d9 ~/ `: e2 A - s9 U# X4 O/ P$ | Breaking the problem into smaller instances of the same problem. ) {" ^1 U' o; m) j ; |% E) S0 d1 H3 D+ [ Solving the smallest instance directly (base case).; M7 k% G0 j8 O5 o4 ^3 J
' J* ^/ _% U D Combining the results of smaller instances to solve the larger problem.( g, C4 {9 Z$ w$ Q3 d
4 L I0 t. J9 ~$ y8 m9 a
Components of a Recursive Function # i. J* e, ^* [) w- C& u9 f0 f( o# ~
Base Case: 9 x1 {7 `, b; X8 o% t % M# g( c- f+ J/ n8 C- ], S This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* i: S" w5 j% h) K$ S& l
8 e7 r/ @) _+ T7 S9 k8 U' i It acts as the stopping condition to prevent infinite recursion.( N* g0 N: w8 `9 n$ i& Y) v
; I4 g, f7 c3 o3 |6 k+ q- j Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 3 {7 \$ b) y( k) W5 v 1 G) w* N" r9 R' x& |: B% H Recursive Case:% [+ o' \! c, J% C" o! d& I0 a/ X( J/ @1 a
$ d3 `# _ N0 U h0 U/ E
This is where the function calls itself with a smaller or simpler version of the problem.9 c: M# A" N* f2 p; g2 p1 A9 G4 J7 A
( ^. \2 n9 L, C6 U2 X2 S! | Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).+ p& H7 Y+ K1 Y/ c
: Z$ B+ }! [- ^9 J0 j1 J6 RExample: Factorial Calculation " P, v0 j. [( k6 c8 s3 w, ~% J/ t$ h7 p0 z. y% v
The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It can be defined recursively as:' Y# H d4 H, U0 P2 s7 B- e
$ z e# F2 O M Base case: 0! = 17 H; m* S: w8 H, ]4 R+ R
. f% D/ j' q3 F/ E! g Recursive case: n! = n * (n-1)! 8 u1 B4 a9 p) a5 {! w4 \7 ?8 ?6 T: d3 m& }- o! b
Here’s how it looks in code (Python): , [0 x: e; | z7 O* h: upython8 v6 e2 [6 w4 R& {7 a' ^
8 v6 `+ V6 P( [5 Y1 H. C
+ O, c: ^3 i3 K
def factorial(n):. U+ I8 ?# m6 ~+ h- j( J- c" X
# Base case + V: g7 Z+ Z! j if n == 0:6 h# y2 X6 g8 ^2 y4 u7 E- L
return 10 i( ?0 t, c" l% s- e) B' U
# Recursive case3 f y1 j( Z& ~- H1 Y9 X
else: 8 T5 E' J9 W5 h+ |3 u return n * factorial(n - 1) & s! ?4 A. ~6 E# y+ w ! Y3 J$ i9 }9 M- g \0 B# Example usage5 u: [- A# N& z# E: D+ [# E- l
print(factorial(5)) # Output: 120 c0 p& t1 _0 s' J4 X/ R
3 h m2 D, J! i$ @
How Recursion Works " S3 z# a$ k1 p+ ? a 8 ^3 [9 {( n: f& m( |. {9 _* S4 ^- f The function keeps calling itself with smaller inputs until it reaches the base case.; L1 s0 B1 n# r
; z8 a& c1 ]" N Once the base case is reached, the function starts returning values back up the call stack.: Q% r; z& n9 \" |
+ z6 E5 ?8 b8 J5 F
These returned values are combined to produce the final result.% i1 e$ I+ h$ Y4 {/ r$ J
& D" s* U" u4 Y8 L
For factorial(5):; H6 W+ p% E/ J# |& d! y9 C
: V1 v$ j. ?6 J9 y" \) B% g, V2 D2 u6 _3 L
factorial(5) = 5 * factorial(4)( E/ \4 S+ c) a$ \" q
factorial(4) = 4 * factorial(3)9 q0 k' m; \- p
factorial(3) = 3 * factorial(2)9 Q$ e2 L. S/ j' S& G
factorial(2) = 2 * factorial(1) 0 w: S' E+ z6 W8 g" Tfactorial(1) = 1 * factorial(0)- U; ~$ ~( ]* U9 b
factorial(0) = 1 # Base case 9 U, h! C+ S1 O- W/ B# h! f1 J - f: f7 i5 A$ DThen, the results are combined:3 u8 f4 k4 \. t# B
/ j" W2 V# A, @( o 6 P6 E/ t1 V' l8 F/ ?. w: `; {7 Zfactorial(1) = 1 * 1 = 1: j& P2 q8 T% C! j& L6 N+ X
factorial(2) = 2 * 1 = 2 9 K. D2 Y# }+ j/ Ifactorial(3) = 3 * 2 = 63 D/ o. |# ^7 ?7 U. a* k' _
factorial(4) = 4 * 6 = 24 2 `- O) c- Q. R" T7 w5 Q' dfactorial(5) = 5 * 24 = 120 J) E/ d9 O' n; I& G- Q7 Q2 o5 E n' {" `
Advantages of Recursion w \0 _, q3 |" _+ n7 V" V5 S: O
) D' F/ S6 y! I
Simplicity: Recursive solutions are often more intuitive and easier to write for problems that have a natural recursive structure (e.g., tree traversals, divide-and-conquer algorithms).2 P. H2 e- `5 f+ Y. v
4 J3 q) ]+ q. q) o' D ] Readability: Recursive code can be more readable and concise compared to iterative solutions. + U* s4 X& Z4 q* {& ^. n4 C/ H D! R2 c+ K, k3 d& r L
Disadvantages of Recursion ' D# T% T! o7 I+ W 0 n8 X% e; @4 S5 @0 Z Performance Overhead: Each recursive call adds a new layer to the call stack, which can lead to high memory usage and potential stack overflow for deep recursion. 7 e, @8 X4 D3 M1 E, s( O! _, }6 N/ A) D' _* ~: O0 w) O& ^ O
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 6 {; j5 F4 e* }' v/ M$ D0 U8 M' W! c! c1 t1 s; u# Z1 j
When to Use Recursion . `* K9 T3 \7 t6 O9 ]/ A4 Y$ X8 a { a7 W# D& ~8 c( i
Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). . Y! ~$ Y& @; s h* o+ c% _6 h ~ h, }
Problems with a clear base case and recursive case. 3 o+ A& [3 }& M: p, ]1 t5 W8 h/ d; {) z/ q
Example: Fibonacci Sequence0 u# z4 J9 Q1 k, _& K+ O
# | N. H: n. t7 ?& SThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: 2 o1 ], y" G5 I2 B' F! h: H9 C& p$ g% G2 }) ]
Base case: fib(0) = 0, fib(1) = 1 ; A4 N# H# N, ?+ o+ U4 Z M1 f" A& l- b
Recursive case: fib(n) = fib(n-1) + fib(n-2)* ~0 L0 \( n. s7 l
4 z6 Q P" ~6 \* Q6 N! X$ F$ l
python " k# B/ r& Z3 u; f% Z w2 E7 u, j$ F2 h0 V( g! M# @, r8 h- A7 u
3 G$ x1 Q& q7 k2 G/ l
def fibonacci(n):2 c& m$ c S" K
# Base cases! B e; @% R0 P4 L% \- b5 B
if n == 0: 3 E. f% j4 M, v, ]( I return 0 w" y- J, @9 b" N) A elif n == 1:. m a/ ~' N$ u- L
return 1' u( H( m0 Z D5 q: X
# Recursive case # n( J4 V" V4 b O- d* o- s$ @ else: $ ~ M' ^: H0 E7 ^ return fibonacci(n - 1) + fibonacci(n - 2) ( s9 b: K0 C( t4 w( L( ] ( Y" b7 e$ O# a! T* X- i# Example usage* q. u3 G+ t5 W h
print(fibonacci(6)) # Output: 8) F; B. W- L% |( O" X
: W: W, C; T2 GTail Recursion/ c) B7 n" W6 |9 A6 L' L5 [
' T6 I; |6 r" s% cTail recursion is a special case of recursion where the recursive call is the last operation in the function. Some programming languages optimize tail-recursive functions to avoid stack overflow, but not all languages (e.g., Python does not optimize tail recursion). : M/ R6 q: o; l8 o! a5 c7 U7 U, N2 n" q, `
In summary, recursion is a fundamental concept in programming that allows you to solve problems by breaking them into smaller, self-similar subproblems. It’s important to define a base case to avoid infinite recursion and to understand the trade-offs between recursion and iteration.作者: nanimarcus 时间: 2025-2-2 00:47
我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。