设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 5 t8 ^4 v) B1 h" `4 F( @

    ( j$ W5 ?  Z4 b6 I* C解释的不错
    , v$ ^7 L7 _2 n! M0 G; l* r
    $ t$ K, u3 \9 h递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ' \# j+ f. s" n5 m; J
    + i. [' P+ k3 | 关键要素- Q( u$ Z- F( C/ N3 a; M
    1. **基线条件(Base Case)**
    ; D$ z$ C5 X* Q( G; |' _' r6 |6 D   - 递归终止的条件,防止无限循环+ K( \3 ^6 i9 u( H. O& s+ |$ }
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 Q' Q, X0 C# {" P& q
    ( d; }7 Y5 `' w; {& r
    2. **递归条件(Recursive Case)**  Z  d) C$ o5 z/ q* R
       - 将原问题分解为更小的子问题
    $ p7 J0 s5 D7 a   - 例如:n! = n × (n-1)!
    ' j2 f: v& {' @  l$ t( e0 B4 X
    : `+ x- n: }2 f6 I( f 经典示例:计算阶乘, i; B5 d$ A: k/ R0 G( [1 F
    python
    5 K, M& }% Q3 }def factorial(n):
    & r# G( }' {" a& a- o- Q( R    if n == 0:        # 基线条件% x4 \9 }  x) r- b4 [
            return 1* F5 r$ q; m. u- E0 o  e/ Q! Z
        else:             # 递归条件7 P: ]! P0 T* A
            return n * factorial(n-1)
    ; y% X/ s* {/ W1 e5 ?: J执行过程(以计算 3! 为例):
    - _( V/ ?/ I8 u% W3 L" }7 Dfactorial(3)9 Q/ N, p) R' b
    3 * factorial(2)
    # h" i, W) n  Q" ]4 w9 U, G( Z3 * (2 * factorial(1))
    ( h+ T2 T6 u, B1 i( K/ U# r; O! A3 * (2 * (1 * factorial(0)))) w$ H3 [$ y3 D- _. `8 W
    3 * (2 * (1 * 1)) = 6
    6 ^8 O$ v6 K7 ^" v
    8 s/ o- ~$ B- G/ j; G% n: c 递归思维要点
    2 v8 o& z: V. v# |8 ~( o1. **信任递归**:假设子问题已经解决,专注当前层逻辑, o* K  ^' H7 U! W2 G8 v
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)( k) g  E8 E3 T. ?1 @# o# s
    3. **递推过程**:不断向下分解问题(递)
    ! N) ?7 T/ R& s! B- M+ a' j  A- k4. **回溯过程**:组合子问题结果返回(归)
    9 l+ C4 ~3 p- ^
    $ R( n7 J! \- z 注意事项' h2 e3 M; E1 u, l, |
    必须要有终止条件
    7 w& l# S5 X, a% @递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ `0 W* l$ c$ t' n! }% u* D
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 N+ D' s$ `! I% K尾递归优化可以提升效率(但Python不支持)' ~9 j" G; f: ]% h% o- t, ^; @: J
    ' Z0 {0 \6 `, ~0 \6 [5 M
    递归 vs 迭代
    ( N) O$ h% F. C) a6 u. g' f|          | 递归                          | 迭代               |' n$ l$ S  o8 E6 L
    |----------|-----------------------------|------------------|) b" L, N$ [, j. s& i# Q
    | 实现方式    | 函数自调用                        | 循环结构            |
    # I# A* L; Z/ D8 U| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |3 q+ G5 f7 [- C3 D6 z& N4 Q: h
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 e2 y& ~. c, L: L7 h0 {$ \| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" \- H, @4 w6 X2 M. q
    / A" {" D$ Y* _8 T  ]( I* C) o+ C
    经典递归应用场景
    ! f2 H1 v. |* t2 ^& u/ y1. 文件系统遍历(目录树结构)& c! h4 j, i% h+ e6 |4 m: A
    2. 快速排序/归并排序算法
    % X9 k2 ?4 |, m+ `" t. u3. 汉诺塔问题
      L2 Z) c" ]6 f& f! L4. 二叉树遍历(前序/中序/后序)
    3 z, E2 H+ K9 i7 Y$ t5. 生成所有可能的组合(回溯算法)' ^1 Q6 m* ]& W( x. C

    ; I7 K! a1 @( W+ T$ A( B  ]4 X试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 12:37
  • 签到天数: 3235 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    . E  d& Q  k, i. t% u我推理机的核心算法应该是二叉树遍历的变种。+ E8 g4 s* Z/ h
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    & G- Q% o7 @, OKey Idea of Recursion
    7 p' I0 _% v0 A. c# Z5 i7 r; X2 x) U' V! L  N! p5 {- ?
    A recursive function solves a problem by:
    8 |+ J, w- C; I: T& V' j' w8 r: C, B
        Breaking the problem into smaller instances of the same problem.
    4 R# _: G/ [( z) v$ Y7 c$ Y9 n: @
        Solving the smallest instance directly (base case).1 ^* @2 w* S4 v( p
    ! m" P( i* |9 I: U* c/ [
        Combining the results of smaller instances to solve the larger problem.
    6 t( t- d( w6 i: j+ `  J+ W* T7 }+ i9 D
    Components of a Recursive Function, B& U! U( }7 y) T
    7 j* O$ Y! Y: E; ]
        Base Case:6 c' s  o8 U' `, F, O: m7 p

    % o. G' g2 ?/ C        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 y; `! O" U; f' q

    7 ~, N. f' k* I( R, b6 A        It acts as the stopping condition to prevent infinite recursion.
    ! K4 p! F: H; x
    6 b& o4 G3 o& k+ [8 R        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( a$ _; D' G/ P/ F# b- _

    . {2 B. q6 w4 T  C    Recursive Case:
    ) C; W4 [! z0 x( ~' @" v  d" e1 b. N7 i4 v6 Q( u0 H) u: U) x+ N
            This is where the function calls itself with a smaller or simpler version of the problem.. L* F- F; ^- a1 v; v; U
    ' |4 m4 j6 @# d- U
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).$ j. T! i% T+ A) g9 v
      Q* O/ Z4 q, L  y+ e# i) U
    Example: Factorial Calculation
    4 m+ v! b$ H% T9 B4 V( w; L
    1 s1 N4 c" F; L; [- lThe 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:
    / U5 G% `- v( }1 p( d6 A" |$ C( r" c2 c" f
        Base case: 0! = 1/ E. o" c) |9 y

    6 X# n+ b4 {% W1 H# l6 V' D    Recursive case: n! = n * (n-1)!, D" ]9 W' ~1 u) n

    # H  u. u9 x: w: K/ S7 ?Here’s how it looks in code (Python):  W8 h! B9 z0 i/ N! y' m
    python& Z9 v% K5 \3 ~7 W" z8 W

    * C2 i- q8 a) p7 k$ I- m
    ) v1 f' l) q, c, R0 R2 wdef factorial(n):
    ) Y1 E, h: Y5 |1 Q    # Base case# j: y6 ~8 [6 Q, k( P
        if n == 0:1 p# |% I% t( [. D
            return 1) l4 ~/ ~) n# e  l
        # Recursive case) N* E- k8 v$ O+ c, d
        else:
    - C' U7 P4 Z* X0 M2 L        return n * factorial(n - 1)2 a$ i) i0 k- _0 R: o0 |
    ; J# t* e2 s; D& y
    # Example usage
    1 e6 P0 C. N, J) l$ [6 gprint(factorial(5))  # Output: 120( P- w( E9 Y( V8 G
    * B5 \9 ^! b+ r. W$ A
    How Recursion Works
    1 z) e, j0 k/ z+ G5 ?& d
    5 i6 {$ f; b5 X* z: N  T" A    The function keeps calling itself with smaller inputs until it reaches the base case.
    + s* W) p! T! ?2 Z: F% y. ?3 t
    7 m) {9 D/ O+ |/ Q    Once the base case is reached, the function starts returning values back up the call stack.
    8 K  {4 A! A( P+ ?
    ) q, y2 `! |) j, Y7 p    These returned values are combined to produce the final result.6 N3 f3 Z5 L$ e# G

    9 B$ y  d: e: q& R4 Q$ OFor factorial(5):) Z5 n* V' y+ F+ a' {# V; s- j
    ) p' M* b$ A. A: U! {! f# C

    6 c- J5 Q7 I* i8 Afactorial(5) = 5 * factorial(4)( ]4 ~2 Y  S: _# M( F1 [) l* T
    factorial(4) = 4 * factorial(3)& ^2 T4 @. U# J; s. G% X! B
    factorial(3) = 3 * factorial(2)
    2 g" ]2 V- M1 U. F# ?- ~3 c  Xfactorial(2) = 2 * factorial(1)8 P' V0 a4 R) z# g6 `( @0 `1 `
    factorial(1) = 1 * factorial(0)6 A$ v; J" E" |% P$ n  p0 m: g
    factorial(0) = 1  # Base case
    $ n, B" |. Q2 P7 {7 V6 n* l1 z% F1 r/ d3 S7 |" i  E+ i
    Then, the results are combined:0 {+ x- L! T  \; p9 ~; s/ ?

      m) I) L6 V/ k; t; W- c! [& i  ?5 o& {: _9 j0 Q, L: @
    factorial(1) = 1 * 1 = 1
    * u1 L( ^% a, Y3 }6 Gfactorial(2) = 2 * 1 = 21 _3 v, f# N7 U3 a" X, b; _5 q5 h' h
    factorial(3) = 3 * 2 = 66 W" Z2 k6 }- `# G0 k+ J: q) s
    factorial(4) = 4 * 6 = 242 K6 S) I, W4 x% v
    factorial(5) = 5 * 24 = 120# B1 z+ i8 a; b% A  U, ?

    8 {+ X2 G+ Y. t+ K7 u$ b, AAdvantages of Recursion' u# e+ [" L0 s; r% j5 g7 e

    / ~9 X% G2 |% T/ n9 R6 L9 o4 X    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).
    & G  A& R6 G7 R* B8 o3 I+ K0 [* S
        Readability: Recursive code can be more readable and concise compared to iterative solutions.( t  m+ r, V) C) O' W3 C& R
    , k8 L& R# V& `9 r
    Disadvantages of Recursion8 p7 z& l( [" t- N2 z; l

    ( s+ |+ Q% w( D) h8 P0 G$ H1 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.# H, n3 n% d. V5 j3 a# j2 ?
    + z! |" R, X( ?  M# n
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    7 R" R2 o$ l& a
    ' O7 }. O3 L3 x! a# \7 jWhen to Use Recursion
    % X8 T5 M# Q: L9 D, o' n& C5 f+ S
    2 l! t/ T3 p5 c( A+ w3 ~2 s! k+ o9 R    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- }# _- ]/ L- t! k/ x. b

    % W: c) P6 |5 L  ]( c    Problems with a clear base case and recursive case.9 w+ N- h, `9 u  M3 U/ |0 D# @( ~& \4 A

    ! I8 ^! o5 q2 x( e. O) `Example: Fibonacci Sequence! G/ ]: t4 f3 k6 Q8 h! C

    " s( {& @. S" \, H# K- x+ S5 EThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    / ?3 V1 D7 H; e' X% g( Y3 J- J2 h. ?/ y
        Base case: fib(0) = 0, fib(1) = 1
    3 Y  e  T- B  ^) F8 @% n4 V; _+ B5 ^4 T/ U( q0 S5 C' a# o3 b
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' b% a# ?9 o; G$ k0 A8 g! [- z+ D& N% B7 g; A1 S' L
    python8 [; s, f7 T, g: ^3 y  A% p2 j% B. o, t

    , I" x0 _4 ^* D" ?2 X/ |9 i* H; v1 b' S/ R
    def fibonacci(n):# v4 L( v- f+ _+ ^; U
        # Base cases) f8 E0 P$ |0 P1 L6 z6 S& Z) _
        if n == 0:. \$ \* k& g' T2 ?3 }. Z8 Q# Z
            return 0: j$ z$ f, ^6 d
        elif n == 1:4 n! x/ y  X. _, {" f+ Q
            return 1
    5 m; K7 f: d  a6 _% R    # Recursive case' A+ c/ G0 S& P
        else:, C$ h' \: U6 }- V& _; M: o
            return fibonacci(n - 1) + fibonacci(n - 2)5 }, Y. y9 [, u& f
    / w& j/ ^3 d+ ]# u
    # Example usage
    0 G2 v6 ^; T& k1 x$ V0 j; m; R2 \0 |0 Eprint(fibonacci(6))  # Output: 8
    . u" u3 Q" i, Z
    6 B: z8 w3 O5 P: k! S7 |) vTail Recursion8 K: D) I" M, i5 a3 c) A) H+ H" _$ p

    3 O! `, z8 a9 xTail 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).
    ) r0 G/ c. b0 \# b0 Q
    * i7 V( K2 N0 w0 vIn 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-10 04:27 , Processed in 0.062073 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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