设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ) U; f0 k0 z5 c  x

    . Z& S- e& f: Z+ b# Y解释的不错9 w) C9 N+ f6 D) \

    ! ~! {# g, B8 r' M* x' |/ \递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。5 S* _# Z7 u* b& T! g
    $ v/ b# s+ P9 b7 p$ p
    关键要素: C) d7 n8 R" ~! K" S7 a; @
    1. **基线条件(Base Case)**
    * O% x2 B5 ]# y# H4 g. q) ]) Y   - 递归终止的条件,防止无限循环
    ) U+ |$ ?4 C  k5 B! B   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1& e- @1 b" M2 a& O1 u  q% _4 m2 I

    ( o- S5 ~2 D7 D6 y7 b( V' M" L% b2. **递归条件(Recursive Case)**
    3 A; Q3 H* @- H; O$ I   - 将原问题分解为更小的子问题
    : N) K% n7 F+ N   - 例如:n! = n × (n-1)!
    " g0 L; h5 M/ }$ i( t9 v: o( U" q/ _0 x. V' K
    经典示例:计算阶乘
    ' i. A1 G; y8 U* Gpython
    ( d  c3 S/ h. u0 Ldef factorial(n):
    / `5 D$ ?! H3 b; m) b' P  \( L    if n == 0:        # 基线条件( ?6 k# _) o6 o/ y
            return 1$ V. d3 S* }  m0 x, a* V& G
        else:             # 递归条件2 M- ?% }/ G0 k9 ], l( ]  x/ X9 {
            return n * factorial(n-1)
    . Y7 @, o; A# ~! r执行过程(以计算 3! 为例):
    5 M1 w7 c( x, P. m7 Vfactorial(3)
    + u& z% _- b! Y6 n; M/ G3 * factorial(2)
    - B& G9 l" n6 q; U. |3 * (2 * factorial(1))) p7 A$ R. ?/ R$ d1 a  e
    3 * (2 * (1 * factorial(0)))% L: n- [* k3 b# b2 B
    3 * (2 * (1 * 1)) = 6
    , W3 y6 v. m* c  y5 y& t5 }* f: h; @1 i  l0 e- p- O. i
    递归思维要点
    3 {: _& [4 V/ j' c, E5 x: v1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . w- S6 X' l# S$ ]4 O" z4 K2. **栈结构**:每次调用都会创建新的栈帧(内存空间)8 r- x$ b) @) ^8 _. _
    3. **递推过程**:不断向下分解问题(递)$ e1 C3 ?- U7 _. `
    4. **回溯过程**:组合子问题结果返回(归): w5 x; i: D$ n- B' v; k

    8 I0 d! e9 n8 m' r" W1 O4 k 注意事项3 J# A  S& o( x0 B5 \! P# N2 Z; t
    必须要有终止条件* ?) y# E3 l2 Q( H# {3 _1 V" I
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ w1 V! o7 y* Z- r: x, i: D
    某些问题用递归更直观(如树遍历),但效率可能不如迭代+ ]8 w* B, a! A  y: ^* f) ?2 D+ y
    尾递归优化可以提升效率(但Python不支持)' z& J' C( {4 G% w' \( _' A" l

    2 b9 e: R0 q) O- O  H# E, y 递归 vs 迭代
    ' ?9 u. z+ K7 B. [* `|          | 递归                          | 迭代               |9 [: E7 k9 H% a+ ?) O
    |----------|-----------------------------|------------------|$ v  g$ i7 Y) x5 F
    | 实现方式    | 函数自调用                        | 循环结构            |
    " o- P! ?7 q# \! G5 ~! J; V8 ?; C| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- m6 U9 a2 n* b+ _1 l
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ) x. y/ o1 s( D( {| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    , y5 @! B  ?% [: _$ C( w& H# @3 Q( s7 l( i* q8 r0 C
    经典递归应用场景! e, s2 y6 E* C8 x0 z! i8 {  N8 U& b
    1. 文件系统遍历(目录树结构)
    0 t2 |. }: m( a  X- [2 z2. 快速排序/归并排序算法
    % j( @: d7 H) w% I- x9 |" p3 q3 f3. 汉诺塔问题8 E' n3 P/ O6 v
    4. 二叉树遍历(前序/中序/后序)
    % _- d! |" c  u) [8 ?5. 生成所有可能的组合(回溯算法)! S5 `8 Z2 [( X7 Y+ Y' w: v1 E

      a2 K) |, K, I/ ?( a8 K- {3 B试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    10 小时前
  • 签到天数: 3189 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % @. H& B- B8 l% ?8 o7 n4 ]我推理机的核心算法应该是二叉树遍历的变种。/ F$ h; v. \& l9 V5 n$ ?
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:6 x  d% n' y$ N! \% o5 {, G8 _
    Key Idea of Recursion
    / |! C) W, R" A( @4 E
    8 ], y1 Q+ P: l2 S% f3 ]A recursive function solves a problem by:
    , A* e* V# c, j
    8 f6 N" B* K$ c! h/ H& c) l# D- u7 l    Breaking the problem into smaller instances of the same problem.; n+ O) d! F% p/ k* R) H# X2 x

    + t8 O% j1 |, |/ I: M    Solving the smallest instance directly (base case).
    # T* N! K; ?& k! R8 o" Q5 r+ L
    4 t9 {( b- J, I  t0 b    Combining the results of smaller instances to solve the larger problem.
    6 E* U3 n, L5 p1 G8 M; k1 k$ \$ V, n8 K, ~
    Components of a Recursive Function
    - W  N! c8 s+ ]& D. J( m. |
    # ?8 O" @" w& u6 C    Base Case:6 b/ }& ]6 w* s  c% S+ C/ t

    ) K2 Y& Q$ T0 Y. t8 m        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ) D5 w  \7 n. `" P' Q4 f3 u+ {2 y6 @& b0 o1 B% c: m5 r
            It acts as the stopping condition to prevent infinite recursion.
    # B  [3 k8 l) e7 U) a# Y3 N, B
    $ n/ j9 G  G* h        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.* y( f) W8 u( \: Q1 i- T

    $ E' F0 y) O, Q6 V6 S, D  B    Recursive Case:
    ) _3 ~1 B  L( W+ x' ?( K
    & _2 o; ]6 R- e7 a- v9 {8 T        This is where the function calls itself with a smaller or simpler version of the problem.* V: F$ q4 m3 D/ W6 z" A! C
    7 R; |- F0 g! C  W9 k
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).* _* s" O( D& o, R; w. I

    . l4 D6 G9 H% H: {6 X: x. lExample: Factorial Calculation& F* A4 N, I  I+ S2 n  @  @
    0 W! M& G, @0 A, b' Z: ]
    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 L+ {: _% N% F7 a& ]. E$ d

    ' X2 O. y. W- L6 G, M6 M# e    Base case: 0! = 11 D9 e& ~9 ^; `$ t
    # w0 M& H) N# F1 z/ ?8 [7 y- q
        Recursive case: n! = n * (n-1)!) a/ l) R( {- e7 F  i  K0 k" Y
    * F1 Y! L' Y. {0 }( F( e" g
    Here’s how it looks in code (Python):
    ' K1 G9 Q6 a% Zpython
    7 s; v3 E1 U8 a1 s7 ]  U# Q* h) T8 a1 F$ {' V: B  V* W7 Y$ S5 N

    0 ]# R5 m! }+ h* o0 Z- ?! hdef factorial(n):: u) \6 e/ i7 p; y5 g
        # Base case
    4 @) o0 |- y- o; N/ c    if n == 0:
    " m& Q9 u5 |2 O  H3 R" k! o        return 1
    $ B7 m! l2 H8 Z+ `    # Recursive case5 L" M* U! o) C: g2 z! }5 X: v5 L- b
        else:% c, ]  F$ S4 Z# S! h8 o
            return n * factorial(n - 1)3 u( k' B. O2 }  |+ e$ x0 m. Z

      ]3 F' s9 z- I, p# Example usage2 s2 U+ H7 V2 `, ]5 v$ h: V, }
    print(factorial(5))  # Output: 120
    : N# m, Q/ N8 a* j3 K; X' P) r3 O
    6 e! H  s" R) ^' f, Q) ?How Recursion Works* m: p$ Y' r8 L8 }8 P! q

    ' b3 j5 `  w/ h8 D* E    The function keeps calling itself with smaller inputs until it reaches the base case.7 r' Z  J; c% H" a9 l
    + ^7 f8 W* G8 t
        Once the base case is reached, the function starts returning values back up the call stack.
    ( f# n. x9 ~6 s. O
    5 b/ V. K% T& B( J; Z    These returned values are combined to produce the final result.( S8 _  k2 M5 j, d1 V) n

    # n6 Y  D5 Q3 W1 {1 hFor factorial(5):" A7 ^: D0 R! N8 Y

    $ R! c" a: y' C( x0 _+ K( g
    : q, h2 L0 ]$ s5 G. `8 i, nfactorial(5) = 5 * factorial(4)
    # Q& m* Q7 x2 C! S8 ffactorial(4) = 4 * factorial(3)
    / s( T6 |, U: p/ h- Zfactorial(3) = 3 * factorial(2)4 O2 d8 e' m, T! }9 p7 V
    factorial(2) = 2 * factorial(1)
    ' E; ~# F) @0 L7 N, S  D' pfactorial(1) = 1 * factorial(0)
    , S! J5 f3 q5 Z$ B! W3 E  t% o2 efactorial(0) = 1  # Base case& T: {' J( O0 @5 I2 m
    5 y& Q# j8 Q$ h
    Then, the results are combined:9 z4 K8 A% h# O, p2 P2 G4 S# x

    9 p' J3 }9 O3 W# h
    & y: x' G9 d/ e3 k0 l2 m) dfactorial(1) = 1 * 1 = 17 X, g1 [  F2 T
    factorial(2) = 2 * 1 = 2
    ( F! _  [( Q  W% Y2 S& Z) nfactorial(3) = 3 * 2 = 68 |- u0 d, F2 A+ M
    factorial(4) = 4 * 6 = 243 _: M" d) P  ?5 d. m! i
    factorial(5) = 5 * 24 = 120# Z3 I& H2 _" p" o! ^* G

    ) a) `2 [/ R6 O! V. p2 J' sAdvantages of Recursion; \( I5 h, X. l0 q# l1 h/ J; A

    # k4 a+ v3 w7 v. [2 h, {# p    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 n7 ]6 [- [; |0 C) k; J6 H& n/ L- r' D. ?& d. y; h8 q3 Y4 z" K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.# t) Y. c% E7 I4 g+ x

    $ V- n  [; x. CDisadvantages of Recursion8 r7 U$ M. ^" r1 e- h7 l: Z
    ! X5 G5 {* a/ h( ]
        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.5 v. a3 X& o& H1 v+ L0 l6 `

    7 G0 L6 I9 t/ J3 e7 x9 G8 \$ O    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ; ~0 R6 q9 t# _" |  \! A
    % R; @; C( h4 a( gWhen to Use Recursion
    ' o( {( {" Q2 C, {# T) x3 M4 Y- m5 _9 i7 O7 d9 }  C& L8 U/ U
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 d! x* l( J, \. Q( ^/ @5 p( n* o( ?1 c8 T2 P' I
        Problems with a clear base case and recursive case.9 g6 X7 a8 q. w% F+ D5 }
    % s5 q& w7 K0 K. L  r5 N4 R
    Example: Fibonacci Sequence) T+ v6 m) O' n4 Z: n& I
    / y$ P9 ~8 ^2 F- x. b7 D" n
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. Z9 D& y& J( n
    - h' p: \6 m. {* e% F% a9 d
        Base case: fib(0) = 0, fib(1) = 1
    * R: _; E8 u$ j$ m- i4 L2 j3 ]$ }0 X) k1 E7 J
        Recursive case: fib(n) = fib(n-1) + fib(n-2)8 \9 O3 e9 V# W
    " N, x/ e( M( a) j+ U4 N! B  x8 U' c
    python, W+ x/ L- j( V) m8 L
    0 o5 |" ]8 C1 g

    : ^9 R& V- e$ O$ E- Q7 n# fdef fibonacci(n):. y+ x: j. G: R
        # Base cases. n1 [3 Q5 O* E3 m; f# \6 A
        if n == 0:
    ; Z) s( p1 y9 w8 w- o; A  k- I  e5 E& F        return 0
    0 n& u/ `) f1 J- C1 ~# V# n% M    elif n == 1:% ^6 R" M$ e& Q3 f: E5 R0 y
            return 1' f$ e& o2 Z# x8 ~! a  w
        # Recursive case
    + Z* d5 V1 s4 i' _    else:
    % H: L4 S7 P/ i, @- w2 O        return fibonacci(n - 1) + fibonacci(n - 2)2 q( q) C: L. G- I
      F. _' V" m/ Q8 G: d% n
    # Example usage# m' Y" F: a! o$ Y" X: N
    print(fibonacci(6))  # Output: 8; L3 F3 ]8 \) q2 B6 a
    1 Q" }4 U; r5 n8 U! i7 H
    Tail Recursion# U6 w6 w, f, m# v6 T

    ; p+ B; v: c0 LTail 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).
    , h$ {& \+ r# j2 Q! P+ P- T% R
    3 U( C: u' U" C  HIn 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-2 17:31 , Processed in 0.062052 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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