设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    * N' ~' Y- K% \% E/ t1 z" A
    9 k4 h) W+ J4 U; r1 q解释的不错
    % g/ T5 A& h) \& O$ K+ \
    6 M. @% Q3 l6 s& O1 M递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 t+ F  m4 ]6 i4 s4 P; H

    # i% d7 Q/ V2 n) b 关键要素- ]6 h: A3 Y" U4 \9 z5 b$ C
    1. **基线条件(Base Case)**) o5 p+ E: D' f5 M, W( N7 t
       - 递归终止的条件,防止无限循环
    0 i, n8 P. C  _# W1 X6 S   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ( N+ e5 r6 x# F6 o! j0 S
    . C. i8 F8 \1 j0 }2. **递归条件(Recursive Case)**
    % p1 _) l1 J5 w. I1 M   - 将原问题分解为更小的子问题
    ; k( r6 x$ C, |: H2 j% Z4 H   - 例如:n! = n × (n-1)!( s+ `; J7 S1 ?3 E
    4 Q1 y3 X  m& h! ^2 O8 a" ^0 O
    经典示例:计算阶乘( H* r8 R' H6 ~% t/ K8 A
    python4 O4 a4 J+ I' n7 }8 O- b
    def factorial(n):
      y/ ^3 h- q* C    if n == 0:        # 基线条件
    2 {- F2 h$ V4 o6 ]        return 1- D5 ^- d% E' P2 i( X
        else:             # 递归条件/ l. z/ S, d* j( L  I! c1 M  j  \7 R" j
            return n * factorial(n-1)) h! z) t$ q6 ]! s* t
    执行过程(以计算 3! 为例):: }5 t7 D+ y9 r: N7 D- F+ m/ b
    factorial(3)
    & `4 v/ K. g( L3 I0 Q. f3 * factorial(2)5 \1 v9 r5 e- k! i+ Q# r
    3 * (2 * factorial(1))) u1 Q! t1 c. D- Y3 L
    3 * (2 * (1 * factorial(0)))& Y; `$ k& B2 a% P* y+ n
    3 * (2 * (1 * 1)) = 6
    3 l6 K" j% D; \/ v, t8 ?1 `' G& i9 F1 h
    递归思维要点
    ' ^3 n0 b$ |' u# v  I2 \1. **信任递归**:假设子问题已经解决,专注当前层逻辑: p/ A% c. G  d" [5 Y1 e# W  ~/ q
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    . x* D: t! f: E6 }5 w9 `: f2 e& ~3. **递推过程**:不断向下分解问题(递)+ S) h; Q: L$ g$ {, x3 g* x6 X
    4. **回溯过程**:组合子问题结果返回(归)  o6 r. ^5 U! I

    / c" J( x  a3 s9 z 注意事项/ P/ j1 D: Z: }0 m
    必须要有终止条件& g8 ~: o, t+ W0 ~* K6 v) I( F
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 D. o# ^1 d4 g% i" y; w某些问题用递归更直观(如树遍历),但效率可能不如迭代
    0 Q6 N, e  t$ Z9 ~  t* M尾递归优化可以提升效率(但Python不支持)
    " _8 q: I, I: v! N
    ; S0 j4 g. m/ E( m0 W" k 递归 vs 迭代
    1 ?. y0 r% `8 {( z5 E|          | 递归                          | 迭代               |# t8 X! G4 H9 X, U+ i1 }
    |----------|-----------------------------|------------------|
    8 R! j$ T/ f, m2 a# K" {! N| 实现方式    | 函数自调用                        | 循环结构            |
    / B% X! D: s( g% z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    : @( x6 x. j8 a/ s0 f| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 R7 ^0 R3 C# W( b3 V| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 Q& G5 R/ R. X/ Q9 P) @# j
    9 K# a+ H! i8 t 经典递归应用场景. S; E1 l* R( q, i) ~9 b( [/ @
    1. 文件系统遍历(目录树结构)
    5 f! H" Q3 K. d7 B+ X2. 快速排序/归并排序算法4 @$ ~+ j* E. `, N' x0 z# U
    3. 汉诺塔问题
    : I. @. A5 j2 m8 U1 y' K& N% u4. 二叉树遍历(前序/中序/后序)
    3 Q3 e$ u5 w3 T4 h' B* s/ n5. 生成所有可能的组合(回溯算法)+ d  I$ a9 l3 \( h2 a/ \" l
    9 \4 F7 D3 e& ]; k8 j3 Z& G' v
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    7 小时前
  • 签到天数: 3211 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,7 \" \$ L- _# q2 r( W) @: C
    我推理机的核心算法应该是二叉树遍历的变种。
    & n* y7 V( n7 [另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:* r1 ?8 g$ H1 O! @$ T; |
    Key Idea of Recursion
    + E$ L7 n6 Q1 \  H0 b- A6 q* W! i2 q' m  r3 I% h% f
    A recursive function solves a problem by:7 U& t+ U2 d- [
    + v% u) Q2 i! N
        Breaking the problem into smaller instances of the same problem./ z1 n* Q! ?: C. N9 J3 O+ S
    + C& t( v$ f* A& m3 a3 X
        Solving the smallest instance directly (base case).2 H( X# t* |. `% }$ R  a: I
    0 O  |: s$ u- W- ]0 N  M, \
        Combining the results of smaller instances to solve the larger problem.
    , r' j9 k+ T: W& S3 ?* w/ z* M! t6 h1 U* Y& [$ Y
    Components of a Recursive Function  O6 \( s% j2 j: a+ E

    - N7 V; i- q+ d5 R$ Y    Base Case:* g/ ~  [3 {  r9 `2 M4 o
    ) q: b: h9 L5 E# h6 M
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 R' o: S2 c# Q  A7 i# a/ G& o2 g; \1 }- X  @2 y5 [$ C
            It acts as the stopping condition to prevent infinite recursion.0 z; ~4 j( S( f4 e; d" Q$ t
      G/ Q% i" A  v1 j4 m! @) i
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    1 z+ e0 {) g1 j: c! b1 p( {( ^  l2 f9 t5 G* L" }1 J
        Recursive Case:/ }9 f( k9 C' j* c$ {
    8 y" |" Q( U" g3 b8 U3 ]0 ~
            This is where the function calls itself with a smaller or simpler version of the problem.4 b4 P" r  P, e) l: E& K% C' D
    # t8 E; W) c4 l, W
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 f* }) Q1 \3 S6 v9 F
    / e; c; v$ a2 {. o0 @Example: Factorial Calculation. r* v) P! }1 I) h: I2 d/ B8 E
    ! r  K. t9 s9 f
    The 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:
    # Q- F( `  e3 d  X, T/ y% F
    + h! b) g; {8 K    Base case: 0! = 17 t1 M3 @! C9 a

    7 G  P  Q( ]; u% d/ x7 U  i    Recursive case: n! = n * (n-1)!
    # h0 o( R1 w4 U! a
    0 R8 H$ R9 a  n# YHere’s how it looks in code (Python):3 k7 L3 N  M! ?; C
    python7 S; i; O5 @0 U1 `
    3 C5 f( [1 X: q. q1 o3 n& S
    . c- s' \; K& _. q) a( r( h& R
    def factorial(n):/ `) L% i; W, F- G
        # Base case
    # R& ^9 T6 M+ K/ i+ ~- T    if n == 0:
    & E: V4 q8 N  B6 z* O4 [7 n        return 1
    % H; c3 A8 F1 @    # Recursive case
    2 A( C$ @7 x( X1 I" u    else:& y0 c9 I! V3 v& q0 h" x
            return n * factorial(n - 1)) Z* Q( R9 q) B' v. M$ D

    3 o' _) [* K8 i/ P# Example usage
    3 g0 w: Y* k( d3 q- r- C$ zprint(factorial(5))  # Output: 120
    ' |  a: h, A  M) i; s4 t3 w* w9 m& W7 M4 g2 R8 n# G/ D
    How Recursion Works
    3 `1 w0 r) K- f5 J: Q' {* h$ K$ I% s( u9 I' l7 U
        The function keeps calling itself with smaller inputs until it reaches the base case.4 w7 h' D& {8 L; G. Q. j
    1 S% k( G0 y% R' A
        Once the base case is reached, the function starts returning values back up the call stack.  ]5 A% {  k' ^+ Z( _4 f

    . U5 ?$ I. O% p7 h4 W, v  D    These returned values are combined to produce the final result.
    ! U  n+ S  k) u
    8 H) k$ A( z' B" HFor factorial(5):; [6 Z$ G% Q4 {2 b9 L  c
    $ W+ ^% d2 ]  Q. v* V2 L. i$ x
    9 z- N3 X. q4 \1 n  ~
    factorial(5) = 5 * factorial(4)  f* |) v; a. o7 L
    factorial(4) = 4 * factorial(3)
    2 a" P( E3 i7 m: D# _; d0 rfactorial(3) = 3 * factorial(2)
    ; m8 z, A8 {$ r3 f: D6 F5 @$ k: Dfactorial(2) = 2 * factorial(1); i2 ]# M2 P9 \0 \6 X2 M" Z
    factorial(1) = 1 * factorial(0), O9 x1 o  x* Y, O6 N. i; _
    factorial(0) = 1  # Base case
    ; ?$ u  N1 J0 j' b: A# l$ I/ d% Y, g5 ~' N2 V/ X# k% `
    Then, the results are combined:# H5 ^7 ?9 F: L# ?" p, u5 x
    . [, G0 U7 M: d; r1 b- g( v( H
    - z$ Z' z* |% x) R
    factorial(1) = 1 * 1 = 1! M" [6 C( a# Z7 R
    factorial(2) = 2 * 1 = 2
    * @$ W9 R- \7 N& ifactorial(3) = 3 * 2 = 6
    " X0 m, i1 D9 k, M6 Gfactorial(4) = 4 * 6 = 248 q( t1 z0 G0 H' ~
    factorial(5) = 5 * 24 = 120: p; C7 \5 u; z/ _) C/ s5 R( f4 p

    : Y9 q9 _  Q3 O+ t# }2 ^Advantages of Recursion
    ; B$ u7 T- A: g$ I& `; p; T8 S
    * @1 v4 ]& p% `6 w. B# g8 ?    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).$ x7 s$ a; T- X' R; b- i/ P0 A
    ) L# O* w! D# Y2 b. w" g
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    % k8 m' T" Z2 L3 S0 Y+ z! Z! d
    4 @0 G2 V( Y1 P# l$ x1 T( S* pDisadvantages of Recursion4 [2 q/ }1 n2 J1 E

    # G" c" E% i6 \    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.' F; F7 r; \/ l

    - ^  X, L3 P5 {1 J    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 ^5 z# g9 B& ]

    8 [2 i- l  K. r8 r! n4 u4 HWhen to Use Recursion& V4 W$ Q7 r/ G: v& q3 ?( E
    2 f  Z# H4 e, U
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- s7 Q$ e% ?  B3 Y
    + J& }. l( v9 p5 P6 k2 \  r
        Problems with a clear base case and recursive case.% D" ^- W: |5 x- l. z" T

    1 b* E5 ^9 _, lExample: Fibonacci Sequence
    $ y" N% G9 p( n% U
    / N' k" S- t# \. q5 a: j: n" }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - Y0 V  x) Y  Q- ~" f5 c/ \7 G. |
    ; _# D0 ~5 w1 V& l* E    Base case: fib(0) = 0, fib(1) = 1; j. [& M; W; ]5 X' I# z

    ( ?) X4 k' v: b( h$ C" W    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ; n( z, x2 C. H- V! q$ l& g" Q
    # e9 c( r$ _: Y# ~2 ~python1 P( H4 o! X  N# I, @, T

    ; ?2 g$ b, k( h4 n$ u0 d3 [0 a' |2 s( W/ [. U% i
    def fibonacci(n):
    6 p+ E4 a* S; q6 M3 _9 d+ [2 @6 b    # Base cases7 F( F7 G; }4 Z5 I. }7 n
        if n == 0:& U2 Y$ g5 l' P: i
            return 0. s  z, U6 e) r/ Z
        elif n == 1:
    * N0 g) c% t$ `3 f, Z# h; b3 a        return 1
    / t) X$ E, U' {/ x$ W  V    # Recursive case7 ~0 n! J7 J2 d$ a
        else:! V9 Z- X) a. P- k- B- t3 D
            return fibonacci(n - 1) + fibonacci(n - 2)4 t* w; v6 B' `7 o
    6 I; p/ e3 p& q1 K5 x# H
    # Example usage& Z, p/ J3 t% t1 S
    print(fibonacci(6))  # Output: 8) S3 k5 H8 c0 C4 \% N: g+ M4 w

    % w  w- G2 c* ~: W9 ]Tail Recursion+ o0 B/ D  r* n: }6 d1 }
    & S& u  _, o$ A- m# H: p2 L: 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).2 g; h' N0 y1 X4 u" M; @

    * h  o( w) G7 V+ Z( B/ Y; b% 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-4-3 14:07 , Processed in 0.063036 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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