1 a; K5 R( j1 ]) a5 y+ }! j试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( @1 ^7 Q9 g1 s/ T- b
我推理机的核心算法应该是二叉树遍历的变种。 2 E! ]1 L0 u1 X7 Y$ c6 Z( D7 S& q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; D. P: k% R' J6 R+ ?$ R8 w
Key Idea of Recursion ' K& a9 P* E6 g1 _# T1 c5 C. x7 W. z* w7 b/ m) V
A recursive function solves a problem by: $ ^" i$ m3 Z F u! C. r& `2 E3 O# z% Y
Breaking the problem into smaller instances of the same problem. ! n7 n( T* C+ l# J9 C( T8 A( {( p9 I& r. q* A H
Solving the smallest instance directly (base case).4 Q2 i8 K" [ Y& S7 f# s
: r' u/ N% {: `6 r9 {, c$ V8 g Combining the results of smaller instances to solve the larger problem.+ `/ F D5 ?/ A: d% v7 `
1 b' |* ^) H' h2 z
Components of a Recursive Function3 G$ h( m9 U V2 o8 x7 V
$ ]' ^ Z1 f. T1 q# M9 U& A' K
Base Case:5 R$ v1 G6 q! X, x$ @( ]
2 X: E% A* }" @ This is the simplest, smallest instance of the problem that can be solved directly without further recursion.) R9 Z, w5 t9 @
( o, K1 @" \" N5 U4 ~0 ^0 D4 i5 m# d It acts as the stopping condition to prevent infinite recursion. $ h: A4 r: q& ]% q8 C / r8 O& D: C" X Example: In calculating the factorial of a number, the base case is factorial(0) = 1.0 l. L# L4 D8 K9 q( v
% I: I% D+ Y/ j: M; t- b5 x1 W Recursive Case: " B$ z) j# i* V" M! U) D: E! K" H: j2 v% x( B: G: |/ R2 z/ ?# |
This is where the function calls itself with a smaller or simpler version of the problem. * M: f2 r' D$ r$ U: p, d/ Z 5 o% u/ E9 v) ]0 D! J Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)., i0 Q- Y# |2 x4 K7 S/ v% R
( M9 T) P# u Z4 nThe 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: : x; A. y* `# j, t) u5 f6 z' L6 }4 `' B, r
Base case: 0! = 1: \9 H; _- m' Q- u& M: ?, u5 ~; m) n
c3 I3 p" a- p/ n( L9 V v; w Recursive case: n! = n * (n-1)!+ `" W' K; O! t* S4 D
3 S5 }9 q" j5 I b7 Q3 n
Here’s how it looks in code (Python):' Y! S0 x; e2 j( T4 M
python & Y4 N0 v& m, u" G6 \/ v2 e* T0 } v* C" k' W$ d8 g; ]% _* l1 I
$ G9 ~0 _# Q' T) ?% Y1 D9 `9 |8 K2 n# T" m
def factorial(n):- l4 v0 M- h7 Z) Q
# Base case + ^& ^$ u: H/ ?4 b2 P if n == 0:6 H1 c2 [& C% K; h
return 1 3 [; r8 `9 R: R7 H; W1 ~: a" a3 [ # Recursive case* E2 E; c- {6 g4 @8 G; U# ~
else: 9 _& V3 Q- H. |" w return n * factorial(n - 1)* q" D, o: F& y1 k; i$ X
- J; p3 v; M% K" M/ p$ t
# Example usage7 N- S' Q- W" f; X; r' J
print(factorial(5)) # Output: 120$ s) ?' P- `/ x" }# D4 |
$ _* s) Y4 @! dHow Recursion Works 0 L* `3 ]% T9 A+ Z2 h$ }$ ]- B1 U 2 X/ S; h+ l0 D, @( p The function keeps calling itself with smaller inputs until it reaches the base case., p6 Y, E3 v$ u* Z4 b$ Q
% v' L P3 K6 c* U0 p Once the base case is reached, the function starts returning values back up the call stack.1 h- U( X" j4 @- A! i
# Z5 k( V/ a c& }6 a These returned values are combined to produce the final result. - b4 X0 m1 h( z' ~ b' o$ M3 |& a) F& N& UFor factorial(5):$ y! c2 a9 W' a& Y& w
, g) |# S, j# B: l( j6 ~
- l" s7 `! [7 ~1 _factorial(5) = 5 * factorial(4)" V7 U! z6 N5 E' k( v2 u0 E
factorial(4) = 4 * factorial(3), L# l0 J! p% A# X
factorial(3) = 3 * factorial(2)9 O& k& z: W3 J) b/ a
factorial(2) = 2 * factorial(1) I5 h0 p6 v3 d9 K. ?6 o5 Q! |factorial(1) = 1 * factorial(0) q1 |/ @' P7 S) q" H) Qfactorial(0) = 1 # Base case; c* ]$ z% I5 b1 K# ^$ g5 \7 a
8 }* ? w. d0 O& d+ O+ Y3 u
Then, the results are combined: 5 a. J8 Z3 d$ G) z3 t. { K/ z2 p3 d' e! U
% C6 \" q, M6 ~% y. P6 ]* h
factorial(1) = 1 * 1 = 1 5 f, a1 g" e, B5 [+ Z5 ffactorial(2) = 2 * 1 = 2 1 e5 W: j/ O+ K1 `- Yfactorial(3) = 3 * 2 = 6& i$ W# ~. w: ^! _/ _4 X2 G
factorial(4) = 4 * 6 = 24# y/ }$ L0 T+ D L% n) P3 o; }6 ]
factorial(5) = 5 * 24 = 120' E# \* x) ]( K+ D: ?
& M2 S: E7 G" a# ]# i! `
Advantages of Recursion& w9 Q- l$ ]* f7 c7 g6 N
8 h& A5 `' ~: v
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). ; M6 p' [2 d1 b; J0 L$ _" t4 k. R& `4 @. `, {& v0 e4 |
Readability: Recursive code can be more readable and concise compared to iterative solutions. + ]5 m6 Z1 u' J/ E& f$ w! C4 |8 B# ^; A8 p0 o
Disadvantages of Recursion X: P5 c7 z4 Q& l+ J- z* I! J" H+ Z, Q
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. ) f& w9 ]2 m. c6 Y$ w + w' ?- H+ B0 L& k7 r) Z% ] Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 1 ^ ?3 l# S. O, ?8 H. o 3 w. r4 g( K' U) ~$ gWhen to Use Recursion # o* j" `4 E/ R, V # E( D) R% j. B. y Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). - j3 x$ {! g C' L7 b ~. t 7 ^7 @+ b U v- \% ^$ f9 G Problems with a clear base case and recursive case. & _2 F5 E1 Z. W3 b) z8 k1 P4 j! r/ m
Example: Fibonacci Sequence % m5 m2 C0 o: w * c/ b, c2 n% s3 b/ |The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: / W5 ^' \: a5 q/ S) K& e- n7 O% u9 p$ P' t2 `, h* d* x. {4 G3 O+ x
Base case: fib(0) = 0, fib(1) = 1 * s5 y$ ]# F( r$ |. C+ n0 o+ j: C% N* `% m+ @/ e, C: K0 h) z6 X
Recursive case: fib(n) = fib(n-1) + fib(n-2)# z( `% \8 |0 y: P! G4 o8 A/ L8 k/ W+ F
+ Q& y% g& w8 |4 [- H
python 8 B, i% a' N3 F0 n {* N( N9 O $ s B: J5 h J/ E7 x3 N8 C6 J) z8 w
def fibonacci(n): % z& X, u& c) e7 x! [$ \ # Base cases " v! z/ n9 W2 M. y. k5 H V if n == 0:% z6 r% R, J4 R
return 0, p2 _2 `! S( U4 |- a
elif n == 1:; [/ V. @4 X6 t( n5 T) _( v6 p3 X
return 1 / q5 {% k5 F3 L: i% s% J # Recursive case7 ^! `% F$ [, J5 F8 g
else:- b% v0 @7 h6 J
return fibonacci(n - 1) + fibonacci(n - 2) G3 G+ ]" O' U0 r; E. B7 b! O$ e& ?* [0 a1 T: r: f) z: C1 v" J
# Example usage ; {, I) A! r2 e) h9 vprint(fibonacci(6)) # Output: 8 ( B3 \+ x/ C) @ G w$ ? ; i) ?) H* ~; y/ y+ G) H# G/ |Tail Recursion " e, I5 h9 d1 Q6 i% |/ U( M1 ]% o# s7 b6 v5 I' M
Tail 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). / V" f+ Z$ W' l* @0 w 2 t7 d$ l" i" i4 z: OIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。