设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! ?; i8 t" m3 z
    ! P' q! x4 @! e  _# d9 _解释的不错
    . K! b( W# q9 N7 c! e) \/ k1 P" g: }* R
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 g$ j; E( M) b: N% q

    , I( [/ ~- b9 R/ v1 @ 关键要素
    ; b7 F1 N( q% r5 i5 P6 u, T; g* ~1. **基线条件(Base Case)**+ p# A/ d+ I$ T  h0 ^' o
       - 递归终止的条件,防止无限循环+ X& N8 ~6 L9 P
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ( U1 Y' p/ V# j  j1 C# }# v. A
    0 |  p# C1 Q2 C2. **递归条件(Recursive Case)**
      i  ^& P& {2 [3 N" C/ ^  o   - 将原问题分解为更小的子问题
    # \% K6 @& b* e+ B) B4 a; h# J   - 例如:n! = n × (n-1)!! d( \" `( }0 \6 Z: a4 `- ]& g

      y5 O: A3 e' L9 d 经典示例:计算阶乘% j; T( w( G; O, j
    python  e3 v) O1 h$ B( {' ~- v
    def factorial(n):7 F2 j- N0 L: t! R
        if n == 0:        # 基线条件7 m6 S- l) @1 _) a
            return 10 A( }) r$ V3 d+ a3 @) V* c
        else:             # 递归条件" j, f8 t6 k$ G; u, ~2 V/ e* _; w
            return n * factorial(n-1)' e% _* o/ _$ a, l
    执行过程(以计算 3! 为例):
    ( W3 h+ K4 t9 n) _& i, h: b1 ~( ^, ffactorial(3)
    / k+ g9 P3 \0 L: B1 G3 * factorial(2)2 m* J2 p9 L' Y$ n' \' Q
    3 * (2 * factorial(1))/ b; @% C7 W  A7 t) p1 Z5 k5 Q! C
    3 * (2 * (1 * factorial(0)))$ r0 o- ~5 ^/ Q4 F: r( |3 W4 M# X( w) X
    3 * (2 * (1 * 1)) = 66 a4 v1 ^2 s! m8 J0 n* U2 y

    4 t7 w" h! E$ ^; N, @ 递归思维要点/ t( m3 U2 b1 d) O. J4 A  P
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ' ^. e% o8 l6 W8 Q2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! R( k0 }5 z- y: G; u% |. z. ~1 `
    3. **递推过程**:不断向下分解问题(递)
    ; m) [; j: }6 u8 V4. **回溯过程**:组合子问题结果返回(归)2 j( P/ b2 ~# W+ Y( L: [6 u; n8 g
    1 z7 V; w1 R  N
    注意事项
    3 f: Q! T; f: u* @( E1 k- ?5 ^0 k必须要有终止条件2 t- J  |/ f5 d, x& t7 q
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 k8 o  ~# R1 I* W" S/ O某些问题用递归更直观(如树遍历),但效率可能不如迭代( z! Z: E% y  |3 }) y
    尾递归优化可以提升效率(但Python不支持)- K+ W2 E2 G2 |6 \, ^

    6 ?- ?8 L" q$ W6 ~ 递归 vs 迭代
      \1 d* I9 w: S0 v( V; z|          | 递归                          | 迭代               |% }$ {: N* s5 l! d& Y7 ?  B/ v2 a
    |----------|-----------------------------|------------------|
    % B2 \0 c; S- s9 x9 N' D: X4 Q| 实现方式    | 函数自调用                        | 循环结构            |1 \- k. q3 k" z: P) r
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    6 x/ |# P, v$ r% s$ ~2 \7 I) b; i| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * b( I2 f$ W4 O* p: d" y( e. @0 d| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    , y# P6 \( [0 b! w7 O! s
    + @$ t! D) Y9 p% p" \$ | 经典递归应用场景, j! \  A( E0 u5 v8 U
    1. 文件系统遍历(目录树结构)" u% B  }: k) ~% @! f$ \4 D
    2. 快速排序/归并排序算法
    9 f: j) x% a+ x& i3. 汉诺塔问题& I- l9 w8 c* W! T
    4. 二叉树遍历(前序/中序/后序)
    ' ]. l1 t8 d  D% [2 u5. 生成所有可能的组合(回溯算法)1 q# v8 E) C, o$ W

    + W, {5 J/ M3 }8 b试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    14 小时前
  • 签到天数: 3281 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( I. O  `) P2 d3 p2 A我推理机的核心算法应该是二叉树遍历的变种。' K# E3 k4 |, D" K  _
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    # f. o" t; q7 N2 R0 g6 s1 eKey Idea of Recursion
    0 L2 B. V- I+ [+ v: A
      \) h2 r% F8 `. v! `7 hA recursive function solves a problem by:3 z% w+ Z% g: |7 ]

    ! U: ?& ^2 C/ ~    Breaking the problem into smaller instances of the same problem.+ b" E  S3 ~! C9 M. r4 k9 Y
    ) H3 M1 ?* t9 d8 O. P
        Solving the smallest instance directly (base case).
    / K, Z4 }) O; [$ r. V& f; {) \' ?1 G1 I0 `$ G
        Combining the results of smaller instances to solve the larger problem.5 f) V( k2 m& t3 `9 O. ^: n7 [

    " ^, [6 @* t) ~* l/ Y. n8 AComponents of a Recursive Function$ N6 X% A4 P% X; r, q- D& h( C

    ) a, y( j% u3 z* [; W$ F+ X    Base Case:
    9 r0 O. o7 Z; U/ a- {* X# C$ W
    * Y! j& H8 L: t" j& D! [        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ! I3 S' e0 _# I& t. A5 k) n
    ) S# I+ H* p% B2 D( s* Q        It acts as the stopping condition to prevent infinite recursion.
    5 F" v! t( O' @4 j" Y  A( Z/ F
    $ {0 e8 Q  n" l/ V# d- w5 M" g7 [        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 ^$ R9 ^/ J  ^. h
    & d8 ]: q/ ^8 [2 h+ Y0 ?2 w  i
        Recursive Case:1 i! n5 a$ a+ N5 P# l) A2 `
    9 h8 m+ O: v! M  q+ M
            This is where the function calls itself with a smaller or simpler version of the problem.
    + s3 y0 m2 m7 V* N4 [* S( e
    ' e( I% e) A& p        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# k  V5 h6 L5 J, K9 {$ @
    , A4 V( O: U! Y" s* h
    Example: Factorial Calculation
    * N6 P  c: |" v9 O3 W/ x# Y
    & I5 A# S+ ]' |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:
    7 F: n) I6 v" x) f% M4 E' F6 s1 K. z6 |3 @6 I+ @0 z4 g9 A8 F' [
        Base case: 0! = 1+ A! i/ U- n) O3 _8 d' r  x
    2 O/ x' K  H4 w+ \/ s3 Z, q
        Recursive case: n! = n * (n-1)!
    9 J7 \0 Q. z: B; |- d" b8 W$ J3 p) J/ j
    Here’s how it looks in code (Python):1 i, }9 B1 {1 T% e9 z% e4 v" f) r
    python6 K) A/ i% @% F) s7 h7 B

    ) C; k3 U( b7 y' ]  R
    - [9 `7 J1 Q2 v3 N  [def factorial(n):) _" h' ~2 E! Z: y9 }
        # Base case
    / z- k; N% h8 |1 z  ?) |  g    if n == 0:
    2 V* v" T+ S) I+ M        return 1- b" Y, p: I! J0 `$ Q, ]6 V  e
        # Recursive case
    8 E; ^* y6 x, S, t. s% ?    else:# j) O8 [3 ?7 U
            return n * factorial(n - 1)/ _5 g# T* M, j5 C. f
    ! g2 k3 I3 L, w# ~) N
    # Example usage
      a7 q" J6 k5 k, p4 S$ oprint(factorial(5))  # Output: 120% p+ r5 S4 G- P9 w

    + F4 z7 Y9 }- xHow Recursion Works
    , S4 m6 J! [: [5 X/ _: x
    9 D5 S% `3 ^- t7 W, f+ |4 r    The function keeps calling itself with smaller inputs until it reaches the base case.+ t# g* P1 Z+ v# `4 s2 m. J) t/ |

      ?* `* V; o" W* D3 _6 |    Once the base case is reached, the function starts returning values back up the call stack.5 Z' q2 w3 U( ]) e( Y& {

    ' E+ v' @: `. J. m8 d& m" y* j: ^    These returned values are combined to produce the final result.
    & |' Q. P9 Q8 A3 t" x( A! }) @& ~1 D) C2 G+ m  y
    For factorial(5):
    ) r' D  O( N$ a" W/ H0 c0 t0 O* j0 |$ \- Y% k  q, k- @3 n) N& e! n
    . E9 L/ j5 ?2 L
    factorial(5) = 5 * factorial(4)4 o0 T% c0 x/ \+ Y: C, h' z
    factorial(4) = 4 * factorial(3)
    + r/ f) ~; _; m0 X1 _factorial(3) = 3 * factorial(2)
    3 p& ~2 ^/ c( Hfactorial(2) = 2 * factorial(1)- p4 ^* D2 S. k- j# t9 z
    factorial(1) = 1 * factorial(0): t; p7 F9 q( \9 ~
    factorial(0) = 1  # Base case4 T6 M/ P+ l- y0 O6 W! z  v

    6 P2 f! N  x* F; \( NThen, the results are combined:
    0 t/ I; t7 V, Y* v8 \
    / [6 v' ?* Z. Q3 F- z: f) o4 d% z" o1 i9 z! h, D
    factorial(1) = 1 * 1 = 15 `! S+ w  e; E/ c' v
    factorial(2) = 2 * 1 = 2' Z  T. N9 M+ A+ h+ B; I; b5 m
    factorial(3) = 3 * 2 = 6, b+ A" W/ S9 `  d
    factorial(4) = 4 * 6 = 247 T2 \- R# d6 Q) M8 c  A8 @4 n
    factorial(5) = 5 * 24 = 120
    " o) [# j2 G7 T6 U, D: a4 F* l% M. D) \
    Advantages of Recursion' X5 Y2 n8 L1 W6 A7 u9 y+ o

    ( M6 M( i; n  O5 p' r    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).; R6 `) \4 B2 m* U& Q7 k( o( S
    ! _6 X) G& U8 ?- o: k
        Readability: Recursive code can be more readable and concise compared to iterative solutions.  _+ N, k1 d. V: e5 f9 X
    5 P, t, J$ u, ~+ h: l4 j1 z
    Disadvantages of Recursion; g- i( k& l( O" ]# k! h

    # f8 z" o5 Y' [9 |1 @    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.
      Q! _0 M8 S" R* G9 G6 ?7 r& m
    7 U; E) K' p, Q" h6 h* V: X    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' n$ n* C6 c7 r+ y! Y- ]$ ^* `8 @0 C2 p# \
    When to Use Recursion& P5 U9 v* ?5 B5 Z* N8 ]9 C
    + a8 @, F! j% g( i
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . e1 V4 J7 z* i' h: |
    & d+ L* o) v$ n5 ~/ E) ^0 |    Problems with a clear base case and recursive case.
    # m6 C+ T3 J9 y
    ; N% [! t9 e1 r# cExample: Fibonacci Sequence) q  {  q# T! W1 N2 ~0 D$ F

    1 n3 g3 n" s$ P; J# F( g& G/ H" oThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:$ P8 J- v3 I+ [. [3 q5 j1 v& G9 T" r

    + ]1 `* T) ~. U9 S    Base case: fib(0) = 0, fib(1) = 1: P5 A) D& K8 }6 o, M

    3 W9 t: w# z5 s    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ) O! ?9 f) N* g( I- y# F& z# U- S
    python+ m% }5 C8 {3 @6 ]
    * m* A% O5 s$ L& _( u
    : e3 t" \& w& C+ h+ Y) Q) h% ]) S
    def fibonacci(n):6 s5 I0 x0 `* j& r
        # Base cases( Q7 }1 e5 X  ^: r' @
        if n == 0:' |; q8 v6 h7 f! K& M9 d
            return 0
    0 ~& ^; U+ X: u    elif n == 1:8 P( Z+ d! Q7 |% n& r2 A) m' Q
            return 1
    4 |) ^; h3 w* D8 M    # Recursive case1 a# j* k# G( u% W" w+ |: A
        else:
    6 Q( g3 u8 L( o' P' V2 ^* d        return fibonacci(n - 1) + fibonacci(n - 2)% s* X8 ^; h8 M( B3 A: H; g0 ]9 V# Z

    ! V) n6 j9 \1 i* u' u. P4 _; ]( \# Example usage
    $ J( F! i! [0 Y- P$ g8 vprint(fibonacci(6))  # Output: 8
    6 h0 t& B9 ^" G: n7 q1 B" h$ o: f+ ?! u
    Tail Recursion/ ?( J. W. E1 \, o: Z* \9 B/ @

    % f& c& `- W" R$ yTail 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).
    # |7 X2 w1 u0 y, v5 |8 \5 X0 e, Z  d/ ^5 n+ F
    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-6-27 23:47 , Processed in 0.075786 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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