设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + u# }( r# x8 @/ }
    & h0 O, K) o8 F
    解释的不错
    : {; y( H) V. g% ]9 F
    6 J1 g: I- F5 r+ \! ~# j2 r  }4 [递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " n; h- d$ N9 X) n3 F) Y
    % a  F6 E8 n. k0 V- x 关键要素7 G, {0 Z1 M6 e3 l2 d" @! d1 R
    1. **基线条件(Base Case)**. ~& W/ A% s# e5 z& n
       - 递归终止的条件,防止无限循环
    ( W1 ?& f8 @* G/ w* k5 Z. I: H! H. R   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " k1 L8 c. \9 l/ T0 l' }3 z( i* [5 F1 X7 f5 _- W
    2. **递归条件(Recursive Case)**
      p4 S5 D6 ~4 S$ p# c2 a   - 将原问题分解为更小的子问题
    1 ]( x2 S" w3 l   - 例如:n! = n × (n-1)!3 f8 _* [1 k5 l" t4 d
    8 c- z- X$ I7 M8 y+ L# T+ n
    经典示例:计算阶乘
    9 {; J- A/ ?/ N+ _! f; ^1 A  E  {python7 {) W5 m8 r. r1 E0 Y) l
    def factorial(n):
    : X1 y* d: O  C' b! }2 H    if n == 0:        # 基线条件
    ' T* E  E$ e3 R& _6 D! w2 S        return 1
    & J* E% _% e+ d- ^) l  Z    else:             # 递归条件
    1 p( g& B. x1 e4 k1 y        return n * factorial(n-1)* [- Y' z1 h& h  k( J. T
    执行过程(以计算 3! 为例):
    % U* _- _/ x/ `factorial(3)
    % Q$ g5 L  G4 r5 a! C3 * factorial(2)
    ; n# R) W2 I4 Z) y& m, x7 J' E3 * (2 * factorial(1))
    ) t8 X( G5 \% a9 W5 A& E$ v# ^' C3 * (2 * (1 * factorial(0)))
    3 H2 \3 A5 H" U( U4 S2 R1 J) W3 * (2 * (1 * 1)) = 6/ ]3 ^: o+ K/ z5 @! B( v0 M$ D6 M! u

    # R6 r7 @. n- U9 P; S. r/ R 递归思维要点1 z  w; m4 w$ ]% H! r: }  M9 Q
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
      u! s4 a% _* R2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ n2 n2 I/ G  E6 m! Y% ~# ?3. **递推过程**:不断向下分解问题(递)6 g& u; p& q7 w" n3 B2 \- z
    4. **回溯过程**:组合子问题结果返回(归). `3 g7 E- D0 q3 @6 o7 c- o
    8 f% ~+ O  x8 e' t
    注意事项
    % H8 O7 o- ^# L5 n必须要有终止条件
    $ Z4 N1 @7 h8 i递归深度过大可能导致栈溢出(Python默认递归深度约1000层)# O: ]) [0 z" G6 |* m" p, r
    某些问题用递归更直观(如树遍历),但效率可能不如迭代& T2 P9 t8 v0 @. k
    尾递归优化可以提升效率(但Python不支持)
    ) L7 F- ?: q* i7 J; D. i% p
    ( ^8 x& n, o& B) @5 G, Q6 X 递归 vs 迭代
    # C& n9 m6 l# G  J! O|          | 递归                          | 迭代               |: n: ~' u, V( [6 ?, z
    |----------|-----------------------------|------------------|' N! @0 ?# p8 B! ~, e
    | 实现方式    | 函数自调用                        | 循环结构            |( N4 Z/ E5 y+ J4 x0 l
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    9 `- _0 n2 R* |; f" q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 W4 O; E1 ]' E3 n
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
      _5 h& F1 N2 S
    " S( g. k2 |! S% B) |* A; h 经典递归应用场景
    , J" k0 G) X2 X* K% _/ q  x1. 文件系统遍历(目录树结构)$ C" K0 j6 I7 }  d
    2. 快速排序/归并排序算法/ ~/ B1 F0 N2 E+ a/ D! m8 s+ q* y
    3. 汉诺塔问题4 u6 d  Q5 [+ @" _# B' H
    4. 二叉树遍历(前序/中序/后序)" `. X- I/ ?$ r1 K, [# s+ |; ]
    5. 生成所有可能的组合(回溯算法)$ }% |: R8 [9 U
    $ j- f1 n! V3 U9 a9 J+ t
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    14 小时前
  • 签到天数: 3202 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : K4 ^! N: i9 T% v我推理机的核心算法应该是二叉树遍历的变种。7 d9 t% c( `! O# B# x
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, r/ M6 n# y( F! |( a; X
    Key Idea of Recursion
    , f; J7 Y% {5 S3 B! G9 ^
    6 j  m) r6 i0 ~( Q! B0 r+ ~A recursive function solves a problem by:
    & n) Q5 j: u: |/ J, P1 T' p7 Y4 H( T" O5 d; U( h( l5 ^. e
        Breaking the problem into smaller instances of the same problem.) g9 o; a  G- A6 [2 u7 a
    $ K! _  O5 |6 Q6 c9 t
        Solving the smallest instance directly (base case).& v- A' x0 J; D
    1 H- k# ]" @( S' c( S2 k9 B
        Combining the results of smaller instances to solve the larger problem.! H) H* y0 |" _" Y7 ]( S  v

    . k2 C' v2 m: R- CComponents of a Recursive Function
    % A% ?% V& V, ~( ]' P  J# I) y; V( r' T( N4 K
        Base Case:
    2 b9 n; e1 ^! D( }  E
    : Z% D4 B7 I& N! _; }9 V        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    - s; @9 l+ X3 {7 N$ M. j9 _# L9 F! g6 Y- k: W) s& x. r
            It acts as the stopping condition to prevent infinite recursion.3 ~7 }2 I/ ~. b7 Q

    4 ~0 |/ V1 K: S. l( h        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# E1 F9 z1 Z/ A$ U8 E3 a2 R

    & i3 W$ l& `5 [6 K. s    Recursive Case:
    5 d- ~' ~) c; I, P- C4 l: a9 ?/ c3 @! P: S! C6 ?
            This is where the function calls itself with a smaller or simpler version of the problem.
      m' ]/ w! x! @9 ^3 h) S( i' |2 I- |' C1 F0 K7 L& M
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ y' p- C5 E- `

    ; G  @9 o2 ^! VExample: Factorial Calculation
    8 t- r) ]& ~4 v6 X  n" J; v, s5 X$ Q. w6 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:
    2 A2 \/ {+ S. ~; s0 z8 O$ k9 _0 F( k3 H2 a
        Base case: 0! = 1
    ) V- |6 ^7 H. P- k5 c' X
    : q/ x6 N# V, g; D6 f% b2 o    Recursive case: n! = n * (n-1)!& U" O. R) @. _. e& d; p2 l% Q& S
    . M$ P4 e3 @6 j, ^$ a
    Here’s how it looks in code (Python):$ L% S. \7 R) X9 E; N7 {
    python
    9 G9 Y- O' N; l: k- Q
    5 G+ u- I, _1 z5 ^
    ! A! p* S( ^% l7 qdef factorial(n):- w1 R/ |% k; N6 G
        # Base case) Z0 ^4 h; B  e* N
        if n == 0:
    9 N8 u8 F3 `+ l; F' L        return 1+ H$ [0 ~! V6 [* x  t/ q- L3 `
        # Recursive case8 R$ P5 L* w- m! Q
        else:
    , a. x  x  F3 i9 O6 j# L& H        return n * factorial(n - 1)6 t1 d6 ^) ?6 K: Q9 B+ y5 ]
    . E$ F3 K; J, e- i% s, w
    # Example usage1 L( e1 M: l+ I. J7 [
    print(factorial(5))  # Output: 1202 q* o! @  S4 z. b
    $ O2 ]8 c4 q- w+ ?/ d' a
    How Recursion Works& k: C0 _& G" g5 E, f

    6 ^9 b" @* Y' z- I: i" H* L+ x    The function keeps calling itself with smaller inputs until it reaches the base case.
    2 G5 J; H, k% |  t: j$ |* d
    8 o. R; v2 h4 b: [    Once the base case is reached, the function starts returning values back up the call stack.
    ; _4 i# c$ c' h% r5 U9 Y
    3 U+ b3 }3 o/ Z1 ]& Q3 F( c    These returned values are combined to produce the final result.2 v  }0 c( O/ n$ h& |

    * R2 Y# |4 z( c- V! wFor factorial(5):
    % n5 d7 S$ h. h( e6 b+ o& p( a% ^2 i4 ]
    & b4 o: k6 C$ e. C
    factorial(5) = 5 * factorial(4), o/ c4 c& O" d
    factorial(4) = 4 * factorial(3)
    3 Z0 m, j- i; A2 Rfactorial(3) = 3 * factorial(2)
    0 Y; H( q% Y2 T% S& v( _factorial(2) = 2 * factorial(1)" U+ t" y3 p* }" y+ C6 e
    factorial(1) = 1 * factorial(0)# M7 ~/ l% y  j: A
    factorial(0) = 1  # Base case! z0 [- v2 [% {+ n2 a' f
    . ^7 Q. Q& }( M. W
    Then, the results are combined:
    5 @; a) B+ c/ P1 D* e/ n
    % [1 b7 I4 t& x
    ! \, Z5 O7 _/ u3 [7 t) y+ P' wfactorial(1) = 1 * 1 = 10 V  I6 ]* e& c3 `; k3 f/ b7 x: ~
    factorial(2) = 2 * 1 = 2
    ( S& w0 e& ^+ S7 A1 @9 k+ }factorial(3) = 3 * 2 = 65 C$ T4 ]' q1 V5 O# [2 J
    factorial(4) = 4 * 6 = 24: C5 y& P* r/ z/ _3 `/ D/ [
    factorial(5) = 5 * 24 = 120. c: {' @: D0 m! l! M3 ]
    % R+ V( Z/ V8 \% A
    Advantages of Recursion
    7 Z$ v! u5 @: l$ N; C5 G; k) i9 ^+ o6 _
    3 E. _' m8 n' 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).3 i% o3 H( K7 H# k* L7 Z" B
    . L% L& K, _* v; ^* {; D0 A
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ F$ x  I  a+ R8 X) D: ?

    , M& |& s% E$ S2 b* F2 YDisadvantages of Recursion$ @# n" [: R  f. m

    + A4 D" r; A" J% c8 [    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.. V. q! _4 E$ [
    # u: Y& O( @: U
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).% D7 J; F& |* }6 T

    4 g5 T" \8 u, }' M: g8 VWhen to Use Recursion
    5 c9 m2 {; s* J/ x. z- O9 K0 A1 b0 m) ^+ Q  I
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % I; Z* u8 x* Z  N7 x! @' d5 [5 }) f" `3 ^. R0 U7 P+ v% W7 ~
        Problems with a clear base case and recursive case.! V9 L0 f+ M7 J5 @

    * p8 v) ~) A; A$ z; gExample: Fibonacci Sequence5 X0 H# J. `/ E8 E
    : }% W# A1 M2 C& c6 r/ _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ' ]2 I1 t& S2 S- o  t- O- `' Z  s' x% f
        Base case: fib(0) = 0, fib(1) = 1/ s/ X3 g2 V5 j9 U* E

    - @: {! O3 m2 ]3 v; b, \2 ~    Recursive case: fib(n) = fib(n-1) + fib(n-2)9 `/ B& ?$ d% d' J0 K: _
    9 l5 K% |( x' N* ^8 N. Y6 Q
    python
    ) [. H/ W& j* ~: X6 `5 h5 B0 h3 Y( {/ O* W5 R3 K

    : G2 q; p: w! v! h" ~def fibonacci(n):% u0 Y0 V1 w5 w% r0 P' S
        # Base cases
    0 n7 V7 L. X2 E4 ~: ]' Z- U    if n == 0:
    7 i$ w2 d; W- l) x2 G        return 0! g; M' O) {: a
        elif n == 1:
    + Z. ^* Z4 J) k# `/ _' N: c        return 1
    0 H- S4 R& z- i  B, b! h8 g5 [. Z    # Recursive case
    ; J0 N( E5 N5 n& T% A& ?    else:
    $ Y3 [! d7 G  J' g* L* o        return fibonacci(n - 1) + fibonacci(n - 2)
    " L! M  Z& M- X9 W$ p$ J$ D. J) Y$ h. ]3 P2 A# G& A
    # Example usage
    ! G" d5 a' @1 T& R' kprint(fibonacci(6))  # Output: 8* z" [! S  S/ N: _7 }% M

    3 ~6 Q7 |) V$ T6 L9 c: M( LTail Recursion
    / @4 s' s+ o0 ~
      X  `9 c4 f+ ITail 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).+ a; I& o0 [: W3 }2 }& {$ y

    0 d- n& H0 `  w1 C2 cIn 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-3-15 21:48 , Processed in 0.059124 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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