设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    % S/ }3 D2 x4 x- ^! M# d
    : B7 K+ a1 o, W. K9 `3 f解释的不错  @. }3 r+ _, V4 P5 _, f

    0 G8 J+ S% ~+ f6 }* ?递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。+ ^* ?$ ]2 p0 S9 m
    # Z. V% _. q0 A& Z
    关键要素/ G/ M/ f- z4 f: i/ ^; L" A; H9 G
    1. **基线条件(Base Case)**
    : t: Q, m# E6 k: j; |   - 递归终止的条件,防止无限循环" e" Q+ l* ^6 `( v6 z# P3 @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    - U9 C8 R5 L& a- S) B9 s2 }% W" m. h/ B  [4 `
    2. **递归条件(Recursive Case)**5 Q! |8 F$ Z1 A( S1 S
       - 将原问题分解为更小的子问题/ j8 s! n  u% x" _6 y
       - 例如:n! = n × (n-1)!1 g; n9 y) t; A5 `5 E- B# Z
    7 G3 h' O3 D3 V8 Y
    经典示例:计算阶乘. O$ ^' F1 E9 B1 s4 }
    python7 l- [4 r5 Y+ p
    def factorial(n):/ a3 k% L8 X  e& x
        if n == 0:        # 基线条件
    4 l/ R2 D6 V* p+ n0 R        return 1* i$ Z! j- d( H4 T; j
        else:             # 递归条件
    & U" Q' e- M) G( n# q3 z; l: N) }        return n * factorial(n-1)
    " n; Y& E* K+ B3 X7 S执行过程(以计算 3! 为例):
    % K0 C( a' c5 U4 a9 {factorial(3)
    + p2 G& @. e2 v6 G6 ^" `# d3 * factorial(2)
    7 e6 K6 i/ @" X  K3 * (2 * factorial(1))" U7 K/ B1 d& U8 t0 h1 j+ h6 P  @
    3 * (2 * (1 * factorial(0)))
    ) `6 A3 Z  J: j. _. Z3 * (2 * (1 * 1)) = 6  w; X; E5 m  \9 P. }

    . v9 P% Y/ D- p, L# W 递归思维要点
    9 w6 y$ t2 t$ e& Q1. **信任递归**:假设子问题已经解决,专注当前层逻辑! s9 G1 Z3 m1 U: P+ h8 D" v' w. G
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    8 j" D  J" Y4 F5 r$ C2 z9 [2 f3. **递推过程**:不断向下分解问题(递)
    $ T8 g8 ~3 ]$ f5 X* T# k$ J  {4. **回溯过程**:组合子问题结果返回(归)
    : ^+ e) w3 H$ j
    / C8 a/ ]) |0 d# f 注意事项% P! N; M% q# @, X& {& |
    必须要有终止条件% d! g( d: \2 I2 T
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' w" L' S' u) X# w7 C4 U2 O某些问题用递归更直观(如树遍历),但效率可能不如迭代
    % L2 y3 [7 V9 F- Z尾递归优化可以提升效率(但Python不支持)% k3 {1 B' c- _8 m% z) e

    , Q' C6 W7 C: _. z5 A: b0 m* f 递归 vs 迭代
    ) q. d+ K5 g9 G6 F2 j|          | 递归                          | 迭代               |
    , {9 Y6 z7 u& f4 s; }|----------|-----------------------------|------------------|
    5 a" W# K  h2 G& m3 y& j| 实现方式    | 函数自调用                        | 循环结构            |
    ( H- G$ O7 F/ `1 a# y  E| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ( [% d1 _" A% Z. W4 a* M| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! r' O- K# T4 x% {3 J| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |6 N  M1 I2 i  Z/ }# P

    9 h- @3 N; l2 j( ~ 经典递归应用场景  Y$ `9 C. `& S7 }' O
    1. 文件系统遍历(目录树结构)
    # ^& x% n$ |+ f: s6 |6 g, `2. 快速排序/归并排序算法
    : \  z# ~6 [) }- w% K3. 汉诺塔问题
    ) }. r4 P. p2 i+ P4 {- @4. 二叉树遍历(前序/中序/后序)- i- O1 O. V- w% T
    5. 生成所有可能的组合(回溯算法)) b. i/ j$ _* y8 s' b
    # w# U/ @" Y4 Y* P" |8 ?7 e
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    3 小时前
  • 签到天数: 3151 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    . j6 n0 G( A' S7 ~  y, I我推理机的核心算法应该是二叉树遍历的变种。
    ( b: S, ?" b# }9 T; _! B另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:- @' P* J. P9 @' v' `$ W6 m, ~
    Key Idea of Recursion
    3 X4 v2 W! m( \$ d) c( P+ ]( }) G# k- w& }  ~0 J2 a
    A recursive function solves a problem by:4 \" S9 N$ E% u9 D; i7 ^2 Y8 T

    ( g6 t2 }! w  L* u    Breaking the problem into smaller instances of the same problem.
    $ i+ p, ^) y  z% o* T) o( \8 E& `8 S" R/ W6 E2 u
        Solving the smallest instance directly (base case).
    ' N& o6 Q5 Q" f3 v
    ( E! S, j2 ]7 l    Combining the results of smaller instances to solve the larger problem.- t0 |; }. n+ ]. U: q. z

    - ^9 ]$ p* r1 Z( n0 XComponents of a Recursive Function
    8 h' S& U3 T# c+ n0 K
    5 J2 l5 M0 z- U8 }8 _1 Z    Base Case:% g- I5 ^* m. v

    # e# h) U/ [9 c% T  d        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    $ E5 s# |& d/ L# ~# q+ _" h6 I( k5 y
            It acts as the stopping condition to prevent infinite recursion.1 M" H3 t6 m$ A/ R
    2 g+ H/ Q: z, y6 X2 I
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  e9 l/ P4 X6 i) c

    * ~/ [% k) d) |  W. Q  ^    Recursive Case:$ W$ v6 Y5 P. K! p: G; P- T
    ' Y3 Y8 [' |; ]. d
            This is where the function calls itself with a smaller or simpler version of the problem.2 J: R; e' O/ h

    # z) W  D8 [6 F% {6 n- W' ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& s0 T2 _6 f- k0 F; O4 ^

    ) Y# x' u* l5 o# JExample: Factorial Calculation
    ' o; g0 O6 x9 y8 d0 r" t6 D5 F2 \4 D& r8 A1 N/ ?) |4 x
    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:8 v- y* c% Q7 {0 i% k( s* |, Z
      w) G, g# M/ G& N/ L9 P
        Base case: 0! = 1% }% ~0 i5 }8 i" i  N' E* L

    7 m$ k* E% n( u9 ?" D8 s    Recursive case: n! = n * (n-1)!
    0 F/ O3 a7 r& z2 B* U# I; b  M/ X% W, N
    Here’s how it looks in code (Python):
    + P- m; B: O% C3 s( k8 w+ ypython1 i" e7 \+ |: j

    , n+ y2 U$ r" G" k* z, F6 N% s  z; R3 V0 P' z* ?; j% e
    def factorial(n):( ?5 d( W. T- f$ X3 R' n
        # Base case$ N0 L" m) Z' [$ K0 b9 ]
        if n == 0:  u, U1 L% I0 e# v3 a2 v5 O
            return 1
      u& e* ?7 g5 W    # Recursive case
    ) \4 D8 _+ a, P4 Q    else:
    ' E- u$ _5 x! Y& |9 P3 }        return n * factorial(n - 1)
    ) ?. P. Y/ ?+ h! a0 t5 j# b* t  g  m9 L
    # Example usage9 I) M7 D# f) Y7 U2 T
    print(factorial(5))  # Output: 1204 R$ e" e% i$ A- j; t
    , Y+ q, \) t  g2 q; C; i
    How Recursion Works
    " [) g9 }$ J+ [" N$ O! S: b; o9 N9 U$ j; i4 a  E9 o) M
        The function keeps calling itself with smaller inputs until it reaches the base case.! o2 N' x3 C5 W2 ^4 c% F6 Q! \8 M
    8 R" a! E! t* N: c% v. |
        Once the base case is reached, the function starts returning values back up the call stack.+ }. ~9 y# M' ?" N) d  @. H

    0 g- h8 M. v- V    These returned values are combined to produce the final result.
    , X4 F) N8 V& `# z8 B+ q$ n6 ~. Q% u3 Z8 ]1 C6 ~  J" F8 c# p
    For factorial(5):) I0 n/ ?3 ?, U( r3 R- S

    + }( E- G6 o. y) l. W8 K5 o, `+ J! Z2 S) C
    factorial(5) = 5 * factorial(4)$ i8 |, K* q3 V3 ]
    factorial(4) = 4 * factorial(3)
    . J+ B8 {# `7 I2 @; @9 E; \factorial(3) = 3 * factorial(2)
    5 `; |5 F, K+ @" Z8 }, `factorial(2) = 2 * factorial(1)( v' |2 x' s" x* R0 B7 E0 ^) `
    factorial(1) = 1 * factorial(0)
    ! N- }& Z+ q2 G/ [) [  [& |. @; kfactorial(0) = 1  # Base case
    ) F) G$ |5 p6 q( x  Z
    * P9 i' h# X+ Z5 b' m* J& \0 @Then, the results are combined:/ C; y& W: T# h/ j9 J  D

    ; ]# s( C+ g8 }* v9 `% a/ K
    / b6 ?9 K# z8 `. Hfactorial(1) = 1 * 1 = 10 w8 y- V$ C8 G  T" Q: t  T1 G
    factorial(2) = 2 * 1 = 2
    / z8 @/ T# z0 V6 B4 x) J2 hfactorial(3) = 3 * 2 = 6  O& r0 W# [5 y7 M
    factorial(4) = 4 * 6 = 24
    9 e8 O% R+ v7 w  F+ X( M- Gfactorial(5) = 5 * 24 = 120/ w& L. C. Y6 M# f  f
    . a, ?5 k0 C' a8 y2 ~
    Advantages of Recursion
    ! t' o: p, S2 ~( f* U+ z( Q3 Z4 m$ A1 j6 \$ `
        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).
    & i: C) O# x: Z' W# H0 j  X
    # k; o( C4 d/ O% X    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , T( J9 W8 x- @& ]: [# g7 u% X/ ^* j7 v* N$ }
    Disadvantages of Recursion
    ) S# b. t; d% j, n* i0 i
    - q* |; D% T5 ?% C3 A: l) y, N    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.
    0 K$ P! L; s6 U1 _% B! P1 T: B8 c" }$ n' U$ Z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 \8 ]6 `) e( Q1 w0 S

    1 U: `: n! D9 n' u2 cWhen to Use Recursion
    & ^5 u, b" t. [' Z1 u2 T. A! I
    ! y; F8 Z3 v3 U7 `; F7 x: i, Q    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 ~) L8 l5 e. r/ Y! Q8 G; q7 X5 I

    & F% `  Y" N# Z( O% Y    Problems with a clear base case and recursive case.
    7 H# k7 H; c" Z9 V
    6 r/ J1 I  m( o- n* b- VExample: Fibonacci Sequence
    5 n0 L( G0 A  k: w7 |5 A
    8 U+ Y  o) m. n+ pThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ K( U+ Q& g1 e' |; D7 s
    * m  X/ p& C7 t7 y) l* {    Base case: fib(0) = 0, fib(1) = 1" c( n$ l+ t) q% B1 W

    - {. K% X! [+ u9 F4 c& W. |    Recursive case: fib(n) = fib(n-1) + fib(n-2). Y2 j6 d0 D! n; _5 d& o# P  [
    / f1 n# l- w( y6 J
    python3 V: Y$ }- F, z! \

    ' c: u+ V! a  W9 `& i2 Y/ U1 K" p$ E6 U4 E" t. Z
    def fibonacci(n):4 w1 X& S5 h1 B! Z. Z# z' V- e. c
        # Base cases
    8 B" P8 q  d2 G. k    if n == 0:: g: w* ?2 m+ l/ H: R1 m; z& }
            return 0
    - I( z6 X8 P7 J2 s' ~- m, K; t1 X    elif n == 1:# {! ]- F6 d+ [% }, k
            return 15 d' X' K# K0 D/ H3 T7 {
        # Recursive case
    " U. Q5 w4 c& |) H" c; ~    else:
    3 W& C8 }$ v+ {        return fibonacci(n - 1) + fibonacci(n - 2)# P) l: T( P4 k$ R9 i

    " G; h/ k4 O# [# Example usage
    , O, O9 [8 E+ O' Mprint(fibonacci(6))  # Output: 84 M/ E+ H: W3 P3 l: _3 b8 a
      D" a9 Z  g$ m: W) ~' @0 \) h. Y
    Tail Recursion& G7 M. U" Q+ m+ R' e( k
    1 a; \2 l( d/ h- ]# P- F/ ^" [
    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).
    6 v) Z  L: Q0 v# `* G# \# N* N/ D1 ^- u, }. q
    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-1-22 13:44 , Processed in 0.065173 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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