. ?4 x4 Y) b: u: v5 c试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。作者: testjhy 时间: 2025-1-30 00:07
挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 y1 Q0 v8 z, o1 \+ N
我推理机的核心算法应该是二叉树遍历的变种。( ~* p9 H/ b; P6 Z t' Z, f6 C& h
另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:# Y. T, i& y8 N& `5 W
Key Idea of Recursion D. j/ Z3 ^+ h* _ 6 M4 g7 @1 l0 _( }' f3 FA recursive function solves a problem by:% v F1 n( \. b ?& c b
' W+ a: Z9 p4 L r% L( C Breaking the problem into smaller instances of the same problem. ! ? |6 p& z# y* D+ q- G2 n/ M% U9 z' {6 t& l0 m7 a6 f; P
Solving the smallest instance directly (base case). . ]% c- g5 w& \* }$ c ^0 `8 N& k1 O; K$ E Combining the results of smaller instances to solve the larger problem. 7 G: b" a( S a8 T( [# s/ j& o" S* \6 z9 a+ j
Components of a Recursive Function; ^7 ?& t0 x1 z2 _. E
. Q5 O, W9 A$ {" I R' p6 n: }
Base Case: 4 i" @9 P0 Y$ {0 C" }, `1 v/ {! r" ?; Z, c+ y6 C( V1 {
This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' u) K* x8 s3 v: L" O
8 z6 r3 l y- ~# i- `
It acts as the stopping condition to prevent infinite recursion.4 D& B% R$ J' [ t0 C
) c: {1 {$ J% Z- |1 A* D Example: In calculating the factorial of a number, the base case is factorial(0) = 1. C. L3 g0 Q! W
& c* ]& g) Y8 y' d3 c! c3 S* k
Recursive Case: 7 k @, I& ?8 w3 J" G6 U: ~% \- V4 q& h. V" k
This is where the function calls itself with a smaller or simpler version of the problem. 6 I+ c* U& g0 M+ y& J3 f6 `' J7 s% m! k3 a8 H$ j
Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1). 2 x$ h* ^6 t$ G5 ]& A# k) p; I6 v2 ^3 R6 P7 `+ e7 ^% e* U
Example: Factorial Calculation( X* Q% @0 q* B0 m
1 o+ M {' ]9 WThe 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:: r8 A4 C9 c3 F
% ^' R5 N' d& E% p
Base case: 0! = 1 9 Y3 G" d$ U z7 L3 I4 I: P* W4 ~) v; A" \5 O" x) h
Recursive case: n! = n * (n-1)!/ T+ g7 k; c8 t! V1 E# H
7 ^. \- b/ \# h2 w' bHere’s how it looks in code (Python):( N3 n6 E8 D8 P" J8 R3 R) _1 L
python 1 c: M" q% q+ d( G; ?$ C% m5 Z! ]. x5 L+ x5 r. @; D
" Y$ B' b s2 E
def factorial(n): ; C9 k$ r2 f c8 _ # Base case0 L/ m) u2 u0 O( P$ n
if n == 0:) r3 u' [0 {! K: s3 }
return 1 : m/ z+ r/ t9 ]# w0 ] # Recursive case b, W; Z! k. ^! N& K4 W- a else: ( L8 k; [' S9 [8 { T return n * factorial(n - 1); J D' N# W, \6 |2 H8 ^ M& Z
, N8 M& G4 G3 B' g
# Example usage/ p9 X2 u4 x; n- l9 f
print(factorial(5)) # Output: 120 1 P, Q9 d. v' r8 ?& p% f2 x2 X; Y& Z/ [* q
How Recursion Works 1 Q3 u4 J" E) A9 |" o 5 g: W. m. J% \: ?6 ^1 L The function keeps calling itself with smaller inputs until it reaches the base case. 0 W5 p' ]; v1 h' A. W 6 m& r) m% w# f d4 g! N Once the base case is reached, the function starts returning values back up the call stack." |# i3 u8 K! E/ e3 [3 j1 h* e0 M
! U4 C, w ]4 y z' A, A( Y( Q
These returned values are combined to produce the final result. 6 V+ n8 N5 G a' x: e2 C# Y6 p9 c. X7 k/ U8 k8 v9 U
For factorial(5):! ?: C* Y8 }" S8 E6 t: T
# W2 P8 x$ s( f5 c; s7 q$ @: ^8 v ( p/ x8 l! S/ X9 Pfactorial(5) = 5 * factorial(4) / u C4 ]* `0 l5 v0 N# Sfactorial(4) = 4 * factorial(3) 6 D( e' V1 F8 |factorial(3) = 3 * factorial(2)% ]1 f: W; S0 R$ F- l4 V6 z% v8 n! T
factorial(2) = 2 * factorial(1) 9 h# B8 e7 y! Efactorial(1) = 1 * factorial(0)1 ?& Q: i k$ u; l" i+ Z1 b$ F, n
factorial(0) = 1 # Base case' H) d; l* H+ C3 t/ f/ [! n: M/ Z
8 r0 H _5 m3 e; `2 d# @
Then, the results are combined:6 G* M x; V6 U
R' Z: }! G6 S: U6 h7 q5 m! V6 S0 u
5 v% t) }; z9 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). 3 ?+ |: I" ]& G; M& H " Q; S, X. z( W, n' q Readability: Recursive code can be more readable and concise compared to iterative solutions.8 T+ o2 O3 z* l
3 ^" a: V7 p& V( K- kDisadvantages of Recursion) ], c$ }3 e4 T7 x B( b
* U( u( v& P! M+ 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.9 @; `- \; v% p4 h: W6 S2 v9 R, U
3 N- d f0 W* u: Z4 Q' f6 ~
Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization). 0 d5 S& S7 F% q6 O; f# r. U - R c! c7 H% R$ eWhen to Use Recursion2 x6 Y1 W4 B/ |8 C
* F- Q9 v# `- J7 F Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 }, ~( y3 J- \
! }0 Z6 s+ M) m1 F' o9 A
Problems with a clear base case and recursive case.% t9 U0 W" O: N, B% T" `2 `
9 \8 ~* {" p; \% L% t2 I# Z
Example: Fibonacci Sequence 6 F& Q! `/ J9 H6 Q$ ` i0 \; J' }% {3 @. L1 m- s0 j: ]& Z/ IThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:, x' }& N% N" n
4 x$ M1 H1 m' ~
Base case: fib(0) = 0, fib(1) = 1 + w: X) }' D8 r6 j ' Q; u# Z+ x- q8 H Recursive case: fib(n) = fib(n-1) + fib(n-2) " B- V& s' D; F4 \) C3 `6 v$ ^4 f% r- \3 x0 ]5 m. U7 H
python 8 C2 ?4 k6 X6 D( \ ) y& R# l$ m$ _/ H* W* [3 d* A' K% m" W
def fibonacci(n): . B( K& \* E# b' E. Q* W6 N2 w # Base cases 0 @; i1 A0 V, ?* B if n == 0: ' g; ?3 F/ i3 ?% W" ~ return 0 # y6 Q# F6 p5 p. p8 [% K elif n == 1:1 O7 R* x9 C: H, `, F$ ]3 r3 }( Y$ T
return 1. ^+ K" c4 R8 O+ {8 b/ A# g
# Recursive case : z% P7 z6 S. v- Y6 `. C- {* t else: + G( v H b+ X& z return fibonacci(n - 1) + fibonacci(n - 2)$ W0 D) `. D2 c$ I4 x6 Y( A' @
+ }$ J6 @$ k# W& I5 x
# Example usage 3 W' U2 a4 x; p% s5 Vprint(fibonacci(6)) # Output: 82 }' j1 B, e8 Z' L
/ N7 d6 I% X1 N) |, H5 `0 G/ h& }8 HTail Recursion; ^" x% {: G" s! m+ K
1 k! I5 o$ o; @4 N7 Y; s& W/ b/ D
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). & p+ K. l5 O( u8 a% {# x# J % Y, i& w4 O7 f4 Q5 q y4 HIn 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 现在的开发流程,让一个老同志复习复习,快忘光了。