设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 q9 l/ p' |& S2 V3 k3 w# V( D8 d3 e/ K8 l
    解释的不错
    5 s  a/ k: p9 u( g2 _* `; c  E3 c, N. K1 Z, }" Y0 T7 W% N& S
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    # o1 s/ w$ V$ m! Z9 P- M
    : z( k; r& D0 K% l0 l 关键要素1 P6 u" t  m3 e' Q
    1. **基线条件(Base Case)**. R. [! {$ x  A
       - 递归终止的条件,防止无限循环# x3 S2 a) X8 K$ L- z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    4 F0 _1 M2 o2 w* h
    1 r1 s8 l2 J0 c# N4 z# _; k$ X& P% L2. **递归条件(Recursive Case)**: z3 y/ L0 E2 I- ?
       - 将原问题分解为更小的子问题+ K( u6 @7 c, ]
       - 例如:n! = n × (n-1)!4 s$ U# K0 y. c0 p1 f

    , ?6 Q( U4 }, M3 C3 z, ~7 L; t- K 经典示例:计算阶乘/ z# t7 s1 n# n
    python
      L/ ?; H6 M2 fdef factorial(n):, i; `$ f3 {- E  B) c- T
        if n == 0:        # 基线条件
    ( f) p6 Z/ D( |4 U9 l; v0 ]. @: a        return 1% S  M* e1 T/ _5 [" y- T
        else:             # 递归条件7 [2 y; o9 Z1 B0 p" }( T! s
            return n * factorial(n-1)+ G/ Q1 p0 ]6 h% |& `- T
    执行过程(以计算 3! 为例):1 u* Q& c. Q3 B8 J
    factorial(3)
    : Z4 F3 I  x. ~$ g( u3 * factorial(2)
    0 V4 C/ I7 |# X, t3 n4 u3 * (2 * factorial(1))
    0 V) H- l* m, \- Q! T6 v2 \3 * (2 * (1 * factorial(0)))% V7 Z! x9 S6 g0 ~3 ~
    3 * (2 * (1 * 1)) = 6- f$ L, O2 W: Y

    5 l) G+ z2 ~- E 递归思维要点
    1 u3 M9 r6 f6 [# F+ e# c1. **信任递归**:假设子问题已经解决,专注当前层逻辑# y  D1 x  C7 M5 H- }7 W
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 M+ h0 o0 T% T( G3. **递推过程**:不断向下分解问题(递)  M" `9 P1 k5 r$ B
    4. **回溯过程**:组合子问题结果返回(归)! \/ i! g& e8 r. s- e7 d

    & j9 Q  P. P: d 注意事项
    4 s3 a- J6 X3 k8 ~& u- j& t1 E$ ~必须要有终止条件
    $ S6 o: \. C9 z8 V- `8 s# J递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 I" `; L$ c8 i. A+ e! V: z3 `# `
    某些问题用递归更直观(如树遍历),但效率可能不如迭代6 q5 U5 `# x* e; n6 y2 `% I9 H
    尾递归优化可以提升效率(但Python不支持); e* c. c" k, v2 J/ B% Y
    " K& n3 n4 r/ k, B
    递归 vs 迭代4 C: w9 s) \4 J  m( Z) ]
    |          | 递归                          | 迭代               |7 S( o" s) Z. r
    |----------|-----------------------------|------------------|) P8 {. x4 P, |$ Q9 ]8 ^1 I% y/ F
    | 实现方式    | 函数自调用                        | 循环结构            |
    & n9 f' ?' U9 v+ O/ `. f| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |  {2 M' u4 y$ H& L. P
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ' U2 d* }' t0 [! A* N) }! i/ _/ w| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |9 a. ?* W: L9 t8 ?) ]

    4 r. D$ b2 {  {0 u/ k, `' R' W# w1 _ 经典递归应用场景; \- v: `  h. `2 C
    1. 文件系统遍历(目录树结构)
    % J$ d' s$ x" t7 W3 G+ L2. 快速排序/归并排序算法
    + }9 {, M( d, T& [3. 汉诺塔问题- r, F$ u& \  d( O$ M. x
    4. 二叉树遍历(前序/中序/后序)
    8 h" ^1 ^- S4 o5. 生成所有可能的组合(回溯算法)" ~0 W( h" b5 c( z% J1 n
    ! y# e+ K1 e3 ~' a7 }9 h( B3 _
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 01:20
  • 签到天数: 3115 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( c) x( g! ^- d% b
    我推理机的核心算法应该是二叉树遍历的变种。) T( U1 \* a- w$ ~. ^
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:. H. t3 y9 b- o3 u
    Key Idea of Recursion
    # W. |8 t0 ~7 s+ F
    . {; ?0 U' @8 P3 DA recursive function solves a problem by:8 B8 J7 z6 F4 z, d( O# i/ E8 p
    # p: L2 e: H8 N8 v- a9 d: A  {# n
        Breaking the problem into smaller instances of the same problem.& P0 \. D. Q1 V& [" z1 i: R

    / O* a# j7 l2 ]5 y$ c    Solving the smallest instance directly (base case).; R: F7 N- Z2 R, l% T- l5 {( r& ~

    0 t+ G" a  Y0 x* }% G    Combining the results of smaller instances to solve the larger problem.% z5 l: x' c9 c; s

    4 p$ D4 T6 g0 U$ aComponents of a Recursive Function
    9 i  y7 h7 a( A( K. c" L, T/ l8 q* N4 y0 g
        Base Case:5 \1 [( F- d, P/ ^2 K: K
    ; e2 N! Z& @0 v: G
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* I+ ?, u: g4 M0 l

    ' n+ P" p+ T0 u4 @) K        It acts as the stopping condition to prevent infinite recursion.
    ( G5 ?; ~4 ~& I, L4 d* v5 d/ T+ S- V, F' ]% L, M7 q/ W2 E" G6 g
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.8 }& t# \8 B" }$ I
    % v% w8 x" N( ?
        Recursive Case:  b1 s& f9 d( I% E
    # y4 C% j% r6 g: a
            This is where the function calls itself with a smaller or simpler version of the problem.
    , E( ~$ J6 k+ X- W( O/ Q( F
    5 ]8 x5 H. p, Z: N5 A# n        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).7 m. S2 o" T& l$ Y* R
      |2 ]1 X. }5 d: `# p
    Example: Factorial Calculation) D4 |* f, V3 q, l( O! M
    1 E$ L9 h6 G6 G/ Q  C* a1 `( p
    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+ x; g0 {' p+ a1 r7 L
    $ f. m. J* z0 C" ]; x" E
        Base case: 0! = 15 w/ H4 ^$ C, ~- w4 D: M
    4 [3 X3 K+ ^* g' a
        Recursive case: n! = n * (n-1)!
    $ I$ x5 j( E7 f) Q5 G# W( d8 _8 e' S5 c8 W
    Here’s how it looks in code (Python):
    ; I) n- d" T* x/ n+ J  A( m  {python2 m$ k" c- f& }) I& b2 k* C
    0 w( ~: K8 _0 N/ ~" }+ Z
    # h( O$ b4 W" b' n
    def factorial(n):
    4 f; e: f7 U5 b: E1 y' v( n  }/ S, J    # Base case# F& S% [! k, l+ W0 X# }/ H- o
        if n == 0:+ _! ?; X- Z6 n& p
            return 1
    0 _- z+ Z- y/ r, G    # Recursive case
    ; q0 ?2 i4 a. d    else:3 s- \) z, Z' h) N1 i5 a# i
            return n * factorial(n - 1)
    $ n4 Y" H# M! H& t# `% C8 n8 C! E, ]1 h; n2 m& V
    # Example usage
    9 b% v( n8 u" [# xprint(factorial(5))  # Output: 1206 W6 [* h* w( I+ O/ f9 K7 @) Z! A

    ! x6 Y' v1 v9 I" ?+ o7 l8 YHow Recursion Works
    9 L* E, W* M9 V9 ^" S/ d, C6 \3 ^+ J/ Y9 k; F4 i
        The function keeps calling itself with smaller inputs until it reaches the base case.0 _6 e9 u  e) U2 F  ~7 M
    0 Z$ Q" w: w$ I7 b/ R
        Once the base case is reached, the function starts returning values back up the call stack.- k5 U# q( T( E& r6 e3 a  V7 j. S
    3 q: i  W) B$ d, O% P0 r$ f8 h' @
        These returned values are combined to produce the final result.
    / G& s% D4 U; W- U: ^3 |
    $ ^' S9 e( I& g0 L5 v  {For factorial(5):. R9 a5 n8 k% D: W0 O; {0 e4 W
    + G6 G- M0 V3 u; r$ K- V
    & b% i# {2 }/ O3 J$ S" l% W
    factorial(5) = 5 * factorial(4)
    ( ?) v. s% O, kfactorial(4) = 4 * factorial(3)# ], W; T. ~- v- R' G+ H6 o- x6 I' Y
    factorial(3) = 3 * factorial(2)% _" t. `( ?. ^* K8 K
    factorial(2) = 2 * factorial(1)
    & @4 e- G' D( g; Z' C' dfactorial(1) = 1 * factorial(0)
    8 Y2 _* k& _# {" d: @/ l) ufactorial(0) = 1  # Base case% Y" Z* c5 ^9 [* A

    # {& o( r6 [/ f. X8 K5 z' QThen, the results are combined:7 D, t) v( C: k1 x' l9 C  d
    , e; e, L1 B' V  U$ i! C- H+ e

    ; l% I# D. _2 t9 v- _% pfactorial(1) = 1 * 1 = 1
    6 Z" j5 ^3 z% m: U+ c+ l4 hfactorial(2) = 2 * 1 = 2
    4 M2 x' G, Y. v, n; O, {$ o* Gfactorial(3) = 3 * 2 = 64 J8 r) `" g! l1 `
    factorial(4) = 4 * 6 = 24
    ) n$ ~# x! D2 Y9 Q5 Ufactorial(5) = 5 * 24 = 120
    $ \, p8 c% k# b3 G
    ( ?: R# E' x, ^8 j" X0 K3 x# IAdvantages of Recursion
    1 U7 x( Q4 l$ R9 t1 h" {
    - d4 s% Z, d) o" ]5 l8 }0 h    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).
    , ], F* s* m" V, A  m! Z8 v  k9 i( o
    6 k; `& O* |) B9 P" c# ~' ^+ l0 T    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 f( x5 z8 G% l7 z/ H% c' J6 O2 J$ H
    Disadvantages of Recursion; F8 D0 P3 i+ I* _& [: X. U/ p* ^, |
    6 v. s: F  x- G0 S
        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.
    # z. ^5 v: {. A4 U' I" d$ ~. k- F: m, ?$ O; a
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)./ i2 l" n5 ]; G
    1 b* Q8 r$ H& {. I! K# W# d) d
    When to Use Recursion
    4 i5 O+ z6 k- u0 X8 `& p% g9 D8 \2 `3 u3 z9 U2 ?2 k) {1 j/ A
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 r  i; o9 b% o
    ' d+ g- j7 c2 a
        Problems with a clear base case and recursive case.
    ' X4 T/ ?4 I: n7 S9 I1 ~( r1 r% k& K- @! S0 ]/ [
    Example: Fibonacci Sequence5 C7 s7 S# i- ^, b4 P
      g% j" {# m& ^5 a- H7 P
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    6 r) A, y# m4 N; N1 b. X/ `& u3 O
      ]+ P; _5 e9 P, h3 i    Base case: fib(0) = 0, fib(1) = 1
    * m( ], i6 x  Q% x6 x, B, K7 M" e& f# m' t+ o
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
      _% Z4 x5 x' W3 f* j: Q" O; l. m( P9 G
    python
    - B. X/ X5 M- |+ A
    # {; y* |- ^4 Q  X
    3 t! h) {; B$ k5 n* zdef fibonacci(n):4 H% Z4 B# ]8 W: C
        # Base cases/ ^9 i" I5 ^1 Q( m+ {/ q9 c
        if n == 0:4 h  t3 j4 V! X& j5 W- G# V
            return 0- F( g+ w( n& K$ K- w* W2 j. d$ K
        elif n == 1:
    8 d8 T1 N- |3 d1 T8 e& }) i        return 1
    ) p% O& s! t5 r" ~. z+ e. S; }    # Recursive case
    % b3 C3 \" b+ F    else:+ Y: U1 X5 ^% r# x' F! z* g, i6 f
            return fibonacci(n - 1) + fibonacci(n - 2)
      s- k3 {( q# \1 m/ N! g9 A7 t3 i9 U6 B
    # Example usage3 E# o' j+ q9 e3 ~
    print(fibonacci(6))  # Output: 8
    # w" X$ ^6 y# c/ h5 ~  D
      A: Y1 R6 Q5 E6 k) LTail Recursion0 Q* `; d9 t" y9 \

    ; F1 \9 U) I0 a9 C4 A# v# {2 @& @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).
    1 q! I2 t6 Z2 F. ^+ J7 Q# l) p! r( l! m: A4 X
    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, 2025-12-12 02:17 , Processed in 0.032067 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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