设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    $ |. m/ \& r5 h
    ! b1 S- Q" G2 S( ?. H7 |% H解释的不错
    7 L6 G# [  B" k0 H: b
    3 Q; ^* P5 W0 S% a1 A! L( D: _递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 a7 \# b( g& [% h3 F) w

    " O( M2 A8 y8 N: f  J: G  _% C 关键要素
    0 o: Q) K( Q7 d/ i! e6 v* i  y& w1. **基线条件(Base Case)**
    ( k% g2 L! ?- h& q5 r   - 递归终止的条件,防止无限循环: }# M+ L8 Q. J, \( `4 W/ D  R
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1  ^. c& s- A5 v5 s
    3 |% H0 X, p7 e( h
    2. **递归条件(Recursive Case)**# g1 _- Q/ T& \- a& X
       - 将原问题分解为更小的子问题; `6 K! E5 H; M$ J4 y9 T1 h  s, W
       - 例如:n! = n × (n-1)!
    ; w, F. l+ g: y) k
    : x& d( P- l( V- @ 经典示例:计算阶乘
    # \2 d( f) H6 m0 w* V" A! v3 C% qpython9 u- S' d! K" K6 f- T- u7 d7 l
    def factorial(n):
    : W: T& Y( P( b- u    if n == 0:        # 基线条件
    2 e1 v' u4 @6 q1 M0 H        return 14 Z; ~5 |1 A* H' {
        else:             # 递归条件
    8 v" L0 e: n1 P  E/ S        return n * factorial(n-1); Y# v& z8 b% e8 P. @+ i
    执行过程(以计算 3! 为例):
    , c% c" E% o, h8 [; x2 [factorial(3)5 ?# n9 ~  d" L
    3 * factorial(2), y  ^/ ]$ t8 L' ]( {
    3 * (2 * factorial(1))
    # S* q, H) ?8 L9 G9 [3 * (2 * (1 * factorial(0)))
    ; L; i6 g- O  t  {: t3 * (2 * (1 * 1)) = 6
    % {# q/ J; F" s9 i9 F
      f2 d2 o& a# |& i# b 递归思维要点5 X! n- @" F2 [9 A
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑7 L1 o% P# e( e# b
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间): ^: @  Z3 u  D4 _
    3. **递推过程**:不断向下分解问题(递)* y4 O- ?' {$ o1 h5 g7 a
    4. **回溯过程**:组合子问题结果返回(归)8 m  Q0 ^& p* `3 j) A

    ' q/ _4 z0 I1 O) E5 t. N0 \ 注意事项6 o( b& B% M7 L/ i. `
    必须要有终止条件: ^: O; D) L5 I5 ^# V+ A* h# |
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)4 h7 A8 e0 \/ M) M
    某些问题用递归更直观(如树遍历),但效率可能不如迭代( `* g, v, ^8 G& {
    尾递归优化可以提升效率(但Python不支持)
    % M1 g' G3 q+ h& _# b* R1 n* I# [9 `' N+ J
    递归 vs 迭代
    % j9 b  h4 S. z1 K0 [3 `# @( X" [( a|          | 递归                          | 迭代               |# I1 c, C9 D2 p/ w4 N
    |----------|-----------------------------|------------------|
      K% |* F; @( y7 o  G9 M| 实现方式    | 函数自调用                        | 循环结构            |
    ) i9 Q& R# H6 K6 g% [4 T6 E| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & {* b) D) L" N9 z7 m! [( U: B1 U| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |+ J& A  |9 R+ e! s; l4 y/ }
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% ~9 H- P* I& ~7 n2 Y6 A

      \$ q" b5 t& _; b% ]% M 经典递归应用场景
    " D% n8 _/ n( b1. 文件系统遍历(目录树结构)
    6 J5 z% }1 z5 @3 Y( m2. 快速排序/归并排序算法
    9 s# r2 K! ^) L1 `' h9 b" w* p( O3. 汉诺塔问题, o* ^, D8 J! t& K; D$ r
    4. 二叉树遍历(前序/中序/后序)5 G5 I8 m. O4 p& p
    5. 生成所有可能的组合(回溯算法)/ R( s6 q4 }' J6 N3 }9 S0 @& y

    " ?" Q; v) x6 U  f. i( J; h( A试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    15 小时前
  • 签到天数: 3224 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    " @# X* ^7 X7 z( @2 b/ M我推理机的核心算法应该是二叉树遍历的变种。
      U' H* Y- ~! D2 u" ^; Z# G另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ m; s& H9 F; G% ?0 O6 Y6 s& ]! }; k
    Key Idea of Recursion: O0 O! B7 n5 P: N

    3 R: Y* k7 P; yA recursive function solves a problem by:8 h' ~$ U2 P: P
    . }- N; i# C) _9 B; e6 w8 d. h
        Breaking the problem into smaller instances of the same problem.3 x0 e5 D0 t  W8 p1 [8 P

    7 C$ f' s( U- X8 w( _& A  E3 t% w    Solving the smallest instance directly (base case).& C' @  h& s, _* D8 _( z6 ^
    8 Z$ K9 p+ R" t0 t' ]
        Combining the results of smaller instances to solve the larger problem.3 I( x3 [1 x8 T4 p5 J- C, B1 U

    " G* H( c- J% {6 |( i2 |! G3 sComponents of a Recursive Function
    9 p  c6 c" m1 }9 y
    : Y* X/ E1 f( I6 U) q+ o* c    Base Case:
      g0 o6 w# G; f. y1 s  V# ^# a+ s  {- l3 q- A
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # z" R  f1 o3 ~# n4 w# e9 Y- `- m% i: K; p. X- e- Z  ?
            It acts as the stopping condition to prevent infinite recursion.
    * f" Z- n$ V' n% N& w; L/ P" m1 v: @7 S4 `2 b
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ U! M' D6 d4 u/ W; p% L" f
    6 S$ b+ j4 s( j5 W# b
        Recursive Case:
    4 V* x8 O7 c& q2 a% _1 L: Q& o5 t2 S0 j! E
            This is where the function calls itself with a smaller or simpler version of the problem.
      A# U9 H1 F: y6 G7 M" E+ Y- A/ q& `
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    : m- [, ~, u" E/ m/ b" v& _) ?8 R% d0 d
    Example: Factorial Calculation
    0 `, _; A% {) k2 P4 S9 C, [
    " `. m  w! r- IThe 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:1 |% U6 [- {1 D3 t* e1 {" ^
    1 F9 m/ M$ ]& M% {0 [6 B
        Base case: 0! = 1
    ; `" j1 X0 m7 w3 U6 L" c/ Z" B& G. r- `: k) a) P- j" c3 F
        Recursive case: n! = n * (n-1)!
    - c8 M' Y) p% f3 z- o# n! U$ v% n8 z& l2 T% z, u2 c
    Here’s how it looks in code (Python):  i/ m# k- f, e8 R; [, g- z
    python0 U0 I' Z, B& N0 I2 ^

    # d; E* q' P* |/ f6 d  a4 K3 }* \! ?$ N3 a$ ]0 k& F
    def factorial(n):1 ]1 ^1 L: [: \4 C! ?+ n% [4 R
        # Base case) X. G+ m* `8 t7 k( w" w
        if n == 0:. Z5 ?- o7 S3 P* `# k, c: B; E6 U
            return 15 C# i' p# `$ |& F% D* w
        # Recursive case
    # p, I& r# ~* C9 B; P" R    else:5 |! _7 l1 H! ?5 P6 a$ G
            return n * factorial(n - 1)
    2 p+ j. X; r, p. p6 F6 _$ S
    7 }6 n, L4 f4 X+ m3 w" a$ Z6 I# Example usage
    2 y$ {5 F3 q7 eprint(factorial(5))  # Output: 1205 x- ^2 z2 O  Y/ s- L
    , l* L) J+ p# M3 u: [
    How Recursion Works
    ' t& t. X+ M: W1 S% S6 Y1 g- g" K/ m1 U7 H
        The function keeps calling itself with smaller inputs until it reaches the base case.- z) W+ d: }$ Z) _" i9 l; \

    9 Y! e5 e3 H8 v- ~4 l' D+ l    Once the base case is reached, the function starts returning values back up the call stack.( H8 R5 U4 L$ P6 _# Y
    4 R8 ?3 B) M, `: A* c7 d: k: V
        These returned values are combined to produce the final result.
    " S, M$ u7 x3 t  Y  Y- ^0 }& s1 C# J5 s1 F! G5 M
    For factorial(5):. K% b7 y( N6 y! w, u6 @1 t  ?
    + \; h9 Z( X4 n0 Z3 S, Q5 J

    5 h1 G% N/ }+ @3 r( `factorial(5) = 5 * factorial(4)
    3 W- l. u/ N6 y; _factorial(4) = 4 * factorial(3)
    $ o; f$ [1 J! h( v( wfactorial(3) = 3 * factorial(2)
    : e7 h8 w* ~- S7 hfactorial(2) = 2 * factorial(1)
    8 u! y, P* k, O$ Z0 k9 p' ~factorial(1) = 1 * factorial(0)1 o% X! g9 M5 r' _6 w8 s
    factorial(0) = 1  # Base case
    4 j* z+ ]% C  o5 y9 ]+ E( w" i
    / }: P1 r$ y8 Z0 |Then, the results are combined:8 o" W& ~6 ]0 ]# I6 }/ p3 _
    " Y7 k# S7 P! H: X* Y8 a

    2 f$ L! |% L( i5 g; tfactorial(1) = 1 * 1 = 11 g& w7 U4 Y0 a: ~
    factorial(2) = 2 * 1 = 20 M" w3 H' f8 V0 B
    factorial(3) = 3 * 2 = 6( b1 c' X+ H0 X3 }; Z
    factorial(4) = 4 * 6 = 24
    / l9 [. o  W# v4 Xfactorial(5) = 5 * 24 = 1200 l7 {/ C* w9 Z& X& J' ?

    9 x& |6 H$ M8 M, x* q0 bAdvantages of Recursion
    ) g$ j% C. I  n" s' v5 f6 p9 r9 K/ E; ~3 b( x% F! r( U
        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)., O" H$ ]9 F& {0 O* Z# ]$ R% i

    6 k! _- U1 z  K- u3 I3 d    Readability: Recursive code can be more readable and concise compared to iterative solutions." |1 j5 A( t  I' a& n
    9 {: ~& }5 [! G0 e- k3 f; j% c$ b. N* G
    Disadvantages of Recursion
    7 s4 ^0 D, ]% j' ?% i
    . h1 ~  Y% p* E  a% V8 v+ |    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.
    ' m* F% M, `) X6 g. y1 t6 i: X9 V
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - _2 ?" \: p, d% A) M$ S# k- z1 D! ~& j+ l% s
    When to Use Recursion
    ! f1 d$ N7 B* W( R" {  y! T4 Y1 V, S- {6 r8 @7 [; y& t' |) g
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).0 ~# C* x% y" P+ S: b

    / U6 i6 h7 b' E9 w. t% h$ n    Problems with a clear base case and recursive case.' D% _- D) |9 }# r) E/ Y( G

    7 f0 W4 a' n7 w1 J( B2 ], cExample: Fibonacci Sequence9 z. F( H0 ~2 h8 T# G

    & M" J: I0 a6 z) dThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. u7 V1 p, J3 B3 W  S3 F# _
    ) Y; z$ N  Q& J5 `7 D; {
        Base case: fib(0) = 0, fib(1) = 12 U: Z: e2 s+ ]6 Z+ O8 ]
    / q# F1 C' ?+ O* _: v) G1 e7 c5 z
        Recursive case: fib(n) = fib(n-1) + fib(n-2)* m! u6 @  O+ F1 G

    ! C$ a2 Z  G) apython3 f; g: w' o1 p2 r6 h8 {0 k- N
    # C; z( K6 p2 e' {& U

    ) y& Q- z) q& j( Y& ]def fibonacci(n):, [/ N/ j; ?: z1 e" t  B$ @4 `  l
        # Base cases
    , b9 r- Z0 ]3 S/ t    if n == 0:
    5 p8 P, C! }0 ~( Z! R4 v; V        return 0
    , \7 p4 ^: s* k5 l    elif n == 1:
    ! t" ]% a1 B3 j, y        return 15 k% \8 h1 u1 Z7 Q- b
        # Recursive case
    # N5 g: E2 M9 c1 r: }6 S    else:
    + {  p$ Q" r* |0 S/ R        return fibonacci(n - 1) + fibonacci(n - 2)
    0 [' @, a& T, W0 y& o/ T
    8 v6 P: j7 Z" e* R# Example usage
    7 q) M" j4 B6 W( u, j! @print(fibonacci(6))  # Output: 84 P* T: G! ^. E, u: K; z2 I

      L/ D. U7 B; _9 B! OTail Recursion
    4 S& Z! \' B7 c5 @& {% T
    % k6 Z9 w7 z. i. f4 a# u5 |& LTail 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).8 _0 C5 B2 f! n! o
    2 W# u" d( S" {# O" T
    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-4-25 22:33 , Processed in 0.057052 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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