设为首页收藏本站

爱吱声

 找回密码
 注册
搜索
查看: 3175|回复: 3
打印 上一主题 下一主题

[科技前沿] 突然想到让deepseek来解释一下递归

[复制链接]
  • TA的每日心情
    开心
    2025-9-8 05:08
  • 签到天数: 3 天

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    8 j" Y1 r, u3 y. O* u
    ( C; `7 i, Z/ N, i) B解释的不错) S! K! d* i" p' w

    6 R! d; |! q5 I4 I递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 y- @$ B) L5 X" s' X. I. f" R0 A5 f

    ' @* s1 Q( e- X 关键要素& l+ \/ E# r1 ]! b7 E% q" d! u! G
    1. **基线条件(Base Case)**
    6 V; p" }# _3 v- T   - 递归终止的条件,防止无限循环
    / {! A& w7 k, l5 D   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# j8 f, i3 \: t4 W6 N0 Y  M

    % P, V1 s3 y4 u% z: S7 K! y: ?2. **递归条件(Recursive Case)**$ ]; i: @+ ]+ ?- z/ @
       - 将原问题分解为更小的子问题2 A0 v$ Y1 ~3 ~( w0 f. U; t8 Q, L
       - 例如:n! = n × (n-1)!
    / K2 i- n3 c- w2 A- x; p" N, a9 F. }
    1 u% A0 e2 O* ] 经典示例:计算阶乘
    : B2 ?9 m+ f, Y  gpython
    ! ~, N: s- V4 r2 Ldef factorial(n):7 W% a" a9 J( f7 k( L7 P$ `6 ^
        if n == 0:        # 基线条件
    $ F1 c  O1 C7 s* Q4 W" C        return 1
    0 Q$ ~* T1 x; f$ {3 `2 g  G    else:             # 递归条件8 T% g5 Q4 E% A, L5 _
            return n * factorial(n-1)8 U9 O" w: z; }8 h6 Z
    执行过程(以计算 3! 为例):/ P8 s9 i( E) h0 s
    factorial(3)
    & D. C; I! Z/ R( I5 i3 * factorial(2)
    % U0 ?0 y1 I& o3 D. p6 p' g" X3 * (2 * factorial(1)); ?2 }' H: U4 W4 C! G7 N/ \+ b
    3 * (2 * (1 * factorial(0)))
    ( ^$ L- k4 m1 T/ P- b3 * (2 * (1 * 1)) = 6+ X2 `; H4 J& c$ F

    3 m, `# f& Q: a. `2 J7 s; |# z 递归思维要点! }2 ?! Z/ a2 O5 m
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* D1 y% V1 Z. ]
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    3 M9 s% C" o3 r" K' b% ~  C3. **递推过程**:不断向下分解问题(递)
    + k8 H8 O% Z, V5 E3 }: d# x4. **回溯过程**:组合子问题结果返回(归)
    ( d( k( h" d8 B9 y* Q) [
    ' O7 w3 V. P  Y8 |; {- Y 注意事项- R: O. _4 ]$ [) I$ d7 L. o8 \$ n
    必须要有终止条件
    4 U) S1 m2 E$ g, g' }递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    0 b/ U; z6 \8 D1 G3 B: {4 z: ?: _某些问题用递归更直观(如树遍历),但效率可能不如迭代
      J8 s+ q+ R% N5 M9 d尾递归优化可以提升效率(但Python不支持)
    : i0 @# c' e% x+ ]; Q+ B  s  m( u8 {* j) @
    递归 vs 迭代( @  v3 j9 ^3 \+ x/ K) F
    |          | 递归                          | 迭代               |
    5 g5 W! H/ e3 i|----------|-----------------------------|------------------|
    2 ^) r/ ^0 z% f) l| 实现方式    | 函数自调用                        | 循环结构            |
    0 a. D; t/ T: X" a| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ) k0 v3 o/ ^# v8 w) j& n% g| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 Y% t6 _4 x& {9 G9 K) P5 W6 Z| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    0 Q0 C, I, \* X( M: j# b
    ! j% G( n- ?4 m 经典递归应用场景
    + K. m# ^! o% P1 D- y( n1. 文件系统遍历(目录树结构)( k6 k2 Y* g) D9 I" O
    2. 快速排序/归并排序算法5 U: Y) D5 R9 B. [1 V
    3. 汉诺塔问题
    * p! C3 p. |) _' Y; J: T4. 二叉树遍历(前序/中序/后序)
    ( c$ Y) ~' D; X& h- ~% z' m: n5. 生成所有可能的组合(回溯算法)
    8 `% L: g  l) i4 G, k8 t7 Y- @# L8 b1 Q1 X5 H7 D  F/ \( Q$ z
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

    参与人数 3爱元 +26 收起 理由
    pcb + 4
    老票 + 16 涨姿势
    住在乡下 + 6 给力

    查看全部评分

  • TA的每日心情
    擦汗
    1 小时前
  • 签到天数: 3332 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( o3 }) a$ }3 h, }/ m" q/ P
    我推理机的核心算法应该是二叉树遍历的变种。- R. x6 b9 H. y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    板凳
    发表于 2025-2-2 00:45:59 | 只看该作者
    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:, q& A% h8 w. J" e3 \$ L
    Key Idea of Recursion* T( s! O- S5 d/ [, [$ }

    $ F8 |  b6 W  G) ~$ f2 U. U; BA recursive function solves a problem by:3 ^0 Z8 Y) k+ n( l4 C4 r
    5 E: b6 a/ E7 G' U9 ^
        Breaking the problem into smaller instances of the same problem.
    5 C0 i+ A! F7 G4 p& ^
    5 J: ~- w" C  b1 C+ r    Solving the smallest instance directly (base case).( y8 y! A6 ]$ X+ T& \
      @+ f9 a0 S; n* S1 F
        Combining the results of smaller instances to solve the larger problem.  A1 y% g4 h/ a( P/ n- t  b
    $ E; R2 y; ]# n. m6 }+ n  b; j# R
    Components of a Recursive Function
    1 _  h1 ?4 O) I: `+ T) ^
    . U" ?$ d( X! v# S: h; i9 [    Base Case:
    " [% f( B7 }9 V5 Z% {# w1 N( v. I2 S0 Z( Q* J/ U8 f. e
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # B8 l- F; A  z$ b7 W& y0 _9 D0 A% b6 o* k
            It acts as the stopping condition to prevent infinite recursion.
    , z- R7 i( {8 N% Q& H3 W, J
    0 }0 u. ?8 ?. [& @9 t, A9 D        Example: In calculating the factorial of a number, the base case is factorial(0) = 1." T, ?, {. N* f, [' F1 W
    $ V4 Z8 M0 a* t4 G& S* \& e: \9 r
        Recursive Case:. C0 a# L* g- K# r$ `4 b2 e! m
    4 z" e: p' i& u( Y: a8 |: V
            This is where the function calls itself with a smaller or simpler version of the problem.
    4 P% f) j1 {! B; u/ p- w0 |  @: e1 z- q& {( u
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    8 [) R3 j$ _/ i/ @5 q$ h- T2 i  L( V+ g
    Example: Factorial Calculation4 M( V5 y+ Q8 j  }9 _

    ; h5 C; Q3 D3 `+ C, iThe 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:8 e0 z1 I; W% z# V. f
    2 @$ N" ?' @/ e3 A2 h: E
        Base case: 0! = 13 k2 q9 @. z8 ], ]/ M

    6 r" ~  l% z& Y& G    Recursive case: n! = n * (n-1)!
    3 e4 c' U# {- w$ Z
    7 B. e6 d& V5 ]8 w9 R" s" v. V8 VHere’s how it looks in code (Python):
    # @  p! }8 P# z: B8 Q5 Zpython
    0 {( ^% J2 ^; z$ }
    / W; \  @' k0 R
    3 G# ~( \2 i0 D( N/ |# w2 f$ rdef factorial(n):
    5 v6 G- Y$ Z" S; _* ?    # Base case
    , R" t, n8 Q6 u/ V    if n == 0:
    - d. z  a7 u8 t/ n8 r, _        return 1
    , b7 z* A( P; A. E0 I  O) y    # Recursive case9 t& h0 _1 f+ i, q2 @1 i4 T
        else:
    . _8 N$ _. h& c8 T        return n * factorial(n - 1)# Z2 ^1 A. l& `2 Z: ?) F* ]
    . `. _. N1 g9 S' M. {$ Q7 K' X% m( E
    # Example usage
    + \5 _0 b# L; U% X+ b% I1 ]print(factorial(5))  # Output: 120- t/ }0 Z" Z/ x4 X) ^0 t

    5 v" E8 c+ F7 K% S+ KHow Recursion Works
    ) Y9 N6 \4 y6 L( D0 S  c/ Y; \6 p2 L
        The function keeps calling itself with smaller inputs until it reaches the base case.- s) o) X$ T% A; p2 O
    9 B& E4 T4 b/ G* ~
        Once the base case is reached, the function starts returning values back up the call stack.7 n4 B; |0 v% y

    ' k' ?. m, R- I0 L- z& v    These returned values are combined to produce the final result.
    $ W$ r  [$ R) I+ v/ c2 Z0 \0 M9 g+ y, u4 x$ A5 {, u: t% F5 Q
    For factorial(5):% ^# o* w" d0 x$ p) l6 E; \( p2 r2 m
    ) x5 a5 ]7 [. b2 P0 d4 v2 `

    : g0 [; l5 z1 d( e# Lfactorial(5) = 5 * factorial(4)
    8 \% [- A& l( m+ ?2 y* ufactorial(4) = 4 * factorial(3)
    : H) g* ~; z* T; V  w( T+ D, Mfactorial(3) = 3 * factorial(2)
      u5 A( V& x* b( B8 Ufactorial(2) = 2 * factorial(1); J/ c1 F" e/ l- n& m
    factorial(1) = 1 * factorial(0)' d$ m( I. ~6 r0 }9 |6 f  k' S
    factorial(0) = 1  # Base case
    3 X1 h  Z% X7 [" m8 A( I; b; h, e
    * H( Y: J9 N) H" d+ M( }Then, the results are combined:
    8 y% w# B9 M, X$ B  L3 ?- Q3 {, h! I6 `' b4 B- \8 R/ g3 U

    . O2 H. z8 }0 v" |0 X: k& rfactorial(1) = 1 * 1 = 1  c/ e+ l; H4 L& b' E6 @* ^1 d
    factorial(2) = 2 * 1 = 2
    ) M! j& [  g/ `1 hfactorial(3) = 3 * 2 = 6
    ) @3 T8 o% s  Q9 @! }% i" `factorial(4) = 4 * 6 = 24- q, I# j8 }% {
    factorial(5) = 5 * 24 = 120! T- f0 j4 K2 O2 N8 L2 z
    0 Y, G, }# v' u9 c& e
    Advantages of Recursion; ~3 Y+ h7 H% K3 _. q* J

      _9 E/ O& X+ \% B3 k2 O    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).: o* z: Q) b8 t9 o/ ?

    # ]+ P* Q; T& Q5 @2 s0 v( Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.  S: e; z# j( I1 j7 m! E+ O. o9 m7 G
    ; z% i$ k$ `3 r) a: \+ P
    Disadvantages of Recursion
    ( I" ?9 S" p3 S5 g& r, J
    9 ]  i0 N: G1 e' m0 k& `6 d    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.# }6 t* u$ f8 r+ J

    & |3 x& S7 u$ o2 j    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 [8 r1 H3 f" J3 Y; U! _4 Z4 K

    9 q3 t. Z' i! t; Z$ @# @When to Use Recursion
    . F8 Q" {+ N1 W9 u" ^, c4 R& I8 m: L) B% c
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).$ P) y' \9 b$ G8 A5 p; s0 B) c

    4 _7 J* F9 F: x$ {4 I: b- F    Problems with a clear base case and recursive case.
    $ b+ Y/ W+ D# O+ a+ p/ v8 S$ P- }% d9 j; u
    Example: Fibonacci Sequence
    : Z, r9 t* v; K9 ?  R7 v3 Z& \1 y: J% d( ]0 _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ X2 }% y5 a: |" u" ?4 D# A& W- s# W
    % F7 D0 [$ o, B% f  F, L    Base case: fib(0) = 0, fib(1) = 1
    ' a/ s) m- g) F8 W( v
    : q% @/ q5 H& R: o& v( `    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    1 U0 c: o( R. j. R3 U
    : i* K: ]& P% ]) p2 lpython
    7 _) x- }. T: g0 _( j7 ~9 l6 e3 B, i- {6 e, L
    + |: ?9 }6 @8 h  U0 D
    def fibonacci(n):/ `" E/ k. f" t6 q& @; B
        # Base cases
    ) S  Q  c9 E0 x. T/ G: U" B* C    if n == 0:9 Z/ N; r9 U7 ~& Q; G* f
            return 0/ w% m: ?6 P* K6 r: u1 R: u( {, |
        elif n == 1:
    . g9 J* ?/ N& }* M; ^$ F% M        return 1) ^# m) }( z# f" g
        # Recursive case
    0 t" l3 L, y# {  j    else:
    * p' f  g/ {7 Y. w+ I2 k/ z  X        return fibonacci(n - 1) + fibonacci(n - 2)
    8 }# w1 R. S/ z) ?3 f
    9 i5 \1 k$ V6 M9 m6 C! _# Example usage
    4 X$ K  n5 B6 f* U2 b9 Mprint(fibonacci(6))  # Output: 8
    5 h' o/ ^) f5 w+ F' ?5 \
    # T  B7 I: n* V( sTail Recursion
    ; F% y' @% Q' g
    4 @9 I) A+ g; x8 i% J( @  D( I& LTail 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).5 j4 Y# E; B7 n6 Y% |% A

    : h5 k2 A) C9 Q' g9 S5 aIn 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.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    地板
    发表于 2025-2-2 00:47:27 | 只看该作者
    我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2026-8-26 07:47 , Processed in 0.059754 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表