设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ) S) `- U& C6 j* p" y1 Q
    % \9 b5 `4 l: Y' `解释的不错
    6 _7 x: o% P5 d$ `6 [& |0 e: w- b  h/ }' c0 x9 H. b+ h( B( T: N
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, A- P' t+ ~% g; t' i  i

    % o. f0 `+ v8 A8 z4 E$ d2 o 关键要素8 }% A" b- V5 U) {
    1. **基线条件(Base Case)**
    * s# j' J/ Z8 B/ e   - 递归终止的条件,防止无限循环/ c, l+ K! r+ V2 Y% M
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1( @, n  ~1 r8 F7 K

    ; C' @5 s, j0 z! d  J1 c2. **递归条件(Recursive Case)**
    ; P. x% {6 m5 F6 m   - 将原问题分解为更小的子问题- X7 \, \/ S) D) |% I% _- F5 x
       - 例如:n! = n × (n-1)!) C! h- D9 q& C

    1 I: i, \1 O) u, v 经典示例:计算阶乘
    # M* [3 N) X2 _, m3 D! j5 zpython: p; c8 N7 u( i  ^0 Q7 k! w
    def factorial(n):" p. j9 K+ Z, r0 b! r! u
        if n == 0:        # 基线条件, ~/ V/ K" e) n4 Z1 u
            return 1
    $ w9 q3 b& }# C- v& `0 Y% B7 x: Q- m    else:             # 递归条件% p- b  D9 k* H; ]! s
            return n * factorial(n-1)
      a4 m" n) \0 b1 }执行过程(以计算 3! 为例):
    7 U3 [8 |7 A5 u7 d, Ffactorial(3)
    , K1 U; t  Q* }* Y( u/ Z' \7 j3 * factorial(2)/ p- }* u& e# ?
    3 * (2 * factorial(1))
    - _) }& j" Q* u) ~9 w7 z3 * (2 * (1 * factorial(0))): U% x$ h+ i5 ?
    3 * (2 * (1 * 1)) = 6& W9 ]' j9 t9 M/ m! y

    # z: I& k/ G) C 递归思维要点: n: {5 q5 C* G( R
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑; A5 Q* V& ~- C6 L& h) J
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    * r! `  R: D4 g( k7 S; @3. **递推过程**:不断向下分解问题(递); G- n6 ~8 t$ M: J
    4. **回溯过程**:组合子问题结果返回(归)
    2 r  O  C$ I2 d- F
    2 {* L5 |: z! W, w8 c1 z+ w 注意事项
    9 W! l( P9 X, A必须要有终止条件( \& d: f% N; ~4 l$ ?
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) y- N; i( s9 P! C
    某些问题用递归更直观(如树遍历),但效率可能不如迭代* p5 b* W" c: N7 H2 p+ G. J. l
    尾递归优化可以提升效率(但Python不支持)$ e' H8 V4 M) \8 U* V

    + ?0 W3 t' u9 y  u2 O0 y  J0 K 递归 vs 迭代) m' F" M) q( _1 d* d  R$ L
    |          | 递归                          | 迭代               |4 A$ C. i- {2 z, i, B0 q  }
    |----------|-----------------------------|------------------|6 b  D5 f3 s' s
    | 实现方式    | 函数自调用                        | 循环结构            |' T0 p7 h  O. X
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ) Y9 A- b" q6 I/ a& ~! z# @# C, f7 M| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    , N: G' j( N5 J$ i$ X  C7 C| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ Y: D& \8 u' E/ Q# ~' a! v' `! U

    % J& Q1 T4 t6 N: V 经典递归应用场景
    / {4 q( t0 E( b, j9 n1. 文件系统遍历(目录树结构)/ _1 [) B3 ~8 Q) p$ D+ Z
    2. 快速排序/归并排序算法/ P! W8 E$ o5 k' L2 e
    3. 汉诺塔问题( _# N3 V+ d7 X) S2 l4 ]' k
    4. 二叉树遍历(前序/中序/后序)7 S& U- x4 W+ j4 F
    5. 生成所有可能的组合(回溯算法)
    7 k8 X1 e7 x5 x1 ?. w+ @! P) N) i) `5 J5 x! h
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 06:15
  • 签到天数: 3181 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    7 ^  ^, t- N- M  b$ d我推理机的核心算法应该是二叉树遍历的变种。
    8 n* H/ g0 {8 s$ S& a2 d' 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:
    8 a0 ], l1 Q, n6 ]1 U. M) Q  vKey Idea of Recursion  P/ m8 Y- N6 K) @) A2 N

    / X1 j  O' Y! J- Y7 R9 x* U! e  \0 I% lA recursive function solves a problem by:/ ?$ Z% W* ?9 [

    ; ?5 Z: y' x7 `  B- f. T    Breaking the problem into smaller instances of the same problem.
    ( L: M  h0 I! x6 i8 T
    - `- X2 h6 M, h- l. W7 A/ P2 a    Solving the smallest instance directly (base case).: T# J& G4 \( `: q$ j, w

    1 r% I4 O, q0 ]9 J+ ~' ^1 r    Combining the results of smaller instances to solve the larger problem.
    + T- r; Q. B* n8 g" o& _' k! \4 y; C$ d1 F3 g8 [0 S/ F
    Components of a Recursive Function  ~0 Z' R, ^5 M) j

    + X- K& k, a/ w    Base Case:
    ) S, E* ^# F* |: P* Y( w. c3 f1 z. {6 L5 x, O
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& _0 `+ [% g6 j3 s1 r/ Q4 D

    / K: M" p# N- a' }        It acts as the stopping condition to prevent infinite recursion.: w4 n0 r: M! A
    ' M5 ?7 R2 R; F1 }; g
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # p! }. D4 N2 L) {/ V7 P# N9 i* o1 I, q
        Recursive Case:* h& X/ }+ c0 Q

    1 f+ U) w) Q1 c$ q1 {, h        This is where the function calls itself with a smaller or simpler version of the problem.
    : E# Z# p( H7 s4 {9 R  ~) n; B2 m5 W/ L- A% @; l3 a2 @
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' T/ I7 x2 U; s# d  H" F/ E

    # Y7 F# w% c$ `  Z: JExample: Factorial Calculation
    " z0 u, m. t9 k
    / i& ?$ s$ ?& W9 Q5 k0 e  n- tThe 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:( W! e0 r5 ^  x3 E( X
    * _" P% @# ]- j1 E0 D- R8 ^7 j
        Base case: 0! = 1$ E9 ?$ d4 ?3 C
    0 G; ?/ ^' G- W7 G: h5 L. Z! O
        Recursive case: n! = n * (n-1)!
    - r) f* \4 c9 z" X& q0 ]$ H
    # ?+ v& w6 W  n& UHere’s how it looks in code (Python):2 @6 a4 R3 F5 G; k
    python
    " q, R2 O4 Z- O9 n
    5 Q' A/ i" R; g
    2 p* \& p- I  A3 y/ K2 _+ n8 V# Ndef factorial(n):
    $ t0 G+ B( P! w0 \3 V! H    # Base case: y3 ~! @" I& C  l! a
        if n == 0:
    0 K/ V% C3 C3 h# M        return 19 C+ m3 K- F  M" Q- [& g
        # Recursive case
    - A# @- X5 R. X    else:$ w3 P: X3 X3 B
            return n * factorial(n - 1)6 S: M& K, W; G- b. S3 Z
    9 R# r1 l0 d  W% h. d, C2 E) b% x6 K
    # Example usage1 W) p4 @( X9 J/ @
    print(factorial(5))  # Output: 120  d! A* w# j, d' p0 j. w$ ~

    3 @# s$ u9 r: S8 Z) U+ H% BHow Recursion Works
    ; z: s  ?) I" `3 ^5 o6 K! {/ N
    ( L6 i6 ^$ N' X( C" k& v    The function keeps calling itself with smaller inputs until it reaches the base case." ~' s& t$ \8 M3 \% A/ H) u/ p+ i

    . e& m1 H# E0 p# R8 e& S% e: `    Once the base case is reached, the function starts returning values back up the call stack.0 V  i4 Z0 O& D5 K: q) |
    ( }# ?  r! i+ r2 L
        These returned values are combined to produce the final result.
    & K; b' L" t: Q+ T5 r' i+ ]2 W+ I/ a& o1 z6 E
    For factorial(5):
    ( g" P6 T8 _- n5 \) u' J' U6 n# e! G% j; D7 o, l9 C* i7 c

    # f3 w* K: ^2 @7 Z( O4 u# L9 ifactorial(5) = 5 * factorial(4). Y3 H- B4 b( i$ a& [4 h6 E$ b% h, Q: Q5 G
    factorial(4) = 4 * factorial(3)
    ' x& {& W& [7 P% `! Z: p0 z5 Vfactorial(3) = 3 * factorial(2), e; N/ r" H% n* P! Y) q( b2 u1 F6 c+ l
    factorial(2) = 2 * factorial(1)6 V: f) _3 ?# e, Z
    factorial(1) = 1 * factorial(0)
    7 I3 G( k! f! `/ N% R3 l9 y0 `! v( bfactorial(0) = 1  # Base case$ |0 B4 y5 \& c: C8 f$ m( {

    $ j: t5 ~3 n9 bThen, the results are combined:$ T9 y- M7 }3 T! [* j

    " R, Y- }2 {3 j" s  M3 c# g8 b1 p' T/ m: d3 m4 S2 @7 q9 X& |# g
    factorial(1) = 1 * 1 = 10 c, i* H- g( z1 y- q
    factorial(2) = 2 * 1 = 2/ _# |7 O' u1 K/ K. h& A
    factorial(3) = 3 * 2 = 6+ o6 @3 \  l4 C
    factorial(4) = 4 * 6 = 24
    $ K1 \8 Z0 w0 h* E5 z2 Ofactorial(5) = 5 * 24 = 1202 |% i9 O6 q: C

    2 A6 v* I4 N& M4 q  kAdvantages of Recursion* }( T  F% [% X: P. A$ ]

    ' F5 L0 z5 o3 E    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).
    ' }0 a9 h/ v1 o5 _0 q5 z0 T$ C! _
        Readability: Recursive code can be more readable and concise compared to iterative solutions.' o2 u: K/ F! W6 K4 i- _$ g6 ?

    * R# A  _  H$ z5 ]$ cDisadvantages of Recursion
    # i  o  y& r# y' p/ }! P9 \+ v- H& Y
    & l6 Q2 t6 d5 a7 f$ r    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.% B8 u# t) Y" O8 a- [+ c- [
    , h0 f$ N$ z6 @, G
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & S6 c: _4 @+ g6 w+ k2 @+ W7 m: y3 _6 E: K
    When to Use Recursion  v4 W  t3 A0 \1 T( ?! g$ ^  F
    + `6 H6 W0 i; e0 _( M1 \
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) b. _0 t% c( u0 M* Z$ O

    ( E8 j( Y3 l; |0 ~3 [    Problems with a clear base case and recursive case.
    / c, w7 t& Y3 T' ?& J# f. G1 f9 j5 h
    Example: Fibonacci Sequence# Z, {, n7 A$ Z" z
    - w+ t! s9 l" z# X
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 t& X! j* O9 ^/ _6 X3 s: {* y

    2 T4 y6 Z: ]0 {' L7 ~; d    Base case: fib(0) = 0, fib(1) = 1
    ' X0 B& \, M3 z7 [  }, _
    ) D' D) a" _, r% V; N0 l1 _1 C1 N/ Y    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    8 h4 @; Z0 ^, m) h3 Q/ ?; S' p2 `) B0 p8 E* j, L
    python1 _# }0 [( B2 ?$ D

    5 s2 i7 k8 A* u7 ]" w0 @9 ]* {  X+ }2 Q% @, \3 `7 g& I
    def fibonacci(n):
    9 O* _) l  l2 h$ v+ K    # Base cases) V7 ~/ U& S$ ^
        if n == 0:
    ) e1 u3 z4 h$ S        return 0- I" p4 p! t) j( Z6 z
        elif n == 1:
    9 E% |0 p. K8 A( ^7 w        return 1
    4 U$ G& X! J7 \5 O. V    # Recursive case/ v! Y7 v8 m: K
        else:2 T/ K$ C# S" H
            return fibonacci(n - 1) + fibonacci(n - 2)' R3 c2 \; |: |( O: p  e' t+ H! f+ K
    ; Q# {; O6 t. C- x6 C+ g1 `  j  V; O: e! P9 O
    # Example usage/ ~. e: `. b6 S- [9 Z
    print(fibonacci(6))  # Output: 88 C. U# _$ \' F$ u
    7 w% D' K4 y0 H6 j
    Tail Recursion0 q4 H0 g9 Q4 ^0 Q  K. y
    + _. `) S- I/ @+ K) ~0 o
    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).
    6 M7 H# l6 ?, r& [+ X2 O% f% Y3 d  f$ R5 k, J, E: e$ F
    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-2-23 02:43 , Processed in 0.063912 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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