设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! O$ E; \+ G; s+ o7 t- b7 H4 S# D
    & Y4 j6 ~1 q- L. }8 X解释的不错" ~+ t) p+ e' I3 ^- k1 c0 |

    ! r( Y, q2 ^4 t: w$ {递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    , ]; w& a9 U8 ~6 n+ `" J
    0 L1 `+ N1 ~2 Y. ] 关键要素+ m4 m; ]; M8 C. G7 j
    1. **基线条件(Base Case)**
    , I" j) P" j' U. k% Q1 a  H! ]6 ^   - 递归终止的条件,防止无限循环/ q7 D) v0 P0 E9 W) u* k( `, h1 r
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1* F+ Q# W. s4 ^4 y% X
    # L0 ?. @0 v! E# Q
    2. **递归条件(Recursive Case)**
    7 V& b3 \& s- H( \+ k6 j+ K! Q   - 将原问题分解为更小的子问题3 ~6 A  X& m7 \3 q3 U
       - 例如:n! = n × (n-1)!
    ( U/ F! f0 {, _2 h  ?( H+ D- v( o* ?/ E5 r2 {4 q- j+ t7 [& F
    经典示例:计算阶乘
    $ C( j/ T# S! N' t7 C! q+ J( P9 Dpython
    - w  I* R8 i% T6 V8 w1 Gdef factorial(n):* t- v* k$ _. r$ H
        if n == 0:        # 基线条件
    1 S' w4 {0 @' p4 H1 j; i' u        return 1
    $ {0 R/ o$ P9 F3 g: h: C  f    else:             # 递归条件
    ) x5 |3 f+ o! q, Y3 C0 N0 U- b0 N# e        return n * factorial(n-1)5 V& D9 e$ R- ^& A7 [
    执行过程(以计算 3! 为例):
    4 |* }+ z$ K8 p) Ufactorial(3)
    # c% p3 u' [2 \% N4 T- f3 * factorial(2)7 _2 f8 l) r0 v+ u6 M7 m
    3 * (2 * factorial(1))0 ]( M5 d: B: `5 \
    3 * (2 * (1 * factorial(0)))
    . F0 [) k+ p' U, h; S& n$ T6 y" k3 R3 * (2 * (1 * 1)) = 6! k1 L6 h% ^0 s) q

    " w4 X! h. a. v. ?8 F 递归思维要点
    ; W7 L3 |# A0 J1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    9 ^, v6 a# r9 m: B2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ; W7 K' o6 X/ V! ~9 o3. **递推过程**:不断向下分解问题(递)
    " {) v  w/ @; m8 X+ h: K, m4. **回溯过程**:组合子问题结果返回(归): G/ d9 N$ B' W. }# \' Z
    8 k1 C- M8 |* |  C" y* h2 c
    注意事项# S7 q8 g! R9 w' B# f
    必须要有终止条件
    $ [% D) K0 F, R7 a0 E3 M' w递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    + ]) H( r1 K. W+ w  R某些问题用递归更直观(如树遍历),但效率可能不如迭代
    # s* d. u' B- A尾递归优化可以提升效率(但Python不支持)7 Q+ |9 s/ T0 W3 s4 y

    : q/ q6 I4 H  h. }( M: f! F2 ^ 递归 vs 迭代" Q# {0 X8 t7 A: F# p2 I9 M4 J
    |          | 递归                          | 迭代               |: U2 r) P# j4 K3 I
    |----------|-----------------------------|------------------|
    ' o' ~' N. @. ]| 实现方式    | 函数自调用                        | 循环结构            |; C2 n! E+ g7 U5 @" E* P
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ B1 G+ y2 G& q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    # Y$ O- M+ H: n* B; G4 ?3 E; r| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ F) X7 s( W" _" J, b* e) K& E

    ( z; C9 a+ l, n* _* K, e* a 经典递归应用场景" c( Z  l$ U7 [$ g7 ~) e, l  `
    1. 文件系统遍历(目录树结构)' E- X( r& S. |& `2 H
    2. 快速排序/归并排序算法
    " l5 F: ~8 [% x3. 汉诺塔问题
    - H9 `; C! J' I4 |6 I4. 二叉树遍历(前序/中序/后序)
    6 m/ D) S3 W6 e  e, n5. 生成所有可能的组合(回溯算法)8 j4 o6 e! M5 [3 K
    % K7 J% h- W/ X# I6 z, ]! k
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    12 小时前
  • 签到天数: 3105 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,! m/ U8 }) ^1 m; e1 [( w2 K3 `% s
    我推理机的核心算法应该是二叉树遍历的变种。
    + C6 m1 Y4 I. ], x) t另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: D8 ~- Z$ ?  P# n+ _" W$ J- S
    Key Idea of Recursion
    ; H: h$ D+ q+ U3 r
    7 t8 u* Y1 N* s7 |4 P# \- |" eA recursive function solves a problem by:
    6 ~2 q- N6 c  ^
    + f! r  H! I* ^# r% T    Breaking the problem into smaller instances of the same problem.
    8 i* S/ Z& g  O: l4 V
    ) S9 m: [2 y' O. Q2 g( h$ I7 ]8 E' ?    Solving the smallest instance directly (base case).
    & o! Q" \# T/ |4 V
    ) b( I% d6 R6 |. C3 V0 a& I    Combining the results of smaller instances to solve the larger problem.) H: J: V/ |/ \5 q# ^0 [0 ?

    + x7 J* q4 Q) T; p7 cComponents of a Recursive Function
    ) u: V% \+ B# C# S! g9 G# B' q% a( d2 l) y
        Base Case:
    ) ?" f- y7 X) m, T, z- e
    + e  y8 ^! C3 {' ~        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 |2 U7 t7 |' l: a* Z! t5 r# u' q

    ; u$ V" \% ^5 E' i" N: W        It acts as the stopping condition to prevent infinite recursion.
    / M0 L, E( e3 b, \* \
    & U7 `# W' j; h# g        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 ]: H" o' @, X4 j2 ?8 N5 [, z
    & w8 y% _# m9 m; I( l# q5 m    Recursive Case:/ M- |- F( L; l2 R5 y

      x$ }: P% }+ h# A1 P5 N* i        This is where the function calls itself with a smaller or simpler version of the problem.1 h# z7 B6 e" P5 i. n

    ' g8 X5 l7 K0 \, k) d0 R        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    * U6 [, S2 M' \* C3 _& Y& K& w0 M
    ' q. u' J( t1 g. U" ^+ yExample: Factorial Calculation  |% X) Z3 Y& G% U! E) t

    / r4 U) i( y4 L7 zThe 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:
    3 B5 l: D' M) O0 `
    # ~, y% }& {& s! ~+ c  l' G    Base case: 0! = 1
    & ]' y2 D7 r& L2 ?3 W! [3 v+ C5 r# ~/ g3 j3 ~8 @
        Recursive case: n! = n * (n-1)!3 g+ U9 c' S$ E; m+ a

      X+ j' P# S2 h* ^( g9 C4 P( kHere’s how it looks in code (Python):
    7 d1 e; ]' n2 i4 |3 R' M. z+ Ipython; w; R+ Y6 L1 ~4 Y5 j6 b7 b& c

    ; ^1 _; c. c3 `7 X/ Z/ s1 r; J/ ^8 l* D
    def factorial(n):
    9 v' i7 m' }9 l$ F) M" P% X    # Base case
    ! W! G7 l3 F& y$ U    if n == 0:
    : r/ j; Q9 h+ x, ?% T+ v        return 1( \0 x6 i) H, N! v+ S
        # Recursive case
    2 e/ ]: Y% v. b, W    else:
    8 ^! _5 c9 O, X9 O, i1 S        return n * factorial(n - 1)2 P, v' Q4 {  E

    6 j9 `' K9 @  |! G4 E) q# Example usage
    9 J6 L+ E# ?7 g! n: H3 E" \# hprint(factorial(5))  # Output: 120
    8 I8 K9 V, e" J# n/ E% v; a
    : L8 L& b9 I. B# S( fHow Recursion Works/ g$ P: [0 N0 Z6 k1 q

    1 x1 G, g" p* N    The function keeps calling itself with smaller inputs until it reaches the base case.
    8 m9 ~/ `/ E1 \5 V8 T' P9 O3 i3 F9 T0 V2 D/ u; e) D5 j" N6 B
        Once the base case is reached, the function starts returning values back up the call stack.* Q3 }& F% i7 J! w2 Q

    . A2 {# N! ]# t- F7 G    These returned values are combined to produce the final result.5 `" w+ f" H" m9 D! Q0 C

    * k, b( y9 u* r7 M. T/ RFor factorial(5):/ C5 c, U7 y+ @

    ( v: z+ T5 ^# a
    1 w5 H+ c$ ]) m8 r( U( Ffactorial(5) = 5 * factorial(4)6 |, R) ]8 u" [/ n) p& N
    factorial(4) = 4 * factorial(3)
    " g4 ^; S3 K/ |& D% c  N4 v3 yfactorial(3) = 3 * factorial(2)
    ! R7 |: a/ g/ ~3 D1 E/ ?factorial(2) = 2 * factorial(1)3 n/ K/ @9 |" _: [' J
    factorial(1) = 1 * factorial(0)9 [1 d' c- l4 x1 E1 G' v- S
    factorial(0) = 1  # Base case" M; M1 d5 P/ D' Z! ?
    ) r7 Z- Z% H/ k: t  |' k
    Then, the results are combined:
    * V9 m) x) \# P4 d4 ^9 V/ U* |; z( V4 f1 g' C/ p- F9 k% W+ O
    4 ^" {. w$ s6 k0 }8 T# U8 w3 z
    factorial(1) = 1 * 1 = 1
    ' A  h  |6 w% I8 T. f: Q- b& Ufactorial(2) = 2 * 1 = 2' {% u( f( X; M5 L- O7 E
    factorial(3) = 3 * 2 = 6
    0 H* R* u& [; g7 |: U: mfactorial(4) = 4 * 6 = 24
    , d. q$ A. N, X7 Ufactorial(5) = 5 * 24 = 1207 d$ E- B1 }2 e- d$ j/ g

    7 P# I/ {4 W7 g3 ~" m+ \/ p/ W1 HAdvantages of Recursion6 {, p! A2 P, ~- p
    / c* C. o0 O' K% q% l- n( ~
        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).9 C5 `, C+ L- p5 T

    1 ]% G: P$ O" N2 q    Readability: Recursive code can be more readable and concise compared to iterative solutions.( r$ g( ?- ?& [2 t8 U8 ]% V

    / r- A1 s4 K# n/ ]+ J# PDisadvantages of Recursion, [8 a) g8 k( q
    - R% Z+ j2 N5 Y
        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 m1 I/ X$ W6 ^+ j) M8 N1 v+ k; j8 `6 H/ Q' M6 r8 t# s
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).% A7 T/ A' S5 z7 C

    : h- Q# J/ l% ~; |; ^, G' lWhen to Use Recursion1 S4 ~, W) z1 j1 a5 l

    ' n8 T7 f* @9 X9 h) x    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 E4 ?. S; _0 r1 z* r
    + y% ^$ G! f2 J$ e# y- [! C, L
        Problems with a clear base case and recursive case.
    5 k, ]# v9 h, ~) y8 |- c0 t8 }5 l, H+ P0 g4 ?
    Example: Fibonacci Sequence: R( P9 a  V' i/ _
    - r2 B% \: b" ]9 [$ P# `8 A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ) d5 v1 ?/ v  p6 D7 x  L; v" [" b2 ~8 q2 u% U
        Base case: fib(0) = 0, fib(1) = 1) f; _6 P' R' e$ `1 w

    1 p8 B( b/ B+ R# S3 |* |    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 s  m: M1 E' o, W
    6 W0 I+ Q; C* h5 j6 B0 I* Jpython: s4 V+ \7 ~8 t8 V3 W, ?+ X6 a! o
    , j* c: I$ }: q) X/ Y2 h0 L) y# D. W

    : z- @. b2 h9 P7 ?4 Ndef fibonacci(n):0 g2 G4 ?4 L7 `1 y, m, {* g
        # Base cases2 g; C& y* W2 T0 N, K
        if n == 0:
    4 Y- Z' B; Y. Q$ r5 p/ s0 j- u; Q        return 0
    6 X/ K1 x& H) x/ s( i2 Y    elif n == 1:
    ( h; t1 {' j/ d+ p* ~4 k" u5 A        return 1
    . N) W, Q) C+ p6 \    # Recursive case
    ) _# e$ _' p) t    else:5 C, A* W. n* ^& {! L4 d) S0 J* ?
            return fibonacci(n - 1) + fibonacci(n - 2)4 h4 o3 l* y2 e/ k+ x! ^- S1 \. r
    % S5 K" s" C: W7 g& V
    # Example usage
    ) S) f, K$ B6 F& t3 K3 A4 Vprint(fibonacci(6))  # Output: 8
    / M+ U5 S% i4 z1 c) u
    & E; Q# |, q4 g; STail Recursion- c, n, ]& s0 q- P9 ?

    , k* W7 t- s4 H$ iTail 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).
    + B% n# A- P2 x4 F0 |
    0 V4 [  E. _5 s* n" D' R0 c" NIn 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, 2025-11-30 19:02 , Processed in 0.030766 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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