3 ?. V% U7 d; L) A) j4 ]试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,7 Z5 }4 J; |7 c/ y2 x* V( p
我推理机的核心算法应该是二叉树遍历的变种。 2 M/ y' `! V7 R9 e另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: 5 x8 B( L: }2 \* TKey Idea of Recursion ( ^* u6 i, v4 X 1 h; N1 W( }' l. |7 I; u: @A recursive function solves a problem by:+ i7 X3 F1 ~! ~: ~
2 g# U ]( `; K
Breaking the problem into smaller instances of the same problem.3 j4 z' H8 q; I' L6 J
0 L- j& d! u" Y1 X$ ^
Solving the smallest instance directly (base case).7 s; a: r# \) W* l
/ L" W3 c) k; D! ~1 g. O' E/ | Combining the results of smaller instances to solve the larger problem.. h+ B% D, u7 D5 {& V
) Q1 {& n, j$ b) s! c3 ZComponents of a Recursive Function! L( g7 `$ y: ^
, x# e* F% i! m- Z* } Base Case:8 k2 w* ^- Q }" W% e7 g0 [
' S7 H: w4 ^4 j, v6 M' @7 ?& ]
This is the simplest, smallest instance of the problem that can be solved directly without further recursion. - _/ v7 m4 f6 ~# w, f# g - ~: s* d. y7 H It acts as the stopping condition to prevent infinite recursion. + J! {, Q. A1 r4 H. y1 m+ r; Q 3 B. \( z) o5 a! o6 S3 G' E1 f" a) u! j Example: In calculating the factorial of a number, the base case is factorial(0) = 1. 7 v: V* v4 p- C # e9 q0 Q. u8 i Recursive Case:$ |4 w: t8 O+ @$ F
- E- x& x. b# Y1 V |/ \8 g" g# K This is where the function calls itself with a smaller or simpler version of the problem.' X5 o \# ?7 T+ P+ _5 J% m& n
7 |( c/ F. D. c$ n. k
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." A* B9 B3 k- K+ h# f7 ]8 D
4 S" m8 O4 }- |# H* E. wExample: Factorial Calculation 1 v$ K7 A3 Z3 N% Z& k + j4 \* o! Y- y! b0 zThe 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:+ k( u; A- \, @9 o8 D$ p) z
+ `2 Y1 _, F& g3 z Base case: 0! = 1 " B" ]" n0 N8 B r: C0 ]/ [3 N1 t2 I& b
Recursive case: n! = n * (n-1)! % E. `* r& o; f+ m" m9 r$ _ ! @4 z+ w- b- i7 \Here’s how it looks in code (Python): 4 |* S! s3 F9 O/ w v7 E, u% W$ Vpython" t6 `: C9 c+ i2 {- N2 M3 E$ i
0 R4 R8 e f: k) r4 Z' F+ Q" o- @$ J: [/ a( @* Z* b% J* ^7 ~. U
def factorial(n): % c5 i2 e2 V- O # Base case! S3 U& k7 K( j; }) D$ S
if n == 0: + k/ X# q( Z' \/ z. _ return 1$ D2 H4 Q6 q) c. g" f$ F
# Recursive case+ b5 j5 a* @. K; |8 N) l# b4 D
else:1 z2 U8 H% O8 F! I6 H
return n * factorial(n - 1) , Z- N ~6 S j* O ; v8 g/ X+ j9 v [2 B3 ^# Example usage: O' s, t/ ^( c' p* `9 ~0 P
print(factorial(5)) # Output: 120! H7 A( x2 u: c9 q, E
z Z0 u8 u: f8 E7 ?. VHow Recursion Works ) Y& P' r2 \/ a+ \% K% h5 \6 F! ?8 V
The function keeps calling itself with smaller inputs until it reaches the base case. 7 d R L/ j! b1 f7 x5 [, u# q) P$ P1 M& F
Once the base case is reached, the function starts returning values back up the call stack.$ z3 z5 H" L, V+ k& N/ d
: J* d5 ]* ` i8 r5 k+ h- i g0 O These returned values are combined to produce the final result." g. S$ y, m2 A: F. ^
& ?5 T0 f* j& I: l, P! j0 M
For factorial(5):- O% A: ?0 |* Q' [
# n7 y2 Y; h3 g9 i Z! G. |
' p0 G: U% k7 m" N5 k. `
factorial(5) = 5 * factorial(4) & S+ k& r8 ?& l4 l5 C/ }factorial(4) = 4 * factorial(3) " A( \1 k# X- L: {7 [3 F: M# dfactorial(3) = 3 * factorial(2) " a+ D, S+ ~3 D9 _$ ?+ `' H cfactorial(2) = 2 * factorial(1)4 Z7 u. c6 q0 w) h
factorial(1) = 1 * factorial(0)" f; k. W( v5 n) i6 D
factorial(0) = 1 # Base case' S$ q( a0 {: e4 _" m& k, x
* n, U; p9 N1 DThen, the results are combined: ! k6 o7 v. d# b% |$ a: f( b4 i2 t; l
( m y7 q1 F6 C9 t6 f+ s U: g
factorial(1) = 1 * 1 = 1 ) F# D4 ?0 p$ S* U* o7 lfactorial(2) = 2 * 1 = 2 ( \3 X3 g, a2 F, n R% H9 H& U- Rfactorial(3) = 3 * 2 = 6; Q7 u+ T7 v% r: R& A
factorial(4) = 4 * 6 = 24 - Z7 z+ U# W7 g. }2 [factorial(5) = 5 * 24 = 120 & {/ A5 s0 C, [' Q. D : R1 Y% l3 W H# t, iAdvantages of Recursion 8 U4 {9 c {2 `% L0 G5 j ) s' U6 o# Z% J! Y6 | 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). ( `- Z7 z) m& E" ]8 g. J 0 b8 m6 z' s8 e* K& F4 `! {( m Readability: Recursive code can be more readable and concise compared to iterative solutions.: l$ H" u9 S; b
1 `2 T8 X8 X" F S& h+ B8 M$ pDisadvantages of Recursion ( T" k: \2 E. ? . I/ |1 l% f9 ~% D2 O. Z% S4 i 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. ( I3 L; N) C5 u) N z( B: i% N3 Z3 I5 ^0 O6 G/ Z
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. U5 E7 W* q2 p
7 |# v, s6 d/ X; [' C# EWhen to Use Recursion1 t% G1 W/ ^: B3 a9 n& B
# l5 B) W& @1 M% q- P: F Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)., c" i7 \% H% [: N3 z$ o" H
7 b/ i3 ?, ~6 q/ @
Problems with a clear base case and recursive case.. |8 j$ c' Q8 F
& Z9 o. N: L |, hExample: Fibonacci Sequence9 S! J; y- ^) R6 _" p
4 D) e7 o" Q- I; ?. x* oThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:7 W; {& k3 d4 s2 u7 r
. `) |! B* J* _7 E3 ~
Base case: fib(0) = 0, fib(1) = 1 + M. ~4 `1 A. H) s( F O2 V" C; z4 M9 f G n
Recursive case: fib(n) = fib(n-1) + fib(n-2) ! U6 ~4 {' b( A9 P A' j 1 u" N2 q. d6 ?. p4 jpython , t# u& u( f3 M8 N0 ]& m . t/ \( R3 H. O * U# W6 Q! E wdef fibonacci(n):4 L. c7 n- V1 n7 S, `$ Y( }4 S6 o
# Base cases 4 e1 k, P- e) D' H if n == 0:/ m& u }3 F* ]: y3 e
return 0* i* L* r% H! a
elif n == 1: " q2 f; a1 y' ~/ y; |3 _ _* A4 H% F return 1. u& ]. u! P( l8 ~; X
# Recursive case " [9 q$ }5 F& |( e: w' U7 {7 |) ? else:% f( r" F+ p/ |
return fibonacci(n - 1) + fibonacci(n - 2)% M0 Q: F6 g c) I
1 s8 ~4 T( \5 H5 g0 a. ^. Y5 m
# Example usage( I8 h" L& Z0 F: k
print(fibonacci(6)) # Output: 8" f3 g2 \) V/ R: ^
( j( c: O( o. A1 k- p% k2 [2 l
Tail Recursion ' Z0 {/ F; W' |# ] - x$ {8 D! o$ }+ m0 mTail 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). + o# a1 t/ {, n$ } _- d9 K8 f) R
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 现在的开发流程,让一个老同志复习复习,快忘光了。