设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & e  {+ P$ R3 c( H- o4 Z+ L
    / c* T0 @5 Z0 C) M7 ^  v( F* b解释的不错2 j* C6 R" Y6 M1 P6 `! r

    : |# U6 O- i% L: w' i递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ n4 r- a% Y/ o( `& D. J" |
    4 B' H9 a1 B8 {- i4 G4 P6 y
    关键要素
    ' L9 \% y9 s8 I% P& Y. \/ T1. **基线条件(Base Case)**  l, @9 z# x. b' [& k
       - 递归终止的条件,防止无限循环
    ) m6 W' Z: N; k% j   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1- Z3 \) T& {& h, S
    9 ~2 Z# ]# y  [
    2. **递归条件(Recursive Case)**
      p% e4 m! L, p* S* E; i   - 将原问题分解为更小的子问题
    6 {5 r& O& E# H3 \! J. p$ k   - 例如:n! = n × (n-1)!
    5 v3 r* v4 o5 i
    : J6 U+ E0 z3 x' X3 F 经典示例:计算阶乘3 ]' X( d' b9 O& q8 k
    python- q' G& [: W5 u5 C. f
    def factorial(n):( A9 F  n8 `/ y+ l! Z8 g# P/ w
        if n == 0:        # 基线条件) q+ e8 i' _; u8 F, M1 a
            return 1
    ' [# s2 B  Q! o. z1 _    else:             # 递归条件
    & y) q# U8 G) s& g7 ]+ }: N7 _9 u        return n * factorial(n-1)
    + [1 |9 @' v8 y7 G9 c执行过程(以计算 3! 为例):8 p/ m  }' r4 _6 f7 \8 D
    factorial(3)% I5 Y# C) B2 E2 R1 F
    3 * factorial(2)
    $ Z2 Z3 N; q7 H; |3 l! I3 * (2 * factorial(1))4 w( c# E) m& @$ B
    3 * (2 * (1 * factorial(0)))
    6 q. d( @5 G  R4 g2 M, |3 * (2 * (1 * 1)) = 6; Z* n  u9 c2 \" W

    " k9 f: h6 M' g* g. S0 _ 递归思维要点
    2 a0 ?" Q0 i+ O- @3 i4 [2 ?1. **信任递归**:假设子问题已经解决,专注当前层逻辑3 d* u+ @8 o  ~3 L9 Y1 w
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)( v! l8 a. S! A( L5 u
    3. **递推过程**:不断向下分解问题(递)8 M0 x3 p7 c; J7 u. u$ |
    4. **回溯过程**:组合子问题结果返回(归), v; E, e9 u' s- j$ m
    + c; y. D% L; Z, I0 T
    注意事项, }# P* E6 W* l% u; _5 }; m! D! }
    必须要有终止条件
    8 U; P! d$ v' c0 W/ ]6 o递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 |  p, _0 B* c( O6 }$ }
    某些问题用递归更直观(如树遍历),但效率可能不如迭代8 M* |! K4 D3 N4 o
    尾递归优化可以提升效率(但Python不支持)1 n1 D; b  i7 f6 [0 G/ ]

    ( \+ Z" d3 Z! r' L1 A, l: a! L 递归 vs 迭代) }& G1 _* o0 {' o7 J) o- p1 U' L
    |          | 递归                          | 迭代               |
    * ]' {8 w. u$ x, ]& S: b4 v|----------|-----------------------------|------------------|4 f9 L1 q* z/ K2 C
    | 实现方式    | 函数自调用                        | 循环结构            |
    : ~- h  I; s" X* G/ y| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 A& n0 L) y  [; g. N& u
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 k* A- ?: g* D1 U0 B& h9 W+ G
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ! o5 m' ]9 s. C- c7 r  d  @- _4 ~  Z: f& _
    经典递归应用场景
    * z8 e5 ]3 U2 Y7 m8 X# |1. 文件系统遍历(目录树结构)
    # J8 [' n+ r0 _$ L# r0 \: i% A+ c2. 快速排序/归并排序算法/ F1 i+ ~2 Y, i8 Q, `
    3. 汉诺塔问题
    5 A6 }8 X4 m' Z( A4. 二叉树遍历(前序/中序/后序)
    5 @# _4 W, h! z& _5 j# z5. 生成所有可能的组合(回溯算法)
    ' G7 Y; b: L& i% [9 c9 ]
    / d  I9 K' G4 I* z9 u; j6 ~试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    4 小时前
  • 签到天数: 3241 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,! @6 f8 }( \) s% _
    我推理机的核心算法应该是二叉树遍历的变种。
    % B8 y6 e0 v* [* F5 N% O  W另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 s+ c& `8 E& O, B. tKey Idea of Recursion
    ; Z8 Q0 d: J  z7 l" C* S+ S& B+ s! t9 l3 n6 F
    A recursive function solves a problem by:
    % b3 c9 U1 U  \! ~. C* h3 Q- B$ j; U# b
        Breaking the problem into smaller instances of the same problem.
    ; e2 ~" j6 r: D# O. E& \7 Y$ \8 x
    $ D; ?, |+ |- q( O% k- e7 d+ l    Solving the smallest instance directly (base case).6 a/ O3 }. f3 C1 d

    9 e4 Y# Z% m/ B3 B( E4 t8 e  ~& s    Combining the results of smaller instances to solve the larger problem.
    2 ?4 J& Y; K; o9 l, }9 `! N5 H2 `1 }; r" o7 V
    Components of a Recursive Function
    ( u/ d8 O  Y& x3 t
    6 |0 X0 T0 p1 Q  W5 U7 I) Z    Base Case:
    8 _  M$ A7 j0 l) G: P, H; C- q: ]+ _+ ~3 W
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.3 S5 I/ m+ o7 Q$ U& ^5 I. T
      e, {9 |, l7 ^9 h2 b1 p
            It acts as the stopping condition to prevent infinite recursion.
    ! g! `$ R4 _4 \' t2 N& ]
    , Z0 a8 s7 T( y5 ?: p# b! Z        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  i- Z, k* i% M
    * F3 h( _0 D; Z1 f. }
        Recursive Case:6 [1 S$ c6 o% f  V

    , B3 s$ V; l& z/ @8 i3 t        This is where the function calls itself with a smaller or simpler version of the problem.. R! B: P: p$ ^" I) ]

    $ J2 r$ T9 ]7 o: e/ D" m# ]2 X' \$ O5 U        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    4 V! Y8 f! U5 r+ `/ t
    1 ^: u# Z! M& n  c2 \) R" ?Example: Factorial Calculation
    8 h0 B" _# L2 {- [" I. @) h; F' B* O/ S1 e, S- i5 F; ]
    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:
    1 O+ a+ H) @, r9 [& ~7 A' C3 M, x
    ( x1 E- H/ D- x2 m$ ~  H  C' e    Base case: 0! = 1
    " j; a0 h" y% x* u' ^9 L7 Q
    9 E! `3 ~% \: ?/ ^- j. q0 R# b    Recursive case: n! = n * (n-1)!
    ) b: R, {, J  N! W& I# ]: P! A
    , q% x: T7 w& D, |1 XHere’s how it looks in code (Python):, V  y* R5 M1 \+ m2 V6 K
    python
    , r0 ?  m5 T+ w% B% g8 D( [( X) I  t  }
    5 Z* A, s8 A, }$ l
    def factorial(n):0 ]  |. C1 [8 Z& f6 Q: E
        # Base case5 ~( Z& _* ~5 s% ]: L! f
        if n == 0:
    8 D& ^" b0 O* i% s, i7 l        return 1
    3 d/ \: x) Z- {" u4 f0 B/ C6 G    # Recursive case
    ' F' d: n: B+ |7 W% A    else:% i' |6 O- {3 k% A6 }; a
            return n * factorial(n - 1)
    0 ?) v6 I/ m( M. R) v% h6 l. ^' ?; b" n$ v* h$ R/ d. l
    # Example usage0 J3 q: {( x2 Q4 L3 z8 d) `' t7 O/ f
    print(factorial(5))  # Output: 120
    : K3 n) Y; B1 }3 X; ^. s0 R4 F' s/ f
    How Recursion Works- H3 N! B9 W5 U3 l) J

    9 M3 j/ j$ ~% i; R9 ?4 A    The function keeps calling itself with smaller inputs until it reaches the base case.
    + a3 a9 g# V; ]* \7 N. s# W5 u, B4 M5 C
        Once the base case is reached, the function starts returning values back up the call stack.1 y) `1 @3 K" l( {, `8 |

    / b: q, \. i; U- o9 R7 _    These returned values are combined to produce the final result.9 p4 N& u! [; w' E7 S2 f& k, m

    9 g4 ~  I6 e) F6 T7 oFor factorial(5):
    / c. ]# `& y0 e6 P! A# C: D  D* A5 }% s

    " n/ h2 i% v0 F' w# ufactorial(5) = 5 * factorial(4)
    9 `# R2 d  r- D9 u0 N2 z5 Q: n1 @factorial(4) = 4 * factorial(3)/ Q7 z4 B4 V' Z& r2 h: w8 [
    factorial(3) = 3 * factorial(2)2 f' u3 j8 H' S0 w. v2 q( |0 l
    factorial(2) = 2 * factorial(1), g6 H  F' d" w% a8 d4 A. {% U1 `
    factorial(1) = 1 * factorial(0)
    2 Q/ x* [! g. s$ Bfactorial(0) = 1  # Base case
    2 u3 S$ B0 a9 Z1 `1 T1 y
    6 t, T# a7 R) X- }8 L7 N& G1 qThen, the results are combined:
    * ~9 X* D# ^9 _3 ^9 Q. Z- c4 _/ [# i; ~# d* n* I  U: y
    ; b9 i8 o* o6 W! c1 ^
    factorial(1) = 1 * 1 = 1, E' }  X4 g9 j" h" T: k
    factorial(2) = 2 * 1 = 2
    $ p+ A! |- p0 ]( N2 X- h6 \factorial(3) = 3 * 2 = 6
    8 p4 C: D8 k7 L2 o3 P  ?factorial(4) = 4 * 6 = 24" g- i3 F  {% j* T1 h6 I
    factorial(5) = 5 * 24 = 120
    6 f6 @7 M# }+ ]- {/ j0 p& I+ ?6 T5 i5 X( O  q; |, @0 G' n  p
    Advantages of Recursion
    3 R* q8 s' a7 q
    8 r+ L, A6 Z7 v! w) t    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).; k5 w0 U8 o8 i

    " Q. `0 F. T+ b5 F, C4 l: X    Readability: Recursive code can be more readable and concise compared to iterative solutions.* G2 q9 z0 A, b! t3 b! p
    $ O3 w- z6 G- h$ i  h
    Disadvantages of Recursion
    ' P# H& S" |( `+ {4 M, y
    ) }( ]( P+ l+ _- P    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.
    $ A% I& @6 [5 ]" K) |! g
    " @; w  ~4 O# D    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    8 \7 e/ F1 E6 ~5 H" T8 z2 a& `( A8 K0 J0 r( B4 N
    When to Use Recursion
    " x9 A9 j$ L* r+ d; c
    4 M3 t; r. W2 F8 U4 {) v) {( [+ J    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % D. B2 B% D2 E' c. Z' [% I. L& I" A* z! F; [. U
        Problems with a clear base case and recursive case.
    4 p. t. B2 \0 a7 H$ [4 W! j$ e" V# s) _& Y, Y! D. D. i# Y1 T, v
    Example: Fibonacci Sequence; M  ^5 f" v! _4 l& U

    ' }5 ~) _$ k  jThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    . c% U% I2 Z+ x; f, p. I  {+ B, ?' L6 R$ a3 L
        Base case: fib(0) = 0, fib(1) = 1
    6 ~2 U  |! h6 U' t+ Y3 w. b9 N: n; S# P8 w9 Q: X# A
        Recursive case: fib(n) = fib(n-1) + fib(n-2)2 Z. w0 a) }5 e1 I+ K
    3 R; B7 C: t! b5 E3 X2 D; y9 {4 g' w
    python, Q( a8 t5 q; A; k2 D2 I; c
    : S, {' A8 R6 K9 g- I0 h

    0 l6 {4 \- s7 |' L. o2 Hdef fibonacci(n):
    3 M8 r& |1 ?3 l    # Base cases
    & j, s, W/ @- n  D* C% d7 u    if n == 0:" l0 F2 o4 |+ [: G0 B2 _
            return 0
    1 l+ L0 m8 y8 W( c" q0 A: I' r    elif n == 1:
    $ w$ Q  C3 c3 w  c4 T        return 16 R. w; q; m: t8 W1 Z  c3 m
        # Recursive case: i" f3 m8 K7 ^6 q6 L. o. E3 e
        else:, w$ R9 d% _; d. Q0 F
            return fibonacci(n - 1) + fibonacci(n - 2)8 Z) }! r2 W: y: T! r

    " g: r. b+ ?/ j) a9 H6 d# Example usage
    5 k' d# z3 k8 g; B% J$ B& J# w( ?print(fibonacci(6))  # Output: 84 r( k" W" d# H0 F6 [

    # `$ ^* S7 k# ~( aTail Recursion  E# l* |& P4 d3 M4 j8 f
    # q) ?( S; f- V. d' a9 j( 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).
    2 @# a% O! q; ?* w- W' p, W# H$ D- c( m+ s  [5 s3 l8 |( C
    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-5-17 11:20 , Processed in 0.058992 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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