设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 9 C: v. y! n& n& y+ M
    0 d$ O  P( l; D- \1 }  |. O' I
    解释的不错
    - d. ?% v8 B: I  ^+ X
    ( W5 l" l$ H6 ]递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ) k" i7 s: l' J$ n6 ?$ a& v
    & U, H4 p, ^: q  ? 关键要素
    ( U2 b1 K) D" j7 h/ Q. S1. **基线条件(Base Case)**0 Z% x" l, R3 M, z& X
       - 递归终止的条件,防止无限循环* D; f- i( q# A$ h
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 10 t0 B4 ?8 I' _. Z+ E

    ) x! n/ t7 m: ]& W3 ?( b- u2. **递归条件(Recursive Case)**2 k# V$ f0 y% d" f/ }0 A, u
       - 将原问题分解为更小的子问题
    , P& ?8 ?0 G- @9 a$ F- ?   - 例如:n! = n × (n-1)!% U7 G0 B- |- ?6 s0 m

    : L7 B+ X2 d2 c, k 经典示例:计算阶乘0 t! g+ x( s# c9 O5 _, z  o
    python+ m$ d: Z8 Q# j
    def factorial(n):
    $ R# n# v" d, v& P2 ^/ B    if n == 0:        # 基线条件
    3 z' b' ^; v8 T8 ^  c& D) B        return 1
    " |! C* T" Q+ O6 [9 k( l* A) r( p/ A    else:             # 递归条件
    + A. u. d7 z0 @% R7 t        return n * factorial(n-1)
    3 Q7 O8 c1 E* z6 [执行过程(以计算 3! 为例):
    6 M8 g$ H$ |9 w# k" l: vfactorial(3)
    # `- g2 p! f) Q, |; H3 * factorial(2)
    % e1 C; j) S8 J0 p2 f( h3 * (2 * factorial(1))
    8 h* v5 m1 T% S  c* Y3 * (2 * (1 * factorial(0)))* `- o( e8 b: Y5 T
    3 * (2 * (1 * 1)) = 6# a; t( d7 |" d0 ~

    " v& @5 J) {7 K" `' s" y. E, V( Z 递归思维要点) j9 C$ ?1 g( {1 x
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑: Y7 T" f3 {& r$ q5 P; X
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间): M; u0 P% _  W
    3. **递推过程**:不断向下分解问题(递)2 O: t2 ^$ E. S  Y9 r( o6 d
    4. **回溯过程**:组合子问题结果返回(归)- L* x$ N& j0 u" C" y# \# v
    ) E: S7 [. k2 u, p/ e: q
    注意事项
    6 j: ?/ k1 O5 U# I' q必须要有终止条件
    ' l* F: F: G! w9 H7 @递归深度过大可能导致栈溢出(Python默认递归深度约1000层); `0 Y. X8 X' Y# s1 J6 Z: G% K
    某些问题用递归更直观(如树遍历),但效率可能不如迭代/ ?( L# A5 Q+ i) O9 `
    尾递归优化可以提升效率(但Python不支持)2 z2 [- X4 {9 O8 [9 U* z. o( x
    ! P; m* i6 d9 O* W  i! v
    递归 vs 迭代) o% b# i$ L' g# J5 N: ]/ E5 }
    |          | 递归                          | 迭代               |
    8 c" r9 G- r" A$ w|----------|-----------------------------|------------------|6 B/ M) q+ H; W: ^" W7 n4 J
    | 实现方式    | 函数自调用                        | 循环结构            |4 p$ Y' {& v+ W/ v+ z) F
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |5 A  G: E3 D2 K& @9 R3 u, e
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    , z8 f- E3 C+ t6 z- p| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    + B1 {) z8 N/ ~% J: s, |4 B6 w
    4 m. I7 H7 P9 t7 A, b- @+ L 经典递归应用场景
    4 \' R% Q' J2 U+ P1. 文件系统遍历(目录树结构)
    3 n2 A+ V, l' e0 i2. 快速排序/归并排序算法
    6 _; J1 d# b, |1 T4 @3. 汉诺塔问题
    , R5 Y: e+ w: U- T  r: m/ s4. 二叉树遍历(前序/中序/后序)& R9 c& D: h: F0 v
    5. 生成所有可能的组合(回溯算法)$ `2 w: m! P+ b4 r' w1 e$ v, D
    8 J% j! i* W! Y7 s" o
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 07:35
  • 签到天数: 3216 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 j$ E' v9 [0 \2 H: F! i) z/ G
    我推理机的核心算法应该是二叉树遍历的变种。( j; X& g- k2 D5 C5 }! \1 B
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 [0 j9 v( @% M* i
    Key Idea of Recursion  x8 }  T0 G) z( z7 O0 Q
    9 F7 X4 C$ `! e2 s$ M" E
    A recursive function solves a problem by:
    : \% G$ M  E0 S3 u+ Y: ?$ w5 L/ X  P( ?% K- w6 u
        Breaking the problem into smaller instances of the same problem.
    . q. m% [) s5 e8 R, h1 B
    : N! G$ q* l+ G& U/ |    Solving the smallest instance directly (base case).
    " ?" m5 b0 y$ p* N
    2 a% I$ v0 E) R0 g2 Q' C+ P4 n. o    Combining the results of smaller instances to solve the larger problem.
    / B+ `2 u& \# G+ D/ e' @. A
    5 `( J3 X9 h& |) b. y6 |: _4 MComponents of a Recursive Function. ]* @3 O$ |' I4 ]" B% w
    4 b/ P( {' i$ f1 e
        Base Case:
    1 P4 w* T4 F' Y1 z9 ^6 G& h4 E' f4 X/ A: S& I! ]0 A- B, b, `
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / E) d' |4 v1 b# c' z9 R
    1 }. P. F0 N! D1 g& {7 t, x        It acts as the stopping condition to prevent infinite recursion.* Q, S6 }. I5 J' H( F

    - ^  Y, N# r, r1 c  x  v        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; F  k: p: P( v7 {  c! h7 i5 i6 T  t% P6 r7 T
        Recursive Case:
    4 N% o3 T/ H' U- h; Q9 a0 J, I4 J: |- L/ m: q
            This is where the function calls itself with a smaller or simpler version of the problem.4 a( X# e* N9 J' K& V5 Q9 J+ v

    * ^0 ^  z% e0 O; w" G* [4 |        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " V7 Y; @- i  Q* i5 F- Z9 V
    1 C; i8 J2 t7 M0 n5 ?Example: Factorial Calculation
    ' f( i% \$ [# m3 F% q! F" t1 r# c* A# E4 N
    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:
    7 l4 Z+ K6 Y. K& W7 [7 x6 M7 J3 s" [3 g' Z$ c4 n2 @
        Base case: 0! = 1* f( G: d, n7 C  A- |; x7 ?" g

    6 H( T& V7 g  Q" ?" ], U: M% \9 B! Z    Recursive case: n! = n * (n-1)!
    * H& }2 Q% a9 f! r" r8 r  C7 d$ k/ x, _% @
    Here’s how it looks in code (Python):
    6 L, f9 G) O0 }% x/ c5 Rpython% C/ |7 Z/ s, w$ B! Y6 A
    . H' q$ x% C4 {4 [- d

    - z. }! r: Q1 t% X) G5 ldef factorial(n):1 M. }2 ?' J% y/ P1 y
        # Base case
    3 @) E+ r% F+ \5 }* n# ^: u1 U7 U    if n == 0:
    # F8 t! o6 H: ^- v/ C& N        return 1
    - B7 x2 A8 d' [! q$ L5 e' w    # Recursive case% Y5 Q, Z! ?9 ~0 D: g! M2 k2 M
        else:" |, m9 ]! ^' m; d# @& i
            return n * factorial(n - 1)
    * ^* ^. n; p9 H; ?7 U# }# Z( _0 O/ D/ d0 n6 J
    # Example usage
    * T" z% {. ~9 @' {print(factorial(5))  # Output: 120
    9 K/ w$ s, v: ~* a: ^' l5 k
    * v0 v5 I* A$ `$ g  g( ]- T& Q6 `How Recursion Works
    ) K. p( q/ _4 ~/ R7 z/ O
    4 |) p% R" N, H$ {& R& C( |4 q    The function keeps calling itself with smaller inputs until it reaches the base case.! w2 A! x# y7 P$ V! G, k
    ! m* Y) }8 a3 m; t  G% a- M; H" s/ ~
        Once the base case is reached, the function starts returning values back up the call stack.
    1 z7 H: b' v# r5 e3 g, g1 b# {0 K/ E. a2 {- h( e5 G
        These returned values are combined to produce the final result.0 d: `  E: o/ n& w" `8 c
    5 v* x( Q9 c* C; f7 ?! T1 y9 K
    For factorial(5):" ^0 k  I2 p" x" d: b9 t6 D( O
    $ b$ F. b: z) `+ |- b# ^: H6 [1 k) F

    & T3 g, x0 F" d7 x! dfactorial(5) = 5 * factorial(4)! w- r. G3 @/ V$ ^7 e8 G6 C
    factorial(4) = 4 * factorial(3)
    1 H' Q& T* p% O) U* p! e% jfactorial(3) = 3 * factorial(2)
    ( R; |, N; k/ _6 y- H' yfactorial(2) = 2 * factorial(1)
    4 G$ |0 f0 [& pfactorial(1) = 1 * factorial(0)
    * U+ _+ |2 [# t' R) A) ]- u8 u# ffactorial(0) = 1  # Base case5 ^! [, J4 O9 j! E* a/ t* E% `

    ! ]! M8 d1 c& r, r) sThen, the results are combined:
    + S' k* r1 b) t5 O  o
    4 P* t- y- i; q4 N  k+ Q  j+ L  g
    , I# {3 X7 d, h2 j5 b5 ifactorial(1) = 1 * 1 = 1
    9 R; _( J" f( {+ z8 P, g7 Q5 Kfactorial(2) = 2 * 1 = 2: A* N, M& R) k6 G8 ]4 X
    factorial(3) = 3 * 2 = 6
    ' y& I8 e) b: I3 ?4 Wfactorial(4) = 4 * 6 = 24# O; N$ T/ s7 y: l# {
    factorial(5) = 5 * 24 = 120
    0 k6 A: i+ b' @. F2 A
    2 Z) Q$ m: }3 q! Y; T' G9 _/ O0 }% zAdvantages of Recursion! ?  X) n9 S$ J4 D8 P
    4 F( C9 R( k/ P9 m. D+ 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).( i6 c/ U- R( @

    - }- T% N+ Z7 l0 P+ O    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    9 b5 l* n5 q9 \  B6 N4 t
    ; L3 j3 \  n& e# N. _  G; ]Disadvantages of Recursion' J6 V  I+ Z0 r' e2 [- `& p% J

    & n& X3 E: f! f# N% O    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.( q9 i. q( I& ^

    ; m- E, x: k" x4 Z0 ?0 _    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    , G; _9 {" q5 e1 J8 _% G
    : m* r. I7 {* o  c+ FWhen to Use Recursion
    2 n7 @/ r9 \% V+ e
    # s( }( p7 W8 j7 W8 C" q% V: \    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! p- Z0 b( L3 R' X* `1 d

    3 A' _) _6 c& w% J5 c' Y    Problems with a clear base case and recursive case.
    ' c- Q: B6 Y2 A
      T/ k" U0 ~9 a5 hExample: Fibonacci Sequence; U" B+ C, a' Z" S7 ^+ |& @+ p: q
    ! }7 J% C2 N  w" ~6 ~' W
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 D# k5 f# p% k. C# ~$ \

    / W9 i& k7 L/ Q8 D  ?; J# ~    Base case: fib(0) = 0, fib(1) = 10 B: I! Z/ x( y/ r+ _
    & R* [3 h% F* Q0 [
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ c( j' i: Z2 {8 i, R5 I! G
    ' g+ P* ^; ^) t" V$ [# t) L3 N
    python* h! E: R( e2 ^7 B, ]' X$ y

    % }9 h, m( D* x6 b1 \8 b' Y$ n, @% K# K$ f5 V# D% N! F4 k
    def fibonacci(n):
    , i7 V4 f. w1 G5 S. |- t2 j    # Base cases1 t2 Y/ a. c& @" P% C. U$ [! }
        if n == 0:. G  P& G3 e/ L! T5 z
            return 0
    1 O# M, R1 w, e1 T) P    elif n == 1:& `$ m' M- I3 G# h  @! V$ [3 Y6 ?
            return 1+ H1 Q3 L* M" S4 H3 \- [( b
        # Recursive case
    ( O  E- @) x4 M8 a4 U7 h    else:) ?2 p! b0 S+ ]) k8 j6 x9 z( a
            return fibonacci(n - 1) + fibonacci(n - 2). ]7 C" v. w' g, B
    ) c" T, G! ^4 q
    # Example usage
    ' O( J- J* @8 h! u" I+ C8 {9 N3 nprint(fibonacci(6))  # Output: 85 J6 M9 {8 O/ ^& d# R( ^
    ' P* _, t7 M% F! Y
    Tail Recursion1 P  D' `' G/ j. D; ^4 r5 X

      L0 C$ h7 Q0 \1 B  hTail 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).
    : \' h0 V$ Z9 i# I7 a8 N
    " e( Y3 P* u0 fIn 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-9 06:48 , Processed in 0.061630 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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