设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 d( c! G0 v, K/ s  W& w
    " B- P3 v) ^1 i, z+ ?* M& @* `+ N解释的不错
    7 h0 Y; ]5 l2 `8 |' ~" w$ z9 d& a9 {2 l# [0 M' U
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : I* N' O0 I9 I% v! t1 @5 k+ r% `6 J2 I# P
    关键要素
    ) n1 S5 Z3 b" W! E2 L( s7 m1. **基线条件(Base Case)**7 Z. s6 P8 Q- n0 r
       - 递归终止的条件,防止无限循环3 |! N4 v# S* ?( j
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ [: A3 ~; Y9 K
    3 t5 X7 x8 Z4 c& k% @  X2 O
    2. **递归条件(Recursive Case)**
    # {9 o9 F7 M" {7 }1 e, F, r2 `   - 将原问题分解为更小的子问题
    2 @' ?( L# I) U3 K0 @" U- R3 K   - 例如:n! = n × (n-1)!
    6 p- J) N8 [' \8 T' F% K% e
    3 T; C+ L! W+ d' \# }, v  h 经典示例:计算阶乘; U! I  @& J$ j
    python
    ; _; q4 o! o* edef factorial(n):4 \+ ~0 @3 u" ^. u0 x
        if n == 0:        # 基线条件% u6 q% `6 i5 g' }$ F
            return 1
    / [% t: e& D5 x6 a+ t    else:             # 递归条件
    & f# [. O6 T$ A        return n * factorial(n-1)
      o8 J" |2 c* u' q执行过程(以计算 3! 为例):1 \# f' I- B( U* C1 L% z
    factorial(3)- O; M* v8 z$ e( d+ d1 w7 f( E- b* s
    3 * factorial(2)
      x5 p" L, j% i& q- X3 * (2 * factorial(1)). ^% P. u2 M7 b5 V/ J
    3 * (2 * (1 * factorial(0)))
    0 M+ I/ e  S4 g' l; f1 d3 * (2 * (1 * 1)) = 6
    - q3 m/ N. [7 A2 Q) Q* _
    9 `( k  e7 }$ @, t  X5 S 递归思维要点
    6 S0 h8 C, M6 j8 j: n1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! Q, w/ z( H4 b% n  F. O/ a3 K2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! U# b* s) e4 o
    3. **递推过程**:不断向下分解问题(递)
    7 b; g% U; A1 ]4. **回溯过程**:组合子问题结果返回(归)
    6 S5 _( G8 d" D9 a  @9 Q9 H: j
    3 h- z' m4 {8 u" @7 E 注意事项! `2 M* Z# G, g1 h2 M0 ?. ~
    必须要有终止条件
    0 w% C8 M6 `- j' i3 D( ]递归深度过大可能导致栈溢出(Python默认递归深度约1000层)% H, o& [( ^+ `- A. ]4 W4 ]1 ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    $ Q$ Y( [" g! p! x; |! q" W5 K尾递归优化可以提升效率(但Python不支持), _5 f- I, C4 W; |& M# x

    - F9 f# S* J& ]# F, }- N5 Q 递归 vs 迭代
    7 g/ p; q6 e; E) ?! d+ i6 u+ v( d% s|          | 递归                          | 迭代               |8 T8 S6 q3 F, [
    |----------|-----------------------------|------------------|
    ; X7 H, r( _6 g  Y. `0 v| 实现方式    | 函数自调用                        | 循环结构            |
    ; s5 B% B/ o0 N) R| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 G1 v* p6 x' f7 }8 {| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |2 f3 \8 h6 M* V
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. z9 f6 i  A; o" ^
    $ @: U5 g; T$ p  a" }$ Z$ U
    经典递归应用场景6 a! e0 W/ w1 r& y+ @
    1. 文件系统遍历(目录树结构)
    ( i( y" P8 o4 T  Q  Y7 [) u2. 快速排序/归并排序算法9 L! R3 T/ |  W+ M
    3. 汉诺塔问题6 p+ V" R9 ?' m* F3 [( ~% o
    4. 二叉树遍历(前序/中序/后序)1 ~. d# I) t. I* h! r: W& c( I
    5. 生成所有可能的组合(回溯算法), _' F: h! A' Y* r' O

    6 g) T  W1 ]% e0 Q! @( o试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    半小时前
  • 签到天数: 2917 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ n  _( G1 v# D) n: {4 b7 w
    我推理机的核心算法应该是二叉树遍历的变种。9 F$ t' _! F) O" S7 L) R8 w
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    $ A' S* K! |; h+ JKey Idea of Recursion
    : y0 Z# H6 K# h- p5 N- M8 A. n/ `) L; ^: f& J0 e' w+ P
    A recursive function solves a problem by:# E- M2 [$ z' m  l) q3 Z
    " |+ P: b2 r" x9 n
        Breaking the problem into smaller instances of the same problem./ `  s4 U2 I; G% ~+ s$ r

    9 m: Q  s7 y, F, {1 K5 Z    Solving the smallest instance directly (base case).
    / I4 c6 f! P+ B9 l) V; d/ \$ r$ J( r# \
        Combining the results of smaller instances to solve the larger problem.
    - c: K% G3 O; }+ Z& U: c5 V- b. e
    Components of a Recursive Function
    $ V+ b6 h7 R2 O6 {. F% T  a4 H4 G0 l( F$ e
        Base Case:
    % B* Y4 h! w+ n' Q5 W' I, G$ g" N8 i; |& ]" c5 L
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    : W- i! L; Q) W; E3 y
    . ]% O8 J3 f! \6 ^! f, ^        It acts as the stopping condition to prevent infinite recursion.
    6 q6 Q! O* |0 ~1 A
    % H; v7 ^7 e1 w/ K* v; H        Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ B1 {. d; d$ J. `- }* f

      m: v7 W) p2 G6 ~2 d    Recursive Case:
    , E) |$ B. O- e1 l" k3 @$ P& S) p0 g6 N$ ^
            This is where the function calls itself with a smaller or simpler version of the problem.
    3 t- }$ N' [" ^" q9 Q8 O" Y4 [& c+ @# }% y1 w: M& [
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ y% A! f5 b3 b+ c6 t1 S

    8 H7 T# l# W, @  q5 T+ zExample: Factorial Calculation6 H7 U) |/ i' R: ?  L

    # ^- I# l- s" O5 B/ @The 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:
    4 X/ P  M8 b4 R/ r+ d
    , I* v3 p7 S  n8 g/ Z    Base case: 0! = 1+ e* d, N2 a+ M6 i

    7 G/ n2 ~( z* C2 F    Recursive case: n! = n * (n-1)!
    % l7 L! K- {1 M( ?1 H  V, k  W- s& `5 D
    Here’s how it looks in code (Python):
    5 `* _- ~/ q4 a+ Fpython
    1 M( O/ i; ~: [0 @6 r' `$ Z* `  A; U; I1 B8 w( S4 l& N4 e$ d2 S  Z

    : d% [6 Z( n$ O# Gdef factorial(n):5 }1 e* i8 j$ _$ H0 A/ P; E, O
        # Base case
    % o# n( o3 g( {5 B& Y+ g' y    if n == 0:
    , H+ g1 l% u5 B: K        return 1, K4 E$ Y2 Y) X0 o' O, e
        # Recursive case" `+ O+ I$ ]) p5 }! D* _
        else:
    * W8 b7 M! g: j# B        return n * factorial(n - 1)( L8 w  Z* L& j' o. f  a1 I/ _/ g
    $ E0 {; F9 i& {  z. a3 ]- u
    # Example usage
      s0 k& r1 c! Kprint(factorial(5))  # Output: 1202 S. C8 F7 T# x6 D
    " \2 F) {* I" V: o
    How Recursion Works
    1 q+ S: W. b& P1 F' J% t" m2 v6 G6 P! s  m- g2 e
        The function keeps calling itself with smaller inputs until it reaches the base case.
    " v" {( f" t& g. e2 m# j, Y' {( x8 H7 o3 m9 Z; ~
        Once the base case is reached, the function starts returning values back up the call stack.
    2 K2 b4 I$ I, m0 |" t, Z; }0 c9 s' i0 f6 l8 O. }! ?+ x( i0 r/ q% [* S9 V2 r" G
        These returned values are combined to produce the final result.( W& _5 l5 g6 h9 x$ e! g) r9 K
    $ t# ^( u: |1 s* r; ~$ g; [  J
    For factorial(5):# O* H0 d: Y7 t* M$ W4 X

    1 R- x7 x0 y6 ]% f  n' V
    , ^4 N2 u3 H; j" f/ y/ f& D6 `factorial(5) = 5 * factorial(4)( n- ?& z2 _2 E9 ~
    factorial(4) = 4 * factorial(3)
    & F( @; K& A# t: w2 ?) t% Zfactorial(3) = 3 * factorial(2)% A: m# B7 F8 q  X4 E2 e
    factorial(2) = 2 * factorial(1)2 X0 @: [5 O/ c" y& r# \- b
    factorial(1) = 1 * factorial(0)
    & Y* ~/ _+ l6 Ffactorial(0) = 1  # Base case: c( ~) l, Q$ S

    % ^& ^& B+ p; }  KThen, the results are combined:1 p! u3 n7 X+ V* O) a

    2 c+ A! R: G. p# E5 p
    . K7 c* U! m9 T* o; C+ N- \factorial(1) = 1 * 1 = 1
    3 R" a% H4 ?% S9 x  o3 P: efactorial(2) = 2 * 1 = 25 t$ v6 l$ E$ [! W7 V  n" h
    factorial(3) = 3 * 2 = 6
    ' h( v8 a5 ?' o' b3 @1 Dfactorial(4) = 4 * 6 = 242 ?3 z/ [$ a' y
    factorial(5) = 5 * 24 = 120( S& @3 f7 q- I# {4 ?8 V

    ) T+ n/ t6 L+ o7 G( gAdvantages of Recursion3 B* o/ b+ J' [8 B% a! W& @+ _
    8 m5 O1 T9 G. D* t
        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).
    0 W! g4 q" w" N2 u8 I( W# ]; u+ ?* M3 Y5 S3 q' q% `2 c
        Readability: Recursive code can be more readable and concise compared to iterative solutions.3 w  w4 L/ }9 @8 w

    ! I7 D# f" i$ l, Q) @/ CDisadvantages of Recursion
    1 H7 r$ ?! k1 [3 w5 J
    , q& T( k& M& q3 H) g2 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.5 c8 p! p7 D8 M: q/ Z

    % x9 Q4 ~! K% A: P6 b; Q( T. v' U    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
      ]- L5 Y3 l! B- H6 _
    5 M% ^  T# G7 K. E2 UWhen to Use Recursion! J- `2 @# D2 m/ U2 K
    1 j* H/ o9 r0 G% ]( X. D4 A0 a# a
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # C7 |4 R, v& e
    8 ~6 v8 n( }, j1 t; a4 G4 y    Problems with a clear base case and recursive case.0 `, z) ^& X8 D' B/ l3 K- S
    : G  f' _! n6 z7 {% F
    Example: Fibonacci Sequence
    6 s5 D* q( m6 x$ s* F4 @3 O3 U& t5 N
    2 P4 B" b+ h3 E6 O3 q0 JThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 [  z: @1 c9 q
    # h, J" J7 S' H- z, h
        Base case: fib(0) = 0, fib(1) = 15 T3 U6 @. b6 j5 S( n
    - y8 S" G8 r1 l% O
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # t+ I6 z4 U2 @1 d+ m2 S9 H
    ( ~. _3 J. B  |# lpython
    : c+ x8 I2 N) Y$ c+ ^1 }; u) M1 }; K2 x* D: C1 S" t
    0 o) r+ u: w+ N+ j3 `+ ?* Q. a
    def fibonacci(n):+ B. D& F% f8 w; G- F, q
        # Base cases; [3 \7 q+ x5 g. A$ Z6 f
        if n == 0:
    1 g3 Y: c1 ~8 f% s9 {        return 0
    2 S9 N, H; E) A/ C3 D    elif n == 1:" [" F6 T$ r  B
            return 1. u+ m" _: G" |7 j
        # Recursive case0 ?; G% e8 {% A7 |. t9 }2 i9 G, e1 N
        else:
    $ [9 J$ d1 Q' [+ ~2 m; F        return fibonacci(n - 1) + fibonacci(n - 2)- _7 g" o$ k2 Z) x9 q

    * [; @6 y. A9 E# Example usage' R: d9 ]# u: p% k' j  t
    print(fibonacci(6))  # Output: 8% t& b& I3 r: ^, n3 U
    ( Z  X- `. v& B( ~
    Tail Recursion- U3 E5 t$ i5 R8 }

    # H! B: u$ @+ ?3 E! PTail 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$ D5 a2 K, W$ @8 _8 X
    + Y2 @9 S1 b6 T" c2 tIn 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, 2025-5-11 01:00 , Processed in 0.035576 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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