+ b6 k( ^: d- s2 ?$ n试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒, 9 [8 d8 D7 f" I1 B) g& a! r O( F( G我推理机的核心算法应该是二叉树遍历的变种。 - t- s' @; A5 i' C另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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: ( m' K, k9 ~% {6 d, ~Key Idea of Recursion u# I! Q W# V: X) w1 V* j4 A% H
" e# H- J; W' r
A recursive function solves a problem by:% ~$ |0 a; L! g7 w, V+ |4 A
9 X; Q. M; h0 s$ b. N1 c7 u/ ~ Breaking the problem into smaller instances of the same problem. ) F; f7 k+ q$ C3 ^. ^6 L7 B$ T2 T% q3 r0 {4 z: R' y" s
Solving the smallest instance directly (base case). " ~# P, u- j9 M- D1 s9 E 4 F* ]' w, |9 f5 @* Q Combining the results of smaller instances to solve the larger problem., k0 f2 w+ d2 H- S V7 S/ C! M x
9 }+ A- a5 y4 u5 M
Components of a Recursive Function7 N0 B) [0 z- v% [1 J/ A
# e& {! l# V3 } Base Case: 6 R" g5 r; [+ c: ^0 f N& K }0 R% [0 P% @# T8 N
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* R8 L2 D( R! } k6 a9 e* s
$ U3 Z' X; e* i% I# c6 p. V
It acts as the stopping condition to prevent infinite recursion., [0 ~, ?( }4 s4 D8 A
1 \# O% b7 ]: Q" Z6 |' u
Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ Q$ N, P$ D3 @" y7 o* \8 t
7 h9 R8 a" P7 y
Recursive Case: : @6 ^7 R1 z1 R8 j8 j _5 k* o: t
This is where the function calls itself with a smaller or simpler version of the problem. / f$ l4 ?+ E, E6 R( M7 I) F5 i. o/ P2 a) L/ z
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- |& X# B6 u/ S5 I% B {4 y
; Y9 _0 k" Y+ d. t6 M9 j" z2 @7 I3 OExample: Factorial Calculation5 T# i! g% _, k4 I$ F n$ d
- M' W8 V: N; |7 oThe 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: # E9 G, C- w6 C- [' x ; S" }4 k \# }: x( ] Base case: 0! = 1* d( M$ o5 `2 p8 i' Z& ~8 o
. T8 \5 Y: _& t6 p! z7 M1 j% ]* T Recursive case: n! = n * (n-1)!) {) N) c1 ~/ x+ F# t
. T3 G$ M+ R& K1 ~* G
Here’s how it looks in code (Python): 8 G& e) R9 S5 zpython8 ^4 k- |% a8 v' p
' J* ~& R+ z/ \. ]2 p& y0 n- d
8 j' M( r4 `8 B% v7 Bdef factorial(n): 1 w9 Z# U: `$ c; A' G7 N+ l1 F/ R/ y # Base case # d7 \4 e! @- l6 Y4 j if n == 0:8 k% A+ I* ?, V: z; \
return 1 ! p% {4 z* N( M2 ], a) h # Recursive case % V. E: Y7 F/ D6 M: ~% b5 |1 G else: + v& P# F' `) T0 W% j9 N* f b return n * factorial(n - 1)8 H. b' P$ r% `6 R+ E8 i+ P" U* I4 p
; r. ?8 P, N* [' N# Example usage 9 I5 |( n0 ?: qprint(factorial(5)) # Output: 120( J1 m& d7 X6 k- `. Z
/ d; Y. e. N8 A. ZHow Recursion Works 0 u" ?* ~$ o; e# M& B/ H1 X. T+ J9 u4 b5 ?
The function keeps calling itself with smaller inputs until it reaches the base case.& q6 Q& e$ Q. G+ p, w0 C- _
: U- n- B& z; h* K3 h Once the base case is reached, the function starts returning values back up the call stack.6 ~: y0 k1 x- O) w# b
% ?0 _/ @& [7 p. ?! b! y+ s, m These returned values are combined to produce the final result.1 O( E& k; l- p$ H" m9 R
/ ^$ Y) Z/ v; ~% B' G' m% aAdvantages of Recursion 6 B' R8 y# L5 f3 W; \ 0 g% S& X. O C# o5 O9 L7 y( D 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).8 _6 f1 t8 T6 n& |9 D9 d# o" m
( }+ f: E4 H2 V- Z5 m1 m
Readability: Recursive code can be more readable and concise compared to iterative solutions.! f5 k: @/ L; R2 w
. D% ^, N8 J( H0 s+ V/ L/ dDisadvantages of Recursion # H3 T' n) G* U4 }4 D4 ^7 m( G+ ~9 }6 P, R2 a# d- a
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.( x6 o/ \$ \4 V, j% R
2 @# j/ n" C3 O- t7 J# {. ? Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).8 L+ V& ^( M% _' v; n/ _% U% j
2 j- S9 \" u7 }0 x
When to Use Recursion & w1 k( ~; P6 H2 D' y 3 G5 T. b" a0 c( r5 L Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort). 3 N6 S4 D8 F: Z1 t: c) Q0 Y3 Q! G! P- i3 \# D m3 Y& ^8 `
Problems with a clear base case and recursive case., ?6 K9 f* U( n
) Q9 m4 B/ C5 d
Example: Fibonacci Sequence / h, S! a3 L* o ^! F$ [/ N 9 K- |% E. I/ Q0 l' IThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones: 5 s' S s3 m |7 Q1 [ # r+ c/ J# [9 [1 q( T. L3 |( N Base case: fib(0) = 0, fib(1) = 18 u( r/ m" \4 o. C9 d! }0 K O0 \6 j
2 S, B3 ~+ X) L9 K/ ] Recursive case: fib(n) = fib(n-1) + fib(n-2) : q* O* V* i5 k$ l) X1 C! B ; Y" U$ w* m' zpython! R$ \$ r4 G* {* A7 N
% ]+ _3 j3 P# v
2 @9 N ~: E x) G/ N7 G
def fibonacci(n):# J; L, _2 \& l0 K
# Base cases , d/ Y& X3 Q( F6 j' v+ I if n == 0:5 j7 P$ T/ w; g2 ^
return 0. Y: \9 L& B) Y( P: t6 _! _1 Y
elif n == 1: 2 F4 T4 K9 b& ] return 1 2 M0 X2 Z! \, b # Recursive case% Q5 R0 f$ m9 _ ?' Z9 e' M% b7 G% M7 P! G
else: ! A* J% w* I3 y" n9 d; P& z return fibonacci(n - 1) + fibonacci(n - 2) / ~9 m. Q* g0 K' g4 ]+ k# p ; G2 o7 ^3 m/ U: p& p8 \# Example usage 9 u+ p# U3 ~* U( T) \8 nprint(fibonacci(6)) # Output: 8 4 ~* i; v/ P. w* @! T! B2 [3 m: ^0 L/ q( B
Tail Recursion" B! h8 L5 `6 a
1 A: n* {+ R* {8 |+ ZTail 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).- `, x, L0 h) _
]' m8 G8 z3 j8 a9 k
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 现在的开发流程,让一个老同志复习复习,快忘光了。