设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % j" V0 J. m: C  ~

    1 o0 v2 y2 {& F4 [解释的不错" n) b4 R9 N$ r4 V: C8 I% E
    & _; k$ }. `; n. K. I4 P  d
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。) h1 o) Y) K# x' C5 G2 K

      w! f% Q  i1 K" q% j% r 关键要素7 C+ `% M8 ]( T' o+ |
    1. **基线条件(Base Case)**
    3 ]3 x1 _" h- n+ X% [/ r; M- J   - 递归终止的条件,防止无限循环
      ?' T* c' t3 m/ N3 \8 }& z$ g   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 15 _/ u# F; t9 j* v  E

    9 ^, }  z' u9 G' l2. **递归条件(Recursive Case)**6 L9 w! l9 C2 y8 H# {
       - 将原问题分解为更小的子问题
    5 }2 ^; e' ]+ p1 h1 ]% x0 D8 n. m   - 例如:n! = n × (n-1)!
    " n" P' t: q$ Q# i5 j
    3 c4 h, Q, c+ `0 O) H; F* c 经典示例:计算阶乘. ~$ U) a& u3 Q$ ?
    python
    4 Q; x# i" a- [4 L/ {( Fdef factorial(n):. F8 D; I! D8 w& x$ ^
        if n == 0:        # 基线条件
    ) Q  C) X# X7 O' G  h# E) ~        return 13 ^+ W% S" u0 h" y3 ~2 o5 z% M2 _
        else:             # 递归条件& l$ d) {+ ^- Q; C7 c% A) o  i4 @, I
            return n * factorial(n-1)0 H& ^2 i( W9 F  q0 q! _* M
    执行过程(以计算 3! 为例):$ N$ h; ^3 d. Z$ |
    factorial(3)
    4 \8 G. k  X3 R: C# ^4 _$ B3 L3 * factorial(2). H4 g: V* \% v! o
    3 * (2 * factorial(1))9 s# r% L+ i  M
    3 * (2 * (1 * factorial(0)))& F* `  d+ ?, n; a7 O( u
    3 * (2 * (1 * 1)) = 6
    ( o5 M1 @. s, s0 |5 ]6 y6 m" u5 ^# Y7 s4 z' }
    递归思维要点
    % r; E7 l* e' h/ D$ g. y* o1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    + \9 J# w/ T: v* Q2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    + C8 b6 H6 o9 |0 y" R3. **递推过程**:不断向下分解问题(递)
    9 \9 \9 J/ b0 ~+ J/ g" K0 b5 w4. **回溯过程**:组合子问题结果返回(归)
    * s2 b" E/ [. o1 P& c) T/ X" _( ]
    9 j' p5 Z) l( T% q 注意事项. B+ s& V3 {( U. G( b8 `; S
    必须要有终止条件/ s+ h6 d, k! K3 \# u
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)4 \8 P  a+ E9 a) M, @. p% H- k
    某些问题用递归更直观(如树遍历),但效率可能不如迭代8 v. s0 k6 U+ _+ I6 ]. \! Q9 Z
    尾递归优化可以提升效率(但Python不支持)* Q7 S) \% B! g+ i5 D1 l

    : u) e2 F9 L( w* Q 递归 vs 迭代
    8 h2 Y+ w$ [6 d- o2 n6 B% V' T|          | 递归                          | 迭代               |
    4 w& Q8 f6 C9 L: B8 T; P|----------|-----------------------------|------------------|/ \, P# D$ ?. g5 P
    | 实现方式    | 函数自调用                        | 循环结构            |
    - J; O- c( C) W. r+ X, s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, A- E( O' Y& i; M: u
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |8 z5 e+ R4 Z+ N% |% A: X
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    * o; \5 _; _" k+ @5 a) ]; r7 N; [" O
    经典递归应用场景
    , n2 N0 O) O4 P: l# [: G! b1 z1. 文件系统遍历(目录树结构)
    * ^4 f$ w- |! X( ~2. 快速排序/归并排序算法9 q. _  k* i! m8 I6 y
    3. 汉诺塔问题% D3 P- U3 Y! r8 R% h% U" S. J4 G
    4. 二叉树遍历(前序/中序/后序)% u" I+ F; z2 B& |# f' E
    5. 生成所有可能的组合(回溯算法)8 l& v# @9 Y( H; z5 {+ S7 `4 D
    & Q, y. I- m; D0 v
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    3 天前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( H, t, s/ U0 E6 }我推理机的核心算法应该是二叉树遍历的变种。
    ' z+ s# \7 G7 J- A4 T. 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:, _* x. W" q! {4 T! T- ~3 v5 J
    Key Idea of Recursion
    + w1 r: T5 F( a4 x
    7 k7 f: h8 ^* W0 g! ~! u4 AA recursive function solves a problem by:
    " C2 ~7 b  c+ K. M. I. D5 ^: j6 a
    - l# n/ s; i. L4 }/ B/ z/ \    Breaking the problem into smaller instances of the same problem.
    5 ^/ y, y' @' L2 J
    . v5 h5 m. ]* U& C    Solving the smallest instance directly (base case).
    6 E5 m+ i3 W1 w$ O: U# o
    0 o+ B5 _) W' P3 R* h) Q4 J% t    Combining the results of smaller instances to solve the larger problem./ ?( Y* x. i0 u
    , i3 b6 |7 d% _4 U1 K+ h
    Components of a Recursive Function; M. N, s! p5 b" l
    & V% S& \! ^3 ?6 r2 @: ^
        Base Case:& A) U% }6 K# o) ~

    7 g& P+ V+ t% E# v& k) [        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 t" S2 Q; [% d

    ' t: g* M9 b8 F; \8 v- w/ V2 K; Q1 ?        It acts as the stopping condition to prevent infinite recursion.) ^& z7 _5 P1 _% ^, W. c7 E5 G4 g5 n

    # s4 c7 u5 X0 r- f: _1 ^6 @' v        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.- o4 D8 j8 D; e2 {7 v9 x* \- {9 r

    1 w4 N8 Z. I4 B5 {( l    Recursive Case:
    ( b7 u3 d& M6 s( o$ y
    7 ~. I* J& P  J: j' V1 [1 }        This is where the function calls itself with a smaller or simpler version of the problem.; i/ u5 t0 ]6 ^5 |) k

    ' x1 l* I6 a* [. V% k" F        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    . a+ c8 o2 ^! W9 Y1 M  C( T
    / c2 s7 m) x# D# HExample: Factorial Calculation
    : s8 a& Z4 H0 y# m( Z, o) U4 Z8 l4 |
    ( P$ N4 Z: l+ |! C6 ~0 kThe 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:5 W3 f: m- U/ _' `: Z( {2 y

    . a& j9 {* D8 d/ \6 Y3 }0 K) l$ O% ?    Base case: 0! = 1
    0 W5 F8 ~! a. D' z. m$ h
    - b. Z6 j1 y* z    Recursive case: n! = n * (n-1)!
    3 D) y5 g! I: G
    2 j5 f! i: U& IHere’s how it looks in code (Python):
    : U+ h2 ]9 K/ V) }6 |python
    2 U2 i: X8 ~) n- D
    * ?) c8 K6 g6 a! F; P7 V% I5 K
    $ W0 S4 w! o- k2 l! N/ Ndef factorial(n):- ^) S( {6 H9 I& x: \6 ~
        # Base case
      a& j( _/ X/ u: Q9 H% U" q+ k0 V    if n == 0:
    1 e) Y: P: K6 N        return 1: ?5 l( H# ]4 ]' R0 o' ^+ m
        # Recursive case
    4 i+ Y) L% t' V! o1 T8 [    else:1 A; r& B  x; S6 L+ L) k# B1 U! t
            return n * factorial(n - 1)
    + J0 u  c; g, [3 P
    8 F. u5 ]! T- N/ V% h  o# Example usage
    6 m4 d9 {2 h; hprint(factorial(5))  # Output: 120& S6 O4 U/ C3 W
    & o3 ~$ a( [; {, D- n
    How Recursion Works
    ; v$ F3 T$ l1 A, G
    & ~/ \" r# F; P" b    The function keeps calling itself with smaller inputs until it reaches the base case.2 I4 c2 O' H0 M  S7 m

    9 E/ R9 g( X# e' _$ R7 p) M    Once the base case is reached, the function starts returning values back up the call stack.
    & L0 d; T. s" G$ j/ f" F
    : @" R; {8 [  d) W    These returned values are combined to produce the final result.
    + o1 I4 J1 @: o$ ^" \
    ( L: D; t! G/ w: {For factorial(5):. S0 u, h# l, a9 G' v6 M  |9 N

    / T! b/ O+ D: e& k& b
    4 c5 V) F$ G) r5 \# A& ^' `" H( afactorial(5) = 5 * factorial(4)
    " a: ?9 m% F: Q3 lfactorial(4) = 4 * factorial(3)
    / ^. `( t, _' Yfactorial(3) = 3 * factorial(2)+ [, l/ P  p+ S2 B& b1 M2 H! f
    factorial(2) = 2 * factorial(1)
    ) Q$ P" Z) b4 a" k, Xfactorial(1) = 1 * factorial(0)
    : E: F" \; V  |8 l. D4 Mfactorial(0) = 1  # Base case
    # f" ]- b9 l3 w; x$ ~' B# N
    4 `5 s  i) d' t7 I  mThen, the results are combined:& \9 R& X9 c$ y2 l, m
      k/ H5 _. ~5 O* g9 X% A

    8 n. n5 ^5 ~2 [5 T5 @) l* t" vfactorial(1) = 1 * 1 = 1
    7 {: I' Z. ~$ [factorial(2) = 2 * 1 = 2
    * w9 I( m: s" y' x  M& N( L5 Cfactorial(3) = 3 * 2 = 6  r7 q3 [' c6 G) ~8 Y' b7 G- _5 c: B
    factorial(4) = 4 * 6 = 24) N1 g" \  Y& b2 c. R# e2 ]
    factorial(5) = 5 * 24 = 120
    . @5 y! G6 c3 Q5 G1 W4 P4 d: K
      P8 S0 V+ A' d) F, _8 m# {7 O) NAdvantages of Recursion8 t# H' d+ N$ C
    3 \& W4 P4 }! r/ W
        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).
    7 Z/ X6 I" ~3 y0 w% f% l: K
    8 @6 H- v& _8 ]5 ^, f/ A- S    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * L* j0 V$ S+ D7 v2 n5 H
    1 V+ Q4 L, o6 [% l: ?; oDisadvantages of Recursion
    * [2 ?1 r0 r1 j1 z8 R) r
    , Y8 w; C0 b3 Z' j: I& N" L    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.
    8 J8 r+ g* U( e  W& Q6 s6 ^% x$ w' P$ A4 h
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + v( o: z9 c# Y  |- p, u9 P  K) {- d5 g1 A9 ^4 _+ X
    When to Use Recursion
    ( S, s6 i4 r$ ^) O, g8 k- t+ R& ]
    6 j7 C& i: I* D/ s) i; }    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    * m4 C# g! M2 ^7 r8 V" t6 z- u
    7 d; ]" j- o* U2 l; v    Problems with a clear base case and recursive case.
    9 }  }% W; `; r, C2 O! F3 l! M: q# E6 E# \8 ]
    Example: Fibonacci Sequence
    1 V% k0 }1 K0 S
    2 E# z% K4 z+ ?: WThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    " I8 f" l# w. y) }# h" O! s
    4 G. C3 `5 m- Z7 s' W: |    Base case: fib(0) = 0, fib(1) = 1
    7 \" y. ?* k4 v9 u: |* p6 ?, r# D* x# X
        Recursive case: fib(n) = fib(n-1) + fib(n-2)& W2 M2 A4 `- [( P6 c; O2 d! }

    6 C6 Z  z0 q2 ^python
    ' P0 ^) v" d7 H7 A7 X  t% R
    / i( Q% c2 n% P7 R' ~, y7 V6 b+ z
    2 s4 d* S+ ~2 d6 }def fibonacci(n):5 f, _& y! X& l: k
        # Base cases
    * k0 ?7 x0 v0 c, D5 }: g    if n == 0:
    ) B: `3 h& L8 }4 m: V& @        return 0' k0 X* J( A4 o; y! f
        elif n == 1:, r3 `1 {) q2 h+ l- h
            return 1
    $ S5 w- \; k6 t6 @* _3 n    # Recursive case
    8 n7 i" _$ ^  Y+ B) c    else:2 ^7 _( t# p0 K. R
            return fibonacci(n - 1) + fibonacci(n - 2)
    ' ^- Y  g" t6 _  k) ]) i  m  @7 o* ?0 S' T- n( ^
    # Example usage: u6 A6 A/ }1 \. x' a8 M0 l
    print(fibonacci(6))  # Output: 85 F5 y  }  B- U/ g: l. U5 ?, p3 @

    ( @; b' i0 ~* F! }: K* ?7 }5 _; cTail Recursion/ Q6 @9 d; b3 A( g

    / Q0 D: X& g# k" j' _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).% c& Q) s) X: ]% u( f% f

    " U  Q- _. @4 A& z+ g1 jIn 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-26 11:51 , Processed in 0.060777 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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