设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 * `2 m; n; N* `: L
    ) t" ]7 j% S+ U4 J+ e' i
    解释的不错
    - h& n! L4 j; e' [0 ^. X! \
    # e; }3 ^1 T* w) I- D2 f# L! |递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ ]( T. @' W: L2 R0 e

    3 H; L1 t- K0 D  [ 关键要素
    ) r4 R( E' ]2 q7 r* N1. **基线条件(Base Case)**
    8 y" }  D3 H3 E/ y   - 递归终止的条件,防止无限循环
    , X) T) e7 d0 D5 ], [! k4 y, u   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1( F/ M, @7 E: t+ o* {" _* t( j3 c, \

    * a: _. r# o' s6 T1 h2. **递归条件(Recursive Case)**
    ( L  Q( f" d+ g+ c6 o   - 将原问题分解为更小的子问题4 _  N7 Y) U% k8 R( }8 c
       - 例如:n! = n × (n-1)!7 k+ y/ h: M! J$ D  F8 k

    1 u; |. C+ P. D. Y) N7 v/ \ 经典示例:计算阶乘& Q3 r* R, ~: Z' z! ^/ H+ m! v
    python
    8 y! ]+ S! W1 S) bdef factorial(n):
    ! |) u- \$ L7 ]8 x: E    if n == 0:        # 基线条件, Q; A3 L. l; b/ I9 C
            return 1) O3 ^! Y' @, Z0 q/ k! e
        else:             # 递归条件. g. u  K. s: C# ]+ ~9 G6 }
            return n * factorial(n-1)
    6 ]$ P: _- r3 ?/ a* g$ ?! h2 @执行过程(以计算 3! 为例):3 S: t) D1 y3 T% h
    factorial(3)6 ^& H3 U1 @  B7 Y. u+ l% O8 i
    3 * factorial(2)
    ) v1 j  C+ }5 X% _3 * (2 * factorial(1))* _) C. j9 }+ m# u6 b$ Y7 K0 @
    3 * (2 * (1 * factorial(0)))* p; n, n# j% g! q9 H- u
    3 * (2 * (1 * 1)) = 6
    ) \$ ~5 K) `7 o9 x$ k' b0 t& L) z  H) v& C' ~% b
    递归思维要点. N6 y0 F8 V7 }. K! T5 e; S; S9 a
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* z9 u0 ]5 M0 H9 u
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    : [& Z) c% [: r3. **递推过程**:不断向下分解问题(递)! }$ ~1 x" z. x" Y
    4. **回溯过程**:组合子问题结果返回(归)7 D3 r, H/ m: c* t, I7 _
      L8 q! n# D. g2 O$ Z. ]5 X6 E1 q
    注意事项
    # d, I/ Y7 ?4 b/ r1 S0 S, k8 _必须要有终止条件
    1 R0 d% x- o9 _1 T3 i递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    : K) U0 p" p6 R/ f  d某些问题用递归更直观(如树遍历),但效率可能不如迭代) e4 [, P4 F9 A- M  Z
    尾递归优化可以提升效率(但Python不支持)
    5 y  U) f1 z( T. M+ f+ M2 w+ F9 G1 H# C
    递归 vs 迭代: {2 i6 @) {. h6 \
    |          | 递归                          | 迭代               |
    + `! _: Z& L4 a|----------|-----------------------------|------------------|* M; r5 |+ v! J& ~
    | 实现方式    | 函数自调用                        | 循环结构            |5 ^+ M; j  t* x  e# y8 v2 P7 b
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |5 S" w' V3 W6 h) N
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! O  {2 X; ^, h3 ^- g| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |, x$ V/ w  |; a# L
    5 y3 C# O- C: h" j; w
    经典递归应用场景
    4 C' L0 C+ @6 z8 E2 E  O, v4 V1. 文件系统遍历(目录树结构)
    ! R+ i/ u  i( f2. 快速排序/归并排序算法
    ) z; |+ S8 o& a4 I, o0 T3. 汉诺塔问题
    # Q0 ~: h  m# b/ y4. 二叉树遍历(前序/中序/后序)
    : _7 |( b( P6 A5 w6 p0 c6 U' V  n0 p5. 生成所有可能的组合(回溯算法)3 E$ V" T. N. H4 \- Y+ U. _3 ]

    7 H4 [1 F6 {5 Y' ]* r试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 07:05
  • 签到天数: 3143 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    0 A% J7 v+ }0 ~! V) [, N3 \我推理机的核心算法应该是二叉树遍历的变种。) Y2 g& @- l. T1 C: U  C3 g/ t
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 p; F& y4 \8 `4 \& ?" L
    Key Idea of Recursion+ ^* a- ], Z4 E, o

    : `/ o; |" w8 P' sA recursive function solves a problem by:
    6 c2 f5 y+ M' r/ f
    2 U# e3 o2 e1 V    Breaking the problem into smaller instances of the same problem.
    7 c- C# |- C9 w! C- d$ e) `- c+ P4 z# k! X1 p2 K
        Solving the smallest instance directly (base case).  X+ s# a  s6 g7 d) b

    8 r0 }/ u( V7 D    Combining the results of smaller instances to solve the larger problem.
    ' {+ g! i  [; f; E  s/ ]" S0 C4 `3 ^
    Components of a Recursive Function
    , a% d0 u& U; g( ?, G! i9 ]4 V" b( I/ z( a
        Base Case:
    8 f( @% Z, {2 ~' S7 Q! R+ \# R8 r) x
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ! }+ f2 p0 j% H; W. d3 t
    1 V/ H2 A  A$ k" j0 d        It acts as the stopping condition to prevent infinite recursion.: ]& v9 k$ }; H  k5 X: U# }4 R
    * h- l+ o* [3 U- p4 Z9 c8 `
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& h- |4 K* t# E5 {8 f" t
    2 K- Y4 R& `; D' q: R+ s+ q2 x8 L, U
        Recursive Case:
    ( e* e+ |- X0 \( G- x  w) L% W* A) |2 w" p% x# A: p
            This is where the function calls itself with a smaller or simpler version of the problem.
    + O1 h6 |% [$ e2 J- I# Y
    2 A+ v- J3 M# _3 Q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ N  y- u9 {$ x6 I7 E. h! Y

    + W* W+ Z- e  f6 ~0 tExample: Factorial Calculation( f) t6 x# n( E' P- B* r, V
    9 ~7 Y; i5 p' {: W
    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:
    " E3 n0 Y9 G% }# ~' b1 Y/ u
    $ c: j6 P# E/ X9 `5 N% ~    Base case: 0! = 1
    ' @2 j2 q0 j! G6 ^& G3 x/ O8 _* s. w% Z7 R8 i- t4 n; g$ b
        Recursive case: n! = n * (n-1)!
    2 P) n1 ]- v3 A/ E
    + p3 x# k" H% m8 l1 J: z% FHere’s how it looks in code (Python):
    9 F  S) O' e# w! P( Bpython
    ; c- ^. ^6 U1 v: B- m
    , L1 t8 t' E$ G4 ]7 e4 s/ Z
      {$ x2 w8 A& p/ Z1 Ldef factorial(n):) W+ z: M5 Z5 e$ v
        # Base case% h3 @' s3 q+ }4 ?) {- ]
        if n == 0:
    9 _  z3 `3 y+ L  l5 R0 {        return 1! @2 C# z+ h$ |* A
        # Recursive case- X+ \! U% e! [0 k) [' H. O& C
        else:
    0 @+ _# U5 L5 {6 ?4 Y% S' t5 _        return n * factorial(n - 1)
    . x4 Y3 H2 Q, `
    / h( R) l* r0 E" l0 s# {/ e# Example usage% N: N- q% G2 f+ @1 n4 o& ]8 |
    print(factorial(5))  # Output: 120
    & J' s: _0 ]* E
    $ D* ~: s( [; JHow Recursion Works* ^+ j7 i0 D5 ^  D6 j

    / m% Q8 c" {0 _: u  T6 p+ L6 Y    The function keeps calling itself with smaller inputs until it reaches the base case.7 r; C) k' c; H2 }, [- a8 E6 |" C
    ) F+ a6 v: \6 K1 B% q
        Once the base case is reached, the function starts returning values back up the call stack.$ Z3 f: s9 y2 `7 q. h/ j6 \
    : _7 u6 J2 a  H4 N. {
        These returned values are combined to produce the final result.
    2 N; }5 Q' j$ }; h  a* ^3 F# C. ?* R. I( _
    For factorial(5):
    ; S) a; J7 u+ `4 W0 E( m* X1 d* ~# u: n) K6 Q0 G3 x6 Q
    7 H) i- i, R3 F7 B  i5 v" Y' U. ^
    factorial(5) = 5 * factorial(4)
    9 s3 [& v0 X# |factorial(4) = 4 * factorial(3); {1 c' U  [+ Q9 j
    factorial(3) = 3 * factorial(2)
    # w) q3 `0 C( V  e/ Zfactorial(2) = 2 * factorial(1)' W8 ]& _9 Y  m/ o5 P$ k
    factorial(1) = 1 * factorial(0)
    ; o2 l0 \1 f' ~factorial(0) = 1  # Base case
    . F. L9 d& C6 }# [# X! ~8 \
    / i$ H7 ~7 h3 w) ?! UThen, the results are combined:% Q" f- d+ b5 a+ k$ K! u/ U1 Z
    0 u% A) e  N0 d: W, |

    8 P* l( e6 k+ G* xfactorial(1) = 1 * 1 = 14 A- V. j; Z5 I1 i5 O. h3 d
    factorial(2) = 2 * 1 = 2
    # ]' b, g  N3 k% G1 efactorial(3) = 3 * 2 = 6
    & {) l" d( |2 r5 J# I" ^$ _factorial(4) = 4 * 6 = 24
    2 Q$ O% G0 L" Dfactorial(5) = 5 * 24 = 120
    0 Q: c; a: @  t3 u1 R0 c, J
    ; v6 @" }! B! B2 c6 p: k* q( s2 tAdvantages of Recursion% |" l  y: @' t- W
    7 W3 r/ z5 P+ _# q* _: J
        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)." h% O6 m" X" A! {5 p2 {

    1 B$ W7 O1 S% G2 R3 Y: x/ C    Readability: Recursive code can be more readable and concise compared to iterative solutions.5 F& [9 [+ \+ R: B: V8 I9 W

    4 s7 b5 D: z0 G. R, ^$ z7 V4 FDisadvantages of Recursion/ L/ }+ B. T' z4 ~
    6 {! }4 Z5 c; r( v
        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.7 X  n& o! r+ r- Q1 C3 M+ h8 B
    ! t& B# E* b4 t8 J; _
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).' ]) t/ U: t2 y7 F
    # f$ e4 V# H, }; x. J
    When to Use Recursion# A) {0 X! X; z6 m- i1 ^

    / d' s6 S& c7 K4 S* x. d3 ?    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    2 |9 F  j3 |( l- g3 d' v7 p1 T- [( }6 y( t, {' j
        Problems with a clear base case and recursive case.
    ) `$ h. m% e& U" ~) Y$ J; k* w" W; E* x0 M
    Example: Fibonacci Sequence1 p5 p' {) v9 M3 `+ K

    % J2 L, t5 K# ^2 u* `5 GThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    % c* {% n9 ]/ w) K: ~  U! }( m3 u9 J( }3 v2 k* [+ j2 O
        Base case: fib(0) = 0, fib(1) = 1
      v0 z+ k' N$ s% N. i% k, n5 h) s+ e; {* k) t7 Z/ m
        Recursive case: fib(n) = fib(n-1) + fib(n-2)/ P9 b  i2 n; y7 Y

    ; c, G7 F7 [) D: @1 Apython' d) y" A  Q: M2 A( j2 B

    6 ?# ~' i/ @) k
    8 G  M+ z( s  p# u# Odef fibonacci(n):
    * O+ ]8 c% \% |    # Base cases
    . d0 i. s* s( f$ W+ `6 l+ x    if n == 0:
    1 u! f) V& f( t+ w  d2 {3 Y        return 0
    ) _, |* W" h, o5 A' I    elif n == 1:
    8 A+ y- `8 m) i1 G: V, c        return 12 m( I' e/ m% }8 D$ x
        # Recursive case
    4 u  z( Q. u% b5 H) Z7 G2 b    else:$ z. x( ^' }& k3 F! s' D3 `8 J- |
            return fibonacci(n - 1) + fibonacci(n - 2)8 O2 s' F1 Z. c0 I* p

      f* y; H- \4 r5 J5 k! k( f# Example usage0 }# b, w3 j0 }5 l5 j3 U
    print(fibonacci(6))  # Output: 8
    1 B2 q/ x7 `$ _# C
    4 H, y+ e* d& s8 q0 fTail Recursion0 g( O; R: y4 ?' a( g8 x

    6 L: b/ x) S$ MTail 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 q) n  B4 \1 V: M- e
    9 A% q6 i$ y3 U" E" o
    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-12 02:05 , Processed in 0.031660 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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