设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    + C' ^- ~8 O1 P1 q0 s+ Y5 `1 Z4 ]$ K/ W3 u9 U$ h! }7 N
    解释的不错
    1 J+ C( v/ X6 c9 x; ~2 m' J
    2 w# @- s* G$ w" X8 @8 [, L% L递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。" H8 R! g9 I, G' Z
    6 [$ d$ {+ o9 V
    关键要素
    ; G" W* o; Q& q1. **基线条件(Base Case)**. U9 }2 [7 ]. C! Q$ _2 ?2 G
       - 递归终止的条件,防止无限循环' o! T; @1 X3 e$ z  w
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' z0 Y6 K; D% E) }) y  \
    0 Y% x) Z! q2 q
    2. **递归条件(Recursive Case)**
    . L8 b  x: F7 f8 w8 E" {1 B   - 将原问题分解为更小的子问题
    " ~6 e- Y1 P( a   - 例如:n! = n × (n-1)!
    6 F; [* c, `; f1 K" w+ M7 h5 m" w8 u* o1 t+ _/ C# h! Y  a
    经典示例:计算阶乘
    8 {1 {6 C4 q; ?: [+ Xpython- [' m( Y& @: I- r* Q9 F
    def factorial(n):
    $ n: M/ k( w3 c+ i2 U& k: d    if n == 0:        # 基线条件2 B" t( `. i0 {5 ?8 o8 M/ e* o& `
            return 1/ d8 {$ p/ {+ J+ |: |
        else:             # 递归条件" n1 w# U9 ~' H' v
            return n * factorial(n-1)
    & h- v4 P4 ^. A6 Z; {执行过程(以计算 3! 为例):
    6 v3 N3 `" F) a- g9 ifactorial(3), U6 M6 [/ S5 {# y1 Y" g4 d
    3 * factorial(2)
      l1 r0 h8 B( D; E4 `3 * (2 * factorial(1)); E2 A2 u2 m" R2 P1 e7 V( g8 d0 l* T* R
    3 * (2 * (1 * factorial(0)))
    - i; k& F& C8 i+ ^9 ~3 ~- ^1 P3 * (2 * (1 * 1)) = 6, i/ D1 ~1 I, r, d+ F

    ' M- ?, V. [1 L: b8 ? 递归思维要点" k1 [# t( Q# D' P& B
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑! u! D: Z* z2 K; ]' x
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)5 Q4 ~& V. f) G- U2 q5 A) i5 I
    3. **递推过程**:不断向下分解问题(递)) C5 J7 _% t" ^/ g. U
    4. **回溯过程**:组合子问题结果返回(归)
    : \3 N- `: j+ _2 r. O2 x1 S# @8 ^5 f4 }7 s
    注意事项% H( Z0 m2 i/ f
    必须要有终止条件
    ' _; y: t6 y+ z8 f- G' M/ |* {递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( C& e& Y9 u1 U2 p, v$ t' ~# c某些问题用递归更直观(如树遍历),但效率可能不如迭代. }0 B# U2 a; ]. q" ~. A
    尾递归优化可以提升效率(但Python不支持)
    ' D0 |2 j  n- m' Q6 ~3 q7 M. w5 L3 p3 r9 G
    递归 vs 迭代
    . @0 \! V& U2 {6 f8 a! m) F|          | 递归                          | 迭代               |
      \/ k4 v7 Y& o; W! B|----------|-----------------------------|------------------|
    # N  n& T( b( {7 F  {$ w| 实现方式    | 函数自调用                        | 循环结构            |
    " C' L, l% b/ A7 J! L7 a| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |' B6 w# Z2 ?+ S0 {6 r6 }
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% }# D, J$ t) t* D
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) m% c3 \8 m' T6 b4 ^% [( Y3 v# h

    7 N0 U9 o7 Z0 p1 f. B 经典递归应用场景
    - y7 z, c! l  b9 B1. 文件系统遍历(目录树结构)9 M: i( @! T: u- b
    2. 快速排序/归并排序算法3 f( r" T# o3 d+ W5 c$ n
    3. 汉诺塔问题
    " s8 y: u3 C, r; n& N+ T9 C6 S; H4. 二叉树遍历(前序/中序/后序)- q% V  E, z8 g/ |+ u0 S
    5. 生成所有可能的组合(回溯算法)2 e* b% ~5 P! `# C, t7 O
    # U8 l) \) `, H3 J: v
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 05:46
  • 签到天数: 3238 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ _# J+ ?) O# T
    我推理机的核心算法应该是二叉树遍历的变种。) t* D* x$ K" y' |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:/ B0 b. Z+ V% f! z7 ]
    Key Idea of Recursion9 B1 P% F1 y7 |6 O9 u( n

    ' G/ C+ z. ~- ^, ]/ ZA recursive function solves a problem by:2 l9 L4 t8 a/ Z, a3 }. N# b" x
    0 R7 g9 s( ?, k
        Breaking the problem into smaller instances of the same problem., U. M0 h8 _# X

    5 i( b# y- J, R# `    Solving the smallest instance directly (base case).
    0 Q8 U/ |* m3 B6 G4 H. ^" O
    7 N0 D, c4 `' q. f) C- E, C    Combining the results of smaller instances to solve the larger problem.
    # ^$ |3 V9 p( [7 m- k- \' X* s9 J. j% d4 Y4 X7 Z* m0 O. K0 X: V7 e1 a
    Components of a Recursive Function
    " q  J/ [* W& @& w% A: S! t7 z8 Q. }! P$ G
        Base Case:& f  s- `3 V7 S! m# u
    % c( V- ]3 g& n6 ^) o
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 j: H, [0 g6 R  Q/ J
    % \7 B- b7 Z! T, Y7 W        It acts as the stopping condition to prevent infinite recursion.
    - |' ^- v3 U! F% Z+ G0 Z/ X
    ; C/ V2 ?. e' r( a" F        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # Z, B% [" I& }% p: S
    2 Z& S$ x5 w( D    Recursive Case:5 r; F# B  m' o& P$ T6 {0 E
    ! V1 [6 |9 v: F% ^3 F; M' O
            This is where the function calls itself with a smaller or simpler version of the problem.+ r. }$ T4 ?8 n+ r) @! z& D

    / b& M  j" r* m$ [. B0 ~! X' [        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).. T* X. F, e5 y6 T
    & ?$ c% I, K$ a
    Example: Factorial Calculation
    . q/ C$ g/ ~- z# {" K5 R0 u) I  W, U8 p3 m
    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 |" H1 b' F4 b: d& I
    - i1 t0 X* t3 ^    Base case: 0! = 1
    / x3 s5 d/ ^, m7 s# o+ i' E9 [) p' J* |9 _& y6 f  V
        Recursive case: n! = n * (n-1)!
    5 |& `& i% Z' d! Z3 n! c! C& n0 p( Y0 o' m7 r! ?+ M" q% G0 A
    Here’s how it looks in code (Python):
    # l1 L" R- w" rpython
    5 T1 a; \- G0 c/ `  R2 I1 d1 l& C
    ( E8 B+ V2 p& m( R, t
    def factorial(n):
    5 j9 h& ^& f: W5 {- e6 R    # Base case0 n( O5 E6 ~/ _3 X! w
        if n == 0:
    ( N0 c# e' b; y        return 1
    . U. m. d/ x& T, Y) l6 \0 F    # Recursive case
    + }! p) M/ r5 x! X& |    else:3 r% M3 O% h" |& r
            return n * factorial(n - 1)
    9 `  h3 e' L) F7 N
    ; A! e; q' `" T" Z) z6 y) R# Example usage
    ! j' }3 x8 W3 O) i. Q' gprint(factorial(5))  # Output: 120
    & L% A  c4 M; Y; u
    2 M  X2 U9 o; f# jHow Recursion Works
    & |4 @6 ?' x5 A  ?; v; V# c. x5 P) F4 L( E: R" T) f
        The function keeps calling itself with smaller inputs until it reaches the base case.
    7 h/ _1 F# l* {  D7 z6 x7 R8 u8 o4 Q2 V# e5 L, a: \: [6 h. y; o
        Once the base case is reached, the function starts returning values back up the call stack.( f% a7 B7 R3 Y' r

    $ N9 w* @1 c! U# ~    These returned values are combined to produce the final result.
    ' |3 |/ u/ J2 W' @
    $ S! M+ K7 b2 s8 iFor factorial(5):
    * a- L& G( W2 |2 w/ V
    / s5 d6 i+ ^* S2 I5 Y9 L" P( Z7 k% n4 e1 V, i7 ^# \
    factorial(5) = 5 * factorial(4)& @2 r2 Q5 x" h% h! k
    factorial(4) = 4 * factorial(3)$ D; C8 h! y, e; |: t
    factorial(3) = 3 * factorial(2)3 m3 p) O3 P8 @( U5 P" I
    factorial(2) = 2 * factorial(1)2 }! |9 `+ y; W" a
    factorial(1) = 1 * factorial(0)
    1 S# @0 I4 {* g+ u7 Q  ofactorial(0) = 1  # Base case" W4 r0 Z  ^( f5 ^6 e  p8 ^
    - e- T0 e1 c, `
    Then, the results are combined:2 N. {/ Q4 L2 P6 _" v
    ) l( J, E6 f: c1 R; h, ~
    ( X. q) i, I$ d  L! `7 {6 W! m) u8 E
    factorial(1) = 1 * 1 = 1
    % u8 L7 W3 A; M5 ^0 }' J3 v+ N3 U- H2 X" }factorial(2) = 2 * 1 = 25 ^& z* W3 M" [4 F6 Z& D+ \/ ?
    factorial(3) = 3 * 2 = 6
    8 m+ [5 Z8 H8 H! }3 J/ V3 ffactorial(4) = 4 * 6 = 246 e/ p( e- L% J; g+ C' D
    factorial(5) = 5 * 24 = 120( A7 k& c$ c; R8 J( h

    8 M) ~* v/ x& Q; Q, R" UAdvantages of Recursion8 ^7 t/ ]: r, z) I( y: A

    + q6 W' H+ n4 q8 e) G$ b    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).
    ; k9 G9 K6 V0 v/ Q8 d( K  U) i1 k# T& S5 a$ Z1 r0 x
        Readability: Recursive code can be more readable and concise compared to iterative solutions.  n  M6 g& [- G: e

    , E+ ~0 w$ V6 ~+ `# q8 oDisadvantages of Recursion. e/ G8 y; l% F4 g; c

    ; H1 q' e) O) i1 ?# s    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.
    0 [& K/ c" \& r( D5 ~6 Z! k2 f- l
    $ x3 k; ~; m& I  A    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    2 j5 {* s0 l+ ?5 i
    + z* E$ L( }' _  B) |9 K  aWhen to Use Recursion5 {) @$ ~3 ^  U5 j+ C, F

    ) o+ m" P) G& C    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).+ _8 m6 W( X7 c  T4 s0 U" L
    ; W# m& Z3 d; w, K
        Problems with a clear base case and recursive case.
      [5 u4 @* O8 O' q, R0 a5 x
    $ o+ b: f& H. FExample: Fibonacci Sequence
    7 A% j, \& v: q8 @- t- r- M
    2 \. _! @' `- c, k3 r4 v' zThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:, p  @: c: S; D2 E. N" g8 a- I! @
    ( B" w( j! m3 D0 _* z4 R
        Base case: fib(0) = 0, fib(1) = 1
    ' f$ j$ ^' c7 Y* E" C) \! `5 F2 y# j& K* l  G
        Recursive case: fib(n) = fib(n-1) + fib(n-2)& T% \8 Q' [0 p. S; Q
      m* E* z  q0 J8 z
    python
    ; Z/ I8 r# M/ L  ^) _" n0 Z9 |6 g! O9 {  G
    2 f9 x) |( V) ~4 g5 C+ r* |# R
    def fibonacci(n):
    ( K, Y# T: [. t, t5 w% }* \    # Base cases
    # W1 k+ R! A  p( K8 P* o    if n == 0:
    5 Y, l# F2 l; w) g        return 0
    7 [7 N: R8 u. L2 C& Z    elif n == 1:
    6 M# i3 p1 t' E1 l: y        return 15 G0 C9 o* R; A% H
        # Recursive case) d: `! j0 u, o( _3 X% U1 U5 C7 H
        else:/ U9 ~. ^/ A5 }" K3 B
            return fibonacci(n - 1) + fibonacci(n - 2)+ i3 g) }% d, z6 I! b0 }
    3 o: x1 t& F2 \9 C
    # Example usage1 K# m( v6 ~$ h. M
    print(fibonacci(6))  # Output: 80 I. s3 R( U. g( \7 v
    4 S" w6 U* I( y3 b0 Y
    Tail Recursion
    1 Y8 i/ r7 O9 ]8 M8 {. V
    7 z% V! B1 Y( K$ K/ [$ ?! rTail 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).3 X% Q' g6 S2 J& Y# p0 x; o9 ]( o
    0 c! P" v, B! w. x& x
    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-13 05:15 , Processed in 0.058475 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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