设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ( X* \: p* r$ ~& C) O7 H* C/ W' t' b% z& f
    解释的不错
    5 K3 C- s+ [5 \9 E* v/ a0 U% h
    8 Z6 P/ d8 L6 D# [递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ( V  J% ?2 {' F( G% m" u! F& D; ^! f" Q8 t
    关键要素1 i4 c3 h% B* K* d6 k
    1. **基线条件(Base Case)**
    1 V' w! m3 E$ I& p6 k   - 递归终止的条件,防止无限循环
    * j! [- D; r/ P! @, Z+ ^1 G& W6 G   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! ~3 M$ E$ U8 p

    5 ^% h# r) r4 w7 Q4 m2. **递归条件(Recursive Case)**
    7 |$ W/ @* d" D   - 将原问题分解为更小的子问题
    & M, s+ K! c" E( H. T  \- V( _   - 例如:n! = n × (n-1)!$ m( M. y+ }0 W& Y5 s6 X1 Y
    " a) L2 H7 j* d
    经典示例:计算阶乘) X# q& r, x+ `' w) t1 V6 n
    python
    & r! \( W" n: edef factorial(n):
    ! w0 ~' ?' `$ H3 V- c    if n == 0:        # 基线条件
    7 s/ b! n) @/ l) ^* q        return 1
    1 G/ \7 E! I0 y: ?$ L    else:             # 递归条件
    8 f6 P6 J) S$ j/ N# j6 ^6 ~* J        return n * factorial(n-1)
      x# F) y. W2 \3 c执行过程(以计算 3! 为例):
    9 Z) V4 _) Y0 C7 ?1 @: B; gfactorial(3)* C7 I$ ?& m/ S
    3 * factorial(2), G% z& _$ ]; o3 v
    3 * (2 * factorial(1))
    8 P% k' J& @9 u& V: K7 \" W' w3 * (2 * (1 * factorial(0)))" C( c( U5 i' h  D4 A  {
    3 * (2 * (1 * 1)) = 6
    , r/ p* k5 r8 [: F2 S6 q( g8 g( f
    ) {2 I5 G$ F, x9 o 递归思维要点/ @) o4 u* Y2 A5 I
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* `/ @# s8 M( C
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    # c) G: I4 c0 I! W3. **递推过程**:不断向下分解问题(递)3 {3 }, K( U+ z3 N- }
    4. **回溯过程**:组合子问题结果返回(归)
    : _3 P! @5 g. ~. o! i* o
    5 W" u% X1 R* C) f" x" g 注意事项3 ?" h" l( [4 K2 m
    必须要有终止条件
    2 `7 ^$ G% q! b4 |递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 H% B6 s3 R. T, C  p
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ) C8 S5 l1 Y: S! ?! W尾递归优化可以提升效率(但Python不支持)
    ; P' ^; n0 l' _$ h. J# E1 r; X" v% G8 x
    递归 vs 迭代7 b9 G( W2 l" w, M+ n4 C  q
    |          | 递归                          | 迭代               |
      i( e! I" g! s: G|----------|-----------------------------|------------------|+ z# l6 Z" L! u" ^2 V; Z, V
    | 实现方式    | 函数自调用                        | 循环结构            |3 u+ X8 g! y$ ^* S: |3 l8 T
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, U6 ~% n8 ^: G; ^$ \6 c
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& L/ p+ R3 Y4 y
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    7 J4 o  W% s3 A2 n5 D
    3 |5 [: x6 h- D9 E 经典递归应用场景
    , W) P# O! @' Z: [+ |  x1. 文件系统遍历(目录树结构)6 c1 _1 v6 ^5 R' X( g1 T8 K
    2. 快速排序/归并排序算法
    , x* Y8 }: L5 D/ \7 P2 j' C4 V5 @* c* f/ S$ g3. 汉诺塔问题! A/ U5 |/ Z3 c
    4. 二叉树遍历(前序/中序/后序)
    ) N+ i" S0 @9 D8 W' ^. ^5. 生成所有可能的组合(回溯算法)% [7 X: v% m7 ^, S( S

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

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    12 小时前
  • 签到天数: 3102 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,: w+ s- O3 N+ c- n! w  D7 |. J3 N6 m
    我推理机的核心算法应该是二叉树遍历的变种。/ w0 @* t5 ^+ p! `) n2 S
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . I9 T& i+ Z; W3 O$ E0 k: RKey Idea of Recursion- Z. [: ~" C; I! e2 d
    : i; j3 I5 V# y) l& l7 ]
    A recursive function solves a problem by:# O7 B/ U1 o$ G- v
    4 ?* e. o& @; a% @
        Breaking the problem into smaller instances of the same problem.* n( U# W) s7 |- f; S, o

    5 m) k2 c/ g0 q; e3 G. @& r    Solving the smallest instance directly (base case).
    ! V1 a* c1 T2 o0 V/ H0 C
    - ^/ T* A; J1 a    Combining the results of smaller instances to solve the larger problem.2 V3 R. e3 w5 _7 R" `( \) I2 L  U+ \" i
    4 r8 s) z: c9 f* R
    Components of a Recursive Function1 ~' J2 Z4 i* j  t0 M( F

    6 N8 t) B" S. b+ T- F, K    Base Case:
    ; M6 [+ B( j1 u- g/ B$ {5 Z. E( E- y% v6 L8 t' I
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 t; c. M7 A) M+ P) s
    * L  F0 _* s& @$ c) `7 w$ z, a
            It acts as the stopping condition to prevent infinite recursion./ Z3 B6 p  I! o/ g# M8 B( c
    6 ^4 L1 {  ~: E' z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' {" e& w( [  G/ q& M! ]
    % i5 j. U8 z( ^! H* s" j% ?  A    Recursive Case:
    " q- e8 w7 T) `5 R
    % v" `& l' V/ {; O        This is where the function calls itself with a smaller or simpler version of the problem.
    6 Z- V( C' b+ e& E
    5 u8 x) {. E0 V        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).3 N; U& ^0 {$ q

    , c1 T" h) z6 A" B9 _. JExample: Factorial Calculation
    $ B( @* Y1 {; N9 s% @  F/ O
      L" j! n1 g2 @4 |' }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:+ k. Y9 _3 \% K3 e! ^6 T( l# w" y3 n

    , n6 Y* L: i- |1 l- b    Base case: 0! = 1
    - ^( X; |2 f* ~
    / E, V+ a# `% e2 P    Recursive case: n! = n * (n-1)!
    & X, @# a7 n) o$ d2 O8 W$ W5 x. n6 s, k& ]$ P- l$ D* l) X; f& v
    Here’s how it looks in code (Python):
      U8 v/ }7 m3 s6 Vpython. \8 ]: \# J" r$ ~9 L  o1 P

    ) R! u& \7 {6 F8 M1 q$ P( ]4 |3 P
    ! j6 l( `/ P$ P* m2 adef factorial(n):8 A# f/ M! H) o, x9 ]
        # Base case/ m! z2 U4 z1 [  @1 A
        if n == 0:
    4 Q+ j5 ~7 S# }7 V" ~% ]  G        return 1: z6 E8 L0 x1 [- G% g7 B4 I! m
        # Recursive case6 ~( L2 {. Q( ?9 j; K$ K% p" ~
        else:
    / ]! d3 S- B) s+ }7 s        return n * factorial(n - 1), k9 o2 e) q  s" e4 N

    / u8 g8 }3 M1 p. x% Z# Example usage2 ]7 c& ^7 m. D' [9 `
    print(factorial(5))  # Output: 120
    $ w3 z* H, p0 k. Q: `, r  g9 z7 u* R) Z4 X, q
    How Recursion Works
    & j- Z  `, H; J) k7 @8 D
    & P9 y6 w8 f) I0 M  m! p    The function keeps calling itself with smaller inputs until it reaches the base case.3 @- E2 B, S2 `

    8 T* y5 |1 @6 s# i( t' v0 X* a- t; C    Once the base case is reached, the function starts returning values back up the call stack.% [  D' u& q2 N
    ( {$ A: B8 Y3 G  Q# ?, B
        These returned values are combined to produce the final result.
    $ Q, S2 R$ N; N+ G( w  _$ [& k0 c% \* z% M+ N: S" O1 |9 A( I
    For factorial(5):$ Q# i- _5 G* j: ?
    0 _) M6 n( f3 {& o; ^! |  |$ }
    % O: V) _8 Z& ]6 M
    factorial(5) = 5 * factorial(4)
    1 S( f' o1 @+ X" f& Y( }factorial(4) = 4 * factorial(3)& g9 K7 `. R: I, i0 ?  m
    factorial(3) = 3 * factorial(2)
    1 x, t( A" p: B3 [5 `. Y6 Cfactorial(2) = 2 * factorial(1)4 V' W3 Z7 p4 p' [1 ?& i1 A4 M
    factorial(1) = 1 * factorial(0)! X7 ]4 b; t$ }* N6 ~1 T' Y
    factorial(0) = 1  # Base case  c$ j, s4 G- S4 u
    ' E) b# m: x8 n, [6 L3 R' B6 E" P1 L
    Then, the results are combined:2 q. B  z$ o6 M4 G$ F5 f8 N/ I: `

    . D, q: H$ m* u9 M. V9 \3 B
    ' p! O8 Q$ u, v: m4 V. Qfactorial(1) = 1 * 1 = 1- u5 @% k- q# c# S: s6 t
    factorial(2) = 2 * 1 = 2) _, H  W, s. u) _: _# ^/ t
    factorial(3) = 3 * 2 = 67 [2 ?# C- {6 e8 X
    factorial(4) = 4 * 6 = 24
    4 S0 z0 B5 r% r9 Rfactorial(5) = 5 * 24 = 1208 u) a1 W$ o( }  {* v
    - T+ k8 w2 Y1 m2 M" [
    Advantages of Recursion/ _! N$ ]7 U1 I

    6 r# V$ ~2 T  X2 s) D    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).- \7 Q# T0 P$ M" H/ _& d3 z

    / l/ i: P$ X1 V, y# T& m  @; Q    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; R0 v' L9 x) m5 V" b) ^, h# H, F7 P( Q
    Disadvantages of Recursion
    / A# |6 H5 M( _: v& F3 [% B
    & m; ~- D, e4 M8 G7 C" k# y    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.- g  f6 x  F3 G& x" u6 J

    $ d' g. b9 b  o- Y7 T3 W    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: U" h4 D7 h- T7 U/ d
    5 G& }' |9 w5 b3 n- k- {- E
    When to Use Recursion" G4 ?& u& g- {! |7 S: o" e

    ) N! o7 a& L; P; x' s    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)./ C6 ~: f4 _; G8 y2 f4 H
    7 Z0 P5 a# R* g* P! o: j
        Problems with a clear base case and recursive case.( u& V: k, P9 Z0 {! B9 s! @

    % U$ t; n& @8 ?% E$ DExample: Fibonacci Sequence& U) l# C" h9 r5 m6 s
    # s, s3 l( K' h) U- T
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # v) X; W% o" e4 L! J6 Q9 P. u
    % p) ?% k3 P, t/ {. T    Base case: fib(0) = 0, fib(1) = 1: d5 r$ X+ M4 U

    9 N) o( U  J. P. f7 c    Recursive case: fib(n) = fib(n-1) + fib(n-2)( {0 O* Y) z$ i" y

    3 Z, p. k, I+ d% F( zpython/ s! j  h) Q, ^( Y2 D

    + u6 N, }/ g; X  [, a1 R: Y9 }$ y! `( h- e  S  ?; h1 z
    def fibonacci(n):& S3 U1 t6 V/ D# Z8 L( U; J
        # Base cases/ c; G) X9 Z3 a2 m5 W
        if n == 0:& ]8 _' W* ?/ @% l5 x* h
            return 0* C. r) Q* r( `' A
        elif n == 1:+ V, n3 O, _0 a+ _; `. e
            return 1
    : D3 |# M/ ]0 N+ O    # Recursive case
    5 u( c0 s) ?5 [5 }9 N$ M% o    else:* }8 C. p* `! R5 ?' X
            return fibonacci(n - 1) + fibonacci(n - 2). m7 M6 r3 V3 h' p+ b
    3 ]3 v* _4 o1 ^, p1 N
    # Example usage0 u4 M( l, g" J: `
    print(fibonacci(6))  # Output: 86 {8 m1 I% |; E! y: {
    / [; q2 i+ V9 C" h" D; O$ e- G* c
    Tail Recursion2 T  q, ?2 s; {! U
    : p: c( e. q" d6 A8 X* |' s
    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 ]2 l% P' c7 a

    & M. q3 F) W1 r  [3 BIn 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-11-26 18:46 , Processed in 0.030643 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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