设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 A/ L0 W4 j0 A3 r$ I, T' H# M8 L! `4 Y1 O; L, B2 G8 f7 f3 s
    解释的不错
    8 H4 k; Q4 m7 z* ^1 a6 N$ V/ s" o+ A& z. X! @
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 c6 r" T7 S/ T- @  I1 r. V; ~# O
    1 k7 B3 n' |& H6 j4 ~( ?. H/ d 关键要素
    0 U) z, d: L- }& N1. **基线条件(Base Case)**$ F; B6 y& f; E- ]( u) N
       - 递归终止的条件,防止无限循环
    4 L: E7 Y% Y: X; i5 E" p+ ?  H   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 16 u0 p( `( B' [. T$ a* L% T5 |
    0 N7 b/ C/ y# x) @$ V( K
    2. **递归条件(Recursive Case)**
    $ `# f9 T2 O  Z$ [5 C& g' C* G2 D   - 将原问题分解为更小的子问题. T. {& w3 S9 O( t) b9 ]
       - 例如:n! = n × (n-1)!: v1 `) a, s+ Z& S6 z

    $ p" G: o% ]5 P7 Z 经典示例:计算阶乘- Q1 Q9 d* G" Y) S0 M
    python
    8 `& T" n' t7 ^0 F6 Jdef factorial(n):  @* T4 Q2 W1 U- {
        if n == 0:        # 基线条件8 V% K9 k) L  _" Z/ K* Z: G; n
            return 1  ^( Z5 @# y+ L" Z6 e
        else:             # 递归条件
    + f& |/ n9 _) P: E7 B        return n * factorial(n-1)
    4 R3 h4 c6 Z4 e% V: M8 j4 e" s4 i执行过程(以计算 3! 为例):
    5 b; Q) F, L- N" Mfactorial(3)4 A8 V& b/ Y: u- G0 x
    3 * factorial(2)
    / n% h* I4 L- ?: T: d. B6 h3 g3 * (2 * factorial(1))
    - s+ \8 s! O- y- N3 ~& q5 e+ W; ~3 * (2 * (1 * factorial(0)))
    5 r' q* O8 {6 P3 * (2 * (1 * 1)) = 6
    * {" B7 F% T; I& O# \9 V1 s
    7 ]1 h" ~; v! B- s4 L4 T8 o/ {7 J 递归思维要点
    ; z5 G; k! m  s* }" K2 u9 z; _9 r: i8 s, y1. **信任递归**:假设子问题已经解决,专注当前层逻辑8 R: G8 w$ Z6 W! \
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)8 Y' Z! n1 |" ]% v+ _6 Y/ B# J
    3. **递推过程**:不断向下分解问题(递)% v1 d( n. f9 X, Z
    4. **回溯过程**:组合子问题结果返回(归)8 w! H1 H" R" `. N% [" ]

    ; _) G% @8 u) p* O# ?3 c 注意事项
    9 m( X' v6 Q! M0 X8 X  X1 `, l: \必须要有终止条件
    $ F8 a- m8 l3 }" a' O; \递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 G: C9 O2 O4 P, b+ _+ H1 s2 J某些问题用递归更直观(如树遍历),但效率可能不如迭代
    6 |) Y8 y( m" ^% u尾递归优化可以提升效率(但Python不支持)
    * C5 d  t$ ?6 K9 V8 E
    . M( z) ~4 i% f" F) w( P5 g: b4 U 递归 vs 迭代
    2 j* i2 ~) L5 P  @  m0 u; ]4 Q|          | 递归                          | 迭代               |
    3 i7 d9 R# ]# f6 l* ~! W|----------|-----------------------------|------------------|8 q3 o& f; q- ?$ f/ `1 `
    | 实现方式    | 函数自调用                        | 循环结构            |, [7 Q9 z: i1 v9 v& y, \' j5 c" s1 q/ I
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 t2 p: f; W9 [& X; ~2 c% ]
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 w$ ~) ?  h% R% I: E
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    3 o8 w) }  X3 T/ b9 S! V: O* w; ]$ H; i3 L1 l5 n
    经典递归应用场景
    ! o8 p8 Q7 `8 w, Y  U2 [1. 文件系统遍历(目录树结构)
    1 F8 D! J, q) O! C/ B2. 快速排序/归并排序算法
    ! S) P/ G% h6 A0 l; ~" K3. 汉诺塔问题
    , \2 P1 j! }2 N' z$ V/ u4 _- e3 f/ `" y) h4. 二叉树遍历(前序/中序/后序)
    : V$ j0 u6 E& }" v; n/ C4 H, N; @5. 生成所有可能的组合(回溯算法)
    9 K- b% ]5 K8 z8 U6 `7 W! F% p
    4 v5 ~# s9 |0 ?试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 06:59
  • 签到天数: 3385 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    0 ~3 b# Q9 C+ H1 R9 d; [我推理机的核心算法应该是二叉树遍历的变种。/ {9 w+ L2 j( g/ H1 z$ O+ ?
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:  u5 F+ Z: Y! d- y
    Key Idea of Recursion4 B/ Y7 o( O, Q
    , E5 b. D! z8 q2 g
    A recursive function solves a problem by:
    . X% f/ s. d6 k
    & k; U" E9 }( p! t    Breaking the problem into smaller instances of the same problem.5 H8 R6 t4 p5 A+ H' w
    # J$ [& q! i# [
        Solving the smallest instance directly (base case).. i" S. ?( I% d0 n" L* T* L
    + ^* b& Z2 j$ P' ^0 k( n
        Combining the results of smaller instances to solve the larger problem.# ~& k) ?4 {# W7 r( @' Y
    ' E6 G/ z/ d/ f2 Q" l3 {6 J
    Components of a Recursive Function
    5 V9 o5 N/ I' l9 J) X" B9 ^3 K
    - I3 m7 U; Q: D; E" J( l2 Z% ^    Base Case:
    ; M, i1 Q4 M4 {& \9 L* _
    3 b- m9 c" \1 ~1 s) `3 Z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 }5 Z4 T9 a2 ]3 I  a, Q* o( s0 l# P" O
            It acts as the stopping condition to prevent infinite recursion.
    2 Y6 K1 m1 D" D1 f* y! w' ~6 J9 O9 U+ i9 f& B& Q
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# A0 a- f( t& g5 |! J+ Z9 [9 U
    9 r3 I  |4 B+ p8 C& n- D: [6 x
        Recursive Case:4 `# ]4 p0 ~5 @- K" `4 {
    6 w/ P- t0 [5 b, t5 \
            This is where the function calls itself with a smaller or simpler version of the problem.
    ) m, Q0 f7 a. k0 a/ q( F8 `& ]& U: b7 w' R; e
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    * z7 u" k8 I3 y4 r" T* _5 C
    7 l1 c( e) r$ L, k8 Z& Q$ G/ @Example: Factorial Calculation
    / F: |9 U9 L& ~  H7 @
    ) n0 v$ T6 G2 S  @4 J4 jThe 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:
    & V# R% C, U/ f5 L: Q# u# z( U" Q+ w' x# R7 O
        Base case: 0! = 1* \; f+ e* z* @) @! _0 O: P4 j7 s& [  z

    ( H6 @' t' r& [1 s) M2 ]0 A5 ~, K    Recursive case: n! = n * (n-1)!
    $ l$ F3 @% q2 R0 i; G8 T) m, ]+ b: i( i( K
    Here’s how it looks in code (Python):8 r( A/ A/ M% Z: X  o
    python$ `9 Q; ~" v6 ~1 I6 n

    . ?- \% S7 C0 v' w5 y
    9 ^5 B5 {: e& {* kdef factorial(n):5 n) q  n5 D9 O9 b1 q
        # Base case/ F9 l1 J' a( A" p4 u
        if n == 0:
    3 b4 u8 i: H+ F6 a' J8 G; X3 f5 n        return 16 x7 h8 e% [* W' ~2 Y4 f# F  b
        # Recursive case
    $ D6 b' w$ l4 d2 N; t  {! b$ q    else:
      C! C% n, K1 ~; t9 p- w0 c        return n * factorial(n - 1), T4 j9 H; a! O  L1 B* p$ c! F: Z; S, F

    " O$ ]7 F0 x/ U- ~# Example usage
    7 q* U. W9 @5 ~; K% x+ ?: @- uprint(factorial(5))  # Output: 1202 F8 w" |. X, z+ o& U

      b% o- T2 w3 O+ e, K: ?' oHow Recursion Works
    * e: N/ f2 f1 @' I, D! j, a
      W9 `  @- T2 J+ v$ U' H  T    The function keeps calling itself with smaller inputs until it reaches the base case.
    * q* _6 ^" ]( Q- B1 I
    $ i6 B# v8 W9 P8 R    Once the base case is reached, the function starts returning values back up the call stack.
    . t! B! _' V* x' s
    : X* a2 X, b/ N* H    These returned values are combined to produce the final result.
      o7 X. e1 M, @  E
    # ^$ V  ^# Z2 z) M8 hFor factorial(5):
    ( r- g. j9 N0 e: t6 `$ w
    ; ~5 t( ]2 X$ K7 i. h
    , o4 u/ O- V2 g& ?factorial(5) = 5 * factorial(4)6 V$ X0 b) u: @$ S$ C7 X& G, @
    factorial(4) = 4 * factorial(3)
    4 ^0 y% ?! M1 R8 X2 a% zfactorial(3) = 3 * factorial(2)$ l$ {/ l* b6 ?+ B' q* Y, z* M" m
    factorial(2) = 2 * factorial(1). q" x9 T6 p% @! b2 y7 T0 M
    factorial(1) = 1 * factorial(0)
    - o& L4 T/ z: @% @, E5 x2 ~+ N1 M. ]factorial(0) = 1  # Base case
    - I, S" R% O5 E% O
    9 G3 e% x% ^# K( |4 ?" xThen, the results are combined:
    ; c7 o1 `0 i6 T: }6 @! L* w. |9 `7 ?, H

    6 x! z; n" R; a: [; m5 K, Ffactorial(1) = 1 * 1 = 1' d7 B5 [* E3 t0 I8 ^" P, f  H
    factorial(2) = 2 * 1 = 2* n9 q  ^& b0 J" H1 r. f$ [
    factorial(3) = 3 * 2 = 6, V" c4 t/ d/ W
    factorial(4) = 4 * 6 = 24
    $ Y9 L$ w3 q1 c# Cfactorial(5) = 5 * 24 = 120
    $ }) e. F: T1 L) G* p  `
    ! j6 W# m8 o# O' \2 SAdvantages of Recursion
    % u# u* h8 W4 y3 f5 f% `0 ]
    % C2 g! X- c: r1 S$ M    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).) G4 J8 ^/ F* {1 D2 W! \
    + o( `8 l4 n8 d2 z, v
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 H1 \# p+ ^8 }) J' a3 X6 w" }" M( @3 o
    Disadvantages of Recursion
    * D9 z( s2 q4 l. S& r8 T8 g6 T% w0 \8 ]  f- g  c2 p
        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.
    " [: i. j6 \% t! P% i: G6 p( e( t; l. h
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).( \: Q6 R$ x* H( c- |, v2 v  O

    3 Q* a9 N3 Y/ i8 x2 X7 xWhen to Use Recursion
    8 U3 T. Y$ V- n3 ^7 U% u* H- c) _* M4 X% G  ]9 h
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( Q# i3 R9 B9 Z8 h- R4 f& W

    " k) m, ], q) E* c* l4 w    Problems with a clear base case and recursive case." N/ ?* m& p% }5 z: c1 C$ u* h
    ; \) v4 I0 L! p
    Example: Fibonacci Sequence; T% W0 V3 Y8 }  |

    % U7 _( f6 y, E7 N1 cThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:" U9 i5 E, z: y" z
    $ K9 b3 x$ g  B5 A; [
        Base case: fib(0) = 0, fib(1) = 1' ~0 C2 P1 Y5 x  E( Q
    1 @: \5 ^* Y1 r" F: M9 k& R4 u
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    5 D. e; ^; o& Y6 u" P' }4 j  G, u! X/ m5 T
    python
    ! k2 i/ @- ^  v, F% ?$ v: ]1 M. k4 C& u% D* j' ]9 k4 s
    / }; I' N6 z, A' I2 x+ P3 Z2 t
    def fibonacci(n):- c5 a, g! b& E  c
        # Base cases
    $ f' ^7 \7 V+ w) R7 [- R    if n == 0:# o! j. j& p! e8 V- p
            return 0
    ) p- G" N7 Z- Z; G. d0 w7 _    elif n == 1:
    6 c4 }. b9 Y7 ?/ E2 y6 K) f        return 17 @3 @2 S6 q  F, A8 I
        # Recursive case
    6 R2 z7 O! M. M1 K& N    else:& `2 V- s; L, ]1 B7 v
            return fibonacci(n - 1) + fibonacci(n - 2)0 t3 a$ V  h* n
    ! Y# ]+ B# Y; @6 A6 D" ?6 }
    # Example usage8 O/ ^. W# c. f7 i0 a& X6 _
    print(fibonacci(6))  # Output: 8/ O- I: {5 U  M7 [/ a' `( t3 f, a

    & V/ H1 \4 m: ~% |: U* v8 y  DTail Recursion
    3 c: R# Z; B7 D$ i& H+ r3 o1 h
    5 C  L( f  n0 E3 m. n0 QTail 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).) Z% n. e- B; M+ T
    % A9 ~. p/ {" [: t, ]
    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.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

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

    使用道具 举报

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

    GMT+8, 2026-10-22 01:49 , Processed in 0.062163 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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