设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / G9 T. ^6 m' `: X) H
    1 @2 W% k3 x, N/ t1 v解释的不错
    * \$ r9 E- x; T) j$ p$ l- L- W0 Z( d
    7 @) x( q- Z; e( v递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    2 _/ i; u% `' i0 n
    5 F% O0 `' Z2 N" K2 A# y" {8 k8 w8 c 关键要素
    3 x, v% H  }5 T  P1. **基线条件(Base Case)**
    ; K: X$ x8 w9 e$ X   - 递归终止的条件,防止无限循环6 `0 U/ J# f* a- [% j
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 k7 Z$ }* H/ y$ O2 k) _
    / I4 p0 n7 e2 y9 ?# s
    2. **递归条件(Recursive Case)**
    2 k4 B# K" [9 w: p   - 将原问题分解为更小的子问题
    7 Y. J) Y/ F( i2 g2 \) R   - 例如:n! = n × (n-1)!
    / S% C# B; {. a! ~' C, Z/ e
    3 s& l, L0 D& S( ^ 经典示例:计算阶乘) D( Z8 _% ~, T
    python
    2 ~9 x# f+ M$ T9 u$ y2 kdef factorial(n):" b9 }1 D& b+ ]! @. P6 }" M
        if n == 0:        # 基线条件
    9 Q" S- {% }: w; J! s        return 1; ^- `/ p) I  Y% P
        else:             # 递归条件
    * @" u2 k+ \9 K, I: B( m. W" a0 m        return n * factorial(n-1)
    $ n8 O: m8 H+ ]+ M执行过程(以计算 3! 为例):
    $ A+ J" \2 q& O7 ]- o* h* Mfactorial(3)9 |8 R( ~; B' p1 q. z
    3 * factorial(2)
    " X' h. h' F" g3 * (2 * factorial(1))( K2 ?" w  j7 \! k2 x. {# h
    3 * (2 * (1 * factorial(0)))
    ! `* v4 P" O$ @- E7 S3 * (2 * (1 * 1)) = 6
    : ?5 m+ Y) I2 F2 M% y, I, e
    ' F1 a  X* ]' S5 X3 m 递归思维要点
    . }' c1 V- \3 v" u( C6 X% J1 O1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 E. M) x* Z$ t# E# a5 g
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)( U* m! ~0 w% H9 D/ ?5 ?: D- l' C
    3. **递推过程**:不断向下分解问题(递): j9 i$ J4 n" f  ?, J0 ^
    4. **回溯过程**:组合子问题结果返回(归)2 r( E9 B% M& V

    9 k7 P3 q2 j. P% n+ C 注意事项- h/ q9 B4 \6 C- b8 x& x" b
    必须要有终止条件
    8 T3 y' ^! o8 R; K递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    $ p6 `* V& b7 B9 ~: ]4 d  \1 [某些问题用递归更直观(如树遍历),但效率可能不如迭代! @! @% U7 C8 r* ?" h2 e: a
    尾递归优化可以提升效率(但Python不支持)  |7 A% n% U2 g7 i

    6 s" F/ {7 ?: d5 r# h; N 递归 vs 迭代
    ! a) s1 u; h/ w8 a% V|          | 递归                          | 迭代               |
    % o2 m* q6 }; p! ]|----------|-----------------------------|------------------|
    , K! T- u3 X/ s  U+ r& H- e0 T3 d| 实现方式    | 函数自调用                        | 循环结构            |
    / A, |! r4 H8 ^  {, S/ b1 v( m| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 G3 C2 m/ {+ O' m| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    + m7 v. T/ Q, S4 o) O* n| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 S+ b  t% {( H+ d' Q  j

    % B- ~, F- v% a" K. E/ R& W5 K) j 经典递归应用场景( U- T% u. p* S0 e" X( H3 x
    1. 文件系统遍历(目录树结构)
    : f) B# k, r. z  p2 @2 S$ F2. 快速排序/归并排序算法' ?# f1 W# K, \7 d
    3. 汉诺塔问题
    ' q! ?9 r6 Z6 M4 \4. 二叉树遍历(前序/中序/后序)4 O" i# G" o" O4 _, l$ p
    5. 生成所有可能的组合(回溯算法)
    0 O. a7 V6 v4 `
    * \2 N: ~: H# ]9 q$ A7 d试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 b4 C) q: F! f7 Q
    我推理机的核心算法应该是二叉树遍历的变种。  X: B7 S" i# [/ ?7 ]
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    6 ~: L% Z& Y$ g( F% m& YKey Idea of Recursion
    ' z; y* U9 @6 c9 p. q7 w; e
    ( a3 [% i; y" W" I; I2 KA recursive function solves a problem by:7 Y4 a* l/ |* Q3 n% x/ E: `/ x: m

    " a* h$ u) a/ u    Breaking the problem into smaller instances of the same problem.
    ) o- e8 u' r$ t2 D1 A; t0 H3 k! r
    ) {: E" V6 J$ T4 J' z. p    Solving the smallest instance directly (base case).' k' i+ m- M9 l/ G$ Z' z
    + w+ w+ a. r# Y6 i' }
        Combining the results of smaller instances to solve the larger problem.7 V5 i* |: H: Q
    8 z6 `+ t4 S8 L4 c3 u; X
    Components of a Recursive Function9 ^6 n# ?1 H: P- |
    & n' K3 Y7 |" O* k
        Base Case:
    - o% Y4 \- P! B5 F/ y" W. F
    ; P1 {, R" u: C5 I. a        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.+ d4 q, l) C4 z! G! \$ c9 p1 O

    5 ?  u2 U" w+ S) q        It acts as the stopping condition to prevent infinite recursion.
    ! Y" }0 t4 n& k- W, j. i1 Y& H1 ?. N
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1., X6 A! k$ O  [% N1 z: D$ J
    # ~7 d* s1 E2 Y2 e
        Recursive Case:
    7 g4 ^  ?- V" R4 N" f# p6 n
    & M, Z5 @' m9 W0 }8 H% t        This is where the function calls itself with a smaller or simpler version of the problem.
    + D. i4 ?0 }9 w' k2 M# ^: d( D% A3 S/ X& s7 ^4 N, ~9 k! J
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    * x* @( v7 s" R/ l8 S2 ]% e# r/ x
    $ W$ o7 j- a5 M' y2 d$ IExample: Factorial Calculation
    5 J+ Q  Z0 s$ R* @9 M; U  F1 }# ^4 ?' f+ V' r, u* [2 ~" L
    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:
    3 G% d8 E. {- R5 B. }0 a! E9 B3 |9 s( z. j6 p! Q
        Base case: 0! = 1
    # ^% l8 r1 P( r' r- |* M$ q* Z. Q
    ' e9 E0 t/ e) K1 S    Recursive case: n! = n * (n-1)!& _, ]9 ?, y4 m5 y. B- }
    9 m) q2 [. j0 \5 q* l
    Here’s how it looks in code (Python):
    4 N1 V  P# D$ Ipython
    2 V* q$ O& o& N3 ?# X, [$ z/ [9 G9 U
    ! H! x' ^6 T2 q' O. Y* E6 \5 D0 p0 b
    def factorial(n):
    9 A' d, v; h+ [# |    # Base case  B5 y9 @/ n  W- t$ J0 T7 k
        if n == 0:4 s- {5 S! M# C
            return 1) y( X/ Y9 Q' N7 m# ?) S) G
        # Recursive case
    8 ?  O1 O/ W8 G; A4 ~( ]( c' A    else:! W; k7 w4 i9 a
            return n * factorial(n - 1)7 J9 e2 b, Z2 R4 y9 n
    8 h) @: z! V6 y
    # Example usage, w/ p% Z8 ^$ i+ G
    print(factorial(5))  # Output: 120
    0 a# v6 \; r6 x" ^
    ( |% f' @" v; ~. |8 q! B( U) K: yHow Recursion Works7 g  T* ~. [' y% ?" E, x& w
    / _% n  Q! Y' l5 `; q: y
        The function keeps calling itself with smaller inputs until it reaches the base case.! `1 o! M  M% g, I
    1 \7 q$ `! O8 T9 F; G
        Once the base case is reached, the function starts returning values back up the call stack.
    " m& i# H; T8 k: X- _/ ]7 @
    * d& d7 |  a% ?( K# A    These returned values are combined to produce the final result.: o& P3 K4 P9 [; p" j6 A/ Z5 ^4 V

    * L# q, L! @1 }  P! ]* z% }For factorial(5):
    ) \0 D2 Y- h" B9 B/ }1 X4 b/ Z
    % `6 @& C5 j3 Q4 T
    , z( T  V' l: p6 s. ?2 tfactorial(5) = 5 * factorial(4)
    ( Y; A! Y/ n4 e5 p% Xfactorial(4) = 4 * factorial(3)
    + S& i$ A, c5 h2 P7 L1 [. {factorial(3) = 3 * factorial(2)
    0 V; P2 k1 n% Gfactorial(2) = 2 * factorial(1)
    & n6 K4 `. c6 B' Z. Mfactorial(1) = 1 * factorial(0)
    & l9 [' `* ]3 b3 G2 m! dfactorial(0) = 1  # Base case
    9 r+ m2 p1 }2 E" }7 P3 v( U$ `3 Y3 \& G0 r, T
    Then, the results are combined:
    8 @1 I8 f6 C; y+ ~( F& h! L: ]: q- f- X4 Z
    ( B2 T0 L4 x/ v8 M& N* K
    factorial(1) = 1 * 1 = 1) N& j1 n8 t& q: L7 R3 M
    factorial(2) = 2 * 1 = 23 ?3 W8 ]* o8 `5 l
    factorial(3) = 3 * 2 = 6
    ( J) F/ c( O7 f6 o& k. Tfactorial(4) = 4 * 6 = 241 w3 e: f; Y0 U: E6 s& U2 M" R
    factorial(5) = 5 * 24 = 120
    0 q# k% z' b) L* L1 \0 K/ F+ q. k) S8 T
    Advantages of Recursion, ]3 W6 U' j0 Z& v, U4 x3 Q% A4 B  K* R

    5 r, q) o- D$ f7 N9 a0 ~5 x    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, V" M1 v* Q3 {/ q# Q" _

    . ^5 T1 }7 M7 d0 I6 B' U! l& {) Z5 Q9 w    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; M+ J1 g9 D' |) ^9 w; G1 v- b* B. A' g% j1 G
    Disadvantages of Recursion+ V" ?% O! `* [5 @0 R- J
    - b! t4 ?, H# |! k9 @( c3 p, v+ I; Q
        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.
    + h; e# D6 [) I" O) m! h% L8 {( v* c- T0 q. L
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).( }1 u; C' @8 @' o, ^* B
    ( d! {" W- H( @, }! Y& L. W8 G, Y
    When to Use Recursion
    & K5 }8 B) P) c! ]1 o5 d! P! u4 ]$ h# N3 o# d( V7 r
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' p# a, E& o. I! M5 I. C/ C
    $ b7 P' p  C% F* ]: \+ P/ @
        Problems with a clear base case and recursive case.0 H/ Q( B# n7 D/ b* A5 L; x$ R
    2 j& E( }- A1 v, L& r
    Example: Fibonacci Sequence% V! u$ M, X. {7 J
    : n! W  e1 K3 W. b
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:  v5 \5 h* H( K+ X: m$ G: F

      c& ?6 f+ ?- k6 k4 k% v( R    Base case: fib(0) = 0, fib(1) = 1+ W" u0 a& v9 Y9 {$ o4 a1 i' D$ s' X

    - u5 [) ]" c6 f( e$ b+ M; @    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    / b9 x  U* J! \0 Z' I5 a, C3 a8 H1 p. i' d
    python; V# E3 S4 R/ Y5 o2 |4 P# d
    " p2 ]: _$ O9 @" I+ f) x  c5 h
    ; G- z8 m+ l: H+ |; }; }$ H
    def fibonacci(n):
    + Y7 x5 t; d1 W+ x    # Base cases
    . z" E( S( p/ E4 U; l    if n == 0:
      Z! D+ \3 E4 g$ l6 r% N) x7 L' W        return 0
    . u3 z- W4 L. d0 y) Y    elif n == 1:
    9 X9 m9 u" E, P1 @1 x- K$ {        return 1
    ( a. U/ o' g5 V+ l0 Q+ O5 m5 ~1 l    # Recursive case- w: U+ O! ^# Y) g
        else:3 a7 V- V8 C) l6 I# Y( |
            return fibonacci(n - 1) + fibonacci(n - 2)
    - I# `- j3 f$ \. {8 h- a
    $ n: Z! r' z# X0 d" m" Z/ e# Example usage, e% @, O9 F+ E) |
    print(fibonacci(6))  # Output: 8# v$ Q' @/ I0 z; D! O- Q) t$ S+ \
    3 k) w- L) G  t
    Tail Recursion
    * O2 |8 F$ R! ]& W' C
    ; |; x, c1 j8 O; D+ ~7 gTail 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).
    0 T0 d  {  a4 c5 u5 x0 E+ T
    : @# T$ i$ k2 eIn 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-5-22 15:54 , Processed in 0.080343 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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