设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    * r' B$ t! p0 O5 B1 q5 g
    : v4 G4 e8 F* U2 {6 P6 H: T; ^7 W解释的不错
    0 u7 o9 `; `: E* L. w
    ' Y( q" G. r, J" {( ]4 i0 j递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ P5 n& N7 T! \& W

    9 ^% b( _! n7 O3 }( [1 Q) R8 m 关键要素
    3 g( c: o7 C. _& v5 ^- ^% `8 ?1. **基线条件(Base Case)**" }* y  M$ f* \3 s& F8 w
       - 递归终止的条件,防止无限循环
    ( M; b, R) `! I- d) d4 e7 c- _   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1, P7 j7 r0 L2 _  o# y: \8 B/ Y

    9 n, ~8 S: H+ |. _6 o0 r# N2. **递归条件(Recursive Case)**9 w4 N7 ?4 X- e
       - 将原问题分解为更小的子问题2 N" s+ n6 @9 \$ |4 i
       - 例如:n! = n × (n-1)!, R, b/ W+ Y4 U2 C3 k) O, K

    # b# j4 n2 x8 j% { 经典示例:计算阶乘
    - F# F- p5 P3 V# h  ?8 Rpython
    3 D4 j: w2 o, g1 ~/ D; Ydef factorial(n):9 @# [5 B1 s$ X7 [' C
        if n == 0:        # 基线条件
    . g" W+ P( G( M' G6 k+ @  D8 h        return 1
    & i% ^# o! Y& b    else:             # 递归条件
    ( V4 Q6 n0 t( q& [. r- F2 E        return n * factorial(n-1)3 L( P6 n6 @7 ~2 p2 e5 C
    执行过程(以计算 3! 为例):. R$ h! m$ D9 |6 @9 v! |
    factorial(3)
    ; e' e% e' t+ F0 B: u, v& P8 g3 * factorial(2)
    8 f6 _2 f1 }9 {! a3 * (2 * factorial(1))
    $ t4 M# Y! P& t  K3 * (2 * (1 * factorial(0)))
    ; j( V$ O! z9 W1 ^0 @8 a; G" y2 x3 * (2 * (1 * 1)) = 6
    " t( `% R; P8 ?% u; p# g
    " D. J7 n; d4 c: X 递归思维要点
    0 n1 [+ X  S) ^" Y1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    0 S  D; i+ z9 @! h. \  G. P2. **栈结构**:每次调用都会创建新的栈帧(内存空间)2 |* n4 d# f# @# z% z
    3. **递推过程**:不断向下分解问题(递)6 A$ T$ P7 n, \2 I0 G
    4. **回溯过程**:组合子问题结果返回(归)
    ( y% f3 w: c3 E, E6 ~
    7 D! a4 e2 d( p$ z 注意事项
    / [$ k: A  a4 O5 B必须要有终止条件
    ( B1 p* K8 {/ S6 o$ d: B2 P4 i递归深度过大可能导致栈溢出(Python默认递归深度约1000层)  p) W0 \" j. f+ S+ O/ v) h) ^% Q
    某些问题用递归更直观(如树遍历),但效率可能不如迭代+ I4 }- h) U8 m8 L' h
    尾递归优化可以提升效率(但Python不支持)6 z2 R3 P0 M6 T/ w# a

    $ E, o1 {& K  Q! e+ I5 J* d 递归 vs 迭代
    ' ?6 b' H% ]1 u2 u. k1 N: p# `9 B|          | 递归                          | 迭代               |3 ?- u* ~" r5 \% I( h0 B' X
    |----------|-----------------------------|------------------|+ A* ?+ q! R2 I8 F$ c5 H
    | 实现方式    | 函数自调用                        | 循环结构            |3 x+ E  R/ G/ G' t. u$ U1 @
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |$ l3 V' k9 V0 E
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |; ^1 W  a4 F3 i  H% B# H
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 n! e1 o  d3 e9 o1 K$ ?$ C
    ) P% U. q' [. G5 s, s$ M( f 经典递归应用场景) ]  Z- K' Z$ W1 s4 K) a
    1. 文件系统遍历(目录树结构)
    ) O* `+ a! t6 ]+ }2 \2. 快速排序/归并排序算法$ O5 S- k, c% o" W4 z1 M! I
    3. 汉诺塔问题
    7 A4 u' X" T; ?( Z; E3 w4. 二叉树遍历(前序/中序/后序)% c7 {% C  n6 ^: W, T+ g$ P7 W+ e1 m
    5. 生成所有可能的组合(回溯算法)
    9 t3 |' b0 f" |) B# ^0 c
    ; ^% i( ^% I; ?+ \* X试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 13:13
  • 签到天数: 3177 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    / V* ]0 V! o0 C3 A6 |6 z我推理机的核心算法应该是二叉树遍历的变种。$ h2 t' |$ `8 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:0 V% C4 V" Y: ~1 T
    Key Idea of Recursion
    2 Z0 Z1 ~( x# z' x) F4 C8 {/ p% w: \: |5 o
    A recursive function solves a problem by:/ P5 F" Y8 t4 O2 o, `, q2 i

    ' N7 @  \0 A( A# z# j; K    Breaking the problem into smaller instances of the same problem.
      A# P  ]8 T' j" P" u! O" t
    % w* v( M8 J$ Y9 J) L! t    Solving the smallest instance directly (base case).7 a% T6 O7 T' a! T( S
    $ n0 f2 h8 h8 Z  I6 X7 x
        Combining the results of smaller instances to solve the larger problem.
    : G* W6 @' P0 T) V" f4 o4 t+ _( I+ q- a# Q* E/ e
    Components of a Recursive Function( `+ S$ b9 ]0 }6 D" s8 O7 Y* Q/ r' m
      |+ u( e: ?7 b, \! |+ F
        Base Case:) M2 h: r0 [: t. X

    # c+ u5 B  Z1 y; p7 H        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# Z3 g1 z# H4 ^7 F0 D! W/ ~9 ^
    ( l# I9 `, y( T! c' A. q2 ~
            It acts as the stopping condition to prevent infinite recursion.
    : c& ^9 W: z1 {8 N# p1 U% q2 I9 T. k' T- L) a7 }5 J
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' F# \  p% O0 _4 R8 z# Y4 y/ t+ \* h5 O) _& S2 P
        Recursive Case:' r5 m5 \  }! `# a1 u0 N- n+ x
    2 Y  a; P# }3 \2 l$ b( k* z
            This is where the function calls itself with a smaller or simpler version of the problem.
    + e* r4 E3 N+ r  t0 D0 _
    / J; E" r0 B* ]' R2 G% L        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; h, ?& }5 }' P( A5 U" L/ _) R. Q
    Example: Factorial Calculation
    1 E! M/ E! f# |6 g5 k' T; n1 T) r' k7 S0 n0 N% ^4 t
    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:  b) Y& G( h) N, V% e$ r( s

    ! m# f' j9 K5 ~' O! B" u    Base case: 0! = 1* d0 S: \- T( e( M* B" W
    ' I3 q  S: e; _/ w. G- T, D# e! {
        Recursive case: n! = n * (n-1)!
    2 f2 }5 z8 U9 m0 I7 N( s# W% _& r. H1 B! ^6 F
    Here’s how it looks in code (Python):
    8 t2 b+ _0 B% p, [& e3 l; qpython
    # ]& O: U8 A9 l2 A  N
    & S5 W. N' k8 {; y9 Q
    % c# X! j- N5 O* H" R6 kdef factorial(n):
    + }% k; U# u4 }: W    # Base case4 x' n4 H9 q$ ]* b3 C, \" W8 p
        if n == 0:
    0 m) }) d# Z) ^( _        return 13 e7 f" P1 v" h( v
        # Recursive case
    & X8 z% z) l" ?    else:4 d$ H( ]) B' S- n0 e" z! T
            return n * factorial(n - 1)
    7 f9 v) H8 ?  z
    - T# {+ w8 b: p; {% h9 k( u+ O7 g% q# Example usage
    - Q0 v' R/ k! h9 i- e8 Tprint(factorial(5))  # Output: 120+ P. s5 L3 f6 r, e2 t9 q( u

    1 _9 l0 t4 |+ r  F8 g3 JHow Recursion Works/ G, M2 p6 C2 Z* U4 t
    . G2 x. \% J9 ~
        The function keeps calling itself with smaller inputs until it reaches the base case.
    : ~8 H3 X' r. s4 F! v3 K
    ! b, z6 |% j9 X$ q% w7 }% U9 t    Once the base case is reached, the function starts returning values back up the call stack.
    & O4 @& R7 {7 [* ~5 D( J. g& Z
    / X! C# u* s, Q& |/ t9 Z0 B    These returned values are combined to produce the final result.0 l2 B' o5 s/ v4 x! O  _( g! p$ V

    ( W, f1 M8 G  `9 g% fFor factorial(5):
    6 ~/ i) E% }1 R" J
      G( u) d# _9 ~. B' `6 I- S
    * T2 C  c+ w  K' P6 p! V2 Ifactorial(5) = 5 * factorial(4)  q* @. |- E* A6 n0 H
    factorial(4) = 4 * factorial(3)
    7 r0 b; T. X% Ufactorial(3) = 3 * factorial(2)/ ?7 b) m) E0 c" J
    factorial(2) = 2 * factorial(1)
    / ?' v2 t3 M! e1 y' ^7 efactorial(1) = 1 * factorial(0)
    * _8 z5 \' i8 J* b4 jfactorial(0) = 1  # Base case1 t1 X) d* X' x* v+ U0 y1 x" K
    / r. H1 y0 h# A) ?- G8 Y
    Then, the results are combined:* y4 [% {) l9 b( S& f8 r5 m
    ! c! n- \; X8 X. d5 D. i

    8 b( C- P) \3 E, H/ D' A+ \factorial(1) = 1 * 1 = 1
    1 F! ]9 I" D- |" M' v% B& C+ Rfactorial(2) = 2 * 1 = 2# ~  H2 S6 d/ e. G7 K: R) Q
    factorial(3) = 3 * 2 = 65 j! R* z' H: F+ U! M) I
    factorial(4) = 4 * 6 = 24
    : f5 G: Y5 {, I$ jfactorial(5) = 5 * 24 = 120
    * ~" J/ C" m% y! @- |% b
    & e! f# \7 X6 D2 k" d& r* {Advantages of Recursion
    / Z/ [" [' x/ m: W; W. C, ~& N! y; G6 ?, d6 m, R& g
        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).1 E  w# Z1 x, {& d2 K6 o
    / M1 ]+ L9 X5 r0 z
        Readability: Recursive code can be more readable and concise compared to iterative solutions.7 C, q& F" x) b2 I/ C9 g) k- R

    $ ^# r3 n& K% b0 t) K) T1 JDisadvantages of Recursion  m1 u# I# J' W/ ^
    . t( n* k$ S7 G1 S+ |/ I' |" D
        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.( u: G( C& u6 m! H3 v$ o
    / x2 x& G" |- E
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).' j/ g, z& N4 ]5 x. p

    . z/ E# T& _5 a" Y$ X2 o" \, vWhen to Use Recursion
    ' V! V! i0 I# h, {  y8 k3 R5 F, q* ~' Q
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).$ G1 S0 Y8 C3 C# z2 p

    2 N9 S  j- x0 U: n' Q    Problems with a clear base case and recursive case.
    . v: r* d+ {/ c1 V; }8 d
    - [7 n' B+ N  p; w9 p9 }Example: Fibonacci Sequence
    # [4 R( |% A8 j- B. x: p1 k0 {0 w9 _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:, V. d; z& F5 t: L3 D" I

    # M2 x. F4 @/ e7 b$ |  O    Base case: fib(0) = 0, fib(1) = 1( l) i0 o" R: `/ m
    3 X8 q& q( A4 Q. \' _
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    0 c( I0 x% }- f$ [4 ~% I( [
    8 v1 c. W4 Z* v: H' Y7 Rpython+ h1 u( l" d4 ]) @* j
    7 l2 A6 Z: W, ^

    3 z: N# n' n! v+ ]def fibonacci(n):
    8 \) {1 Y3 e' L3 H7 i* E. [    # Base cases/ [. G; c( T+ V9 P0 z- r5 b5 f
        if n == 0:
    ) L2 n! G# |/ W4 H7 V        return 0% c9 j/ d9 C% q3 {, x
        elif n == 1:
    ; K0 w& F& C5 Z& h; G( b7 e        return 16 d/ D! H6 r. b$ l. H
        # Recursive case
    ( ~# W: E9 _; z    else:$ S3 P9 i4 q+ J+ v/ R" l( k
            return fibonacci(n - 1) + fibonacci(n - 2)- e- g! o% ?5 K0 L0 z& o' v/ q4 U4 S
    6 |6 v3 \6 t) u: M
    # Example usage6 G* C/ Q" x: c" j- q% B4 U
    print(fibonacci(6))  # Output: 8
    6 c7 W2 M7 f% _  O# m/ y3 r
    . Q1 j* d# z* ATail Recursion
    % q3 }* h9 Q, i0 L: }5 \0 x& s$ f' j6 v* e8 W  b
    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).
    4 `9 d' V2 j3 W
    ; k3 i6 s6 Z. [8 O, WIn 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-2-19 04:16 , Processed in 0.056784 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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