设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    4 o1 C) \6 r: s- V1 r7 d/ h0 S% ?( P6 c* `* C! g
    解释的不错3 Z# P5 @) _& Y$ b

    & ^& ]; t/ v+ K# n递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。: S( I  G- p$ m) D. P/ {

    9 q6 U- b6 d3 j5 h 关键要素  ~7 V+ N9 K  h
    1. **基线条件(Base Case)**1 {7 B" @4 v* {) v' L& ]5 J- S
       - 递归终止的条件,防止无限循环
    " e' `8 o" g- p8 N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    2 g+ P0 `; s% ~
    - _/ L2 [8 e6 B) t& m! W2. **递归条件(Recursive Case)**6 @% g( {# E3 }6 B- U  E5 C0 I, `; I
       - 将原问题分解为更小的子问题; }* T% x; m( `0 K! `2 V
       - 例如:n! = n × (n-1)!
    9 l3 l8 ]8 e6 H1 P" O, g% x9 x" `  F& r4 J% \
    经典示例:计算阶乘4 `% U0 s) v6 e1 d3 f. K
    python* H9 e: b, I& d* J5 [
    def factorial(n):
    ' S+ F5 Q% v+ s    if n == 0:        # 基线条件
    , z; V6 K2 P4 X& R) l2 m( V        return 1& X0 L: [" H, E, i( L1 C# V
        else:             # 递归条件  ], o* A3 a4 K
            return n * factorial(n-1)
    , e! M3 }/ r; C( J4 Y执行过程(以计算 3! 为例):1 ]0 L4 s8 e( A( G) d* ]9 B
    factorial(3)8 z( |. ~2 m3 _6 n
    3 * factorial(2)
    5 M5 ^7 i; |, ]1 D& {3 * (2 * factorial(1))
    : z- R2 d6 I1 g0 g5 E3 * (2 * (1 * factorial(0)))
    , M2 e. X3 `( `# J, _. a" ^3 * (2 * (1 * 1)) = 6  c5 a, e" h/ l5 c# y& m
    ) T5 g4 B; _" [9 \, d
    递归思维要点% ~6 F! M- e, e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑. o: R8 p. E7 `9 D: M5 {
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间): c. C9 [: O$ i7 M# o2 _9 C* s
    3. **递推过程**:不断向下分解问题(递)
    * h- x  h- I  k; P; }, ?  k4. **回溯过程**:组合子问题结果返回(归)
    9 }$ e& p1 }: w) l+ P9 C. o! x5 E1 w$ o6 r" q' Z$ Y: y: _1 n  y$ ]
    注意事项9 K) _. g" z0 @7 }
    必须要有终止条件- e/ c6 U. u0 m5 c
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( u) _2 C1 l1 Z! {6 L* e  g某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ( ~, [7 @" r3 J2 U2 m7 o: t尾递归优化可以提升效率(但Python不支持)
    & r% v7 l1 W. q
    - l& n5 ^: i! _! A; g* p7 F 递归 vs 迭代- w( n& O8 Z( z! e% E
    |          | 递归                          | 迭代               |; o! i& d- I( e: S' B! D; ?. |( m% y
    |----------|-----------------------------|------------------|
    1 a/ g6 W- R. Y| 实现方式    | 函数自调用                        | 循环结构            |
    9 X. h! U5 S* @4 ?9 R) l! d. ^2 q| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |! A, v4 T/ F* _
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |. A3 y; ~; z+ V5 _+ ^
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 L& H  J( E) v  `5 i
    ) U+ J( ?5 ]" X& W  c 经典递归应用场景
    9 f& e* X7 w* w4 K+ p1. 文件系统遍历(目录树结构): X1 J9 H* F: V+ s, |6 O% _
    2. 快速排序/归并排序算法7 C7 a4 G1 X" C7 O7 h' D
    3. 汉诺塔问题
    " T6 o7 m( q, @$ z6 U+ D8 E/ \4. 二叉树遍历(前序/中序/后序)
    + f$ V# X, q7 M2 w; g8 O. A2 t1 t# w5. 生成所有可能的组合(回溯算法)
    9 h: D) j8 }& q* L
    ; Z! X2 P1 q+ X5 W试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 14:57
  • 签到天数: 3134 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 h8 Y3 N' W) A) b: a
    我推理机的核心算法应该是二叉树遍历的变种。& p* O8 N' p5 R7 L* S# s* ~
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    - Z! h# x( z5 C5 L- NKey Idea of Recursion
    6 g4 S  l/ I8 c/ ^2 A7 m# [/ s- I1 C8 }
    A recursive function solves a problem by:8 `1 @) D* m. B$ P; |1 W/ \
    2 V% f. @6 Y# M! m, ~: B5 V6 ]
        Breaking the problem into smaller instances of the same problem.
    " [. d( a  s; n0 O7 j5 p0 {: S8 [
    ( b+ E3 V* W' W( W: P9 A    Solving the smallest instance directly (base case).
    ( c  B  K- A2 ]* |( {6 B4 f* h. D! @* I  p  P
        Combining the results of smaller instances to solve the larger problem.
    : T) H+ \# a9 x' R' d
    9 y8 v8 d7 H# k: R1 U7 ~, i/ bComponents of a Recursive Function
    9 u5 b6 X8 |" W" Z$ H$ j/ V& g2 f$ l
      U  e$ |6 E; N3 a0 y    Base Case:
    ' S! @4 f3 g% R+ v- ^1 F* ]9 M3 a, g5 f0 c5 k0 G  Q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& H) P: V8 D6 S3 A: h; B- h

    - N3 u- p7 a. w% w8 P' V- U        It acts as the stopping condition to prevent infinite recursion.
    + a. Q- ?/ q$ j2 V  G3 u7 A" b7 z2 V1 h9 n( [3 D  R( w% R2 G/ h
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& Y2 a, u$ f: R' M

    ! Q1 B& p) c4 U1 q8 S* ^    Recursive Case:) S  I7 B1 A5 _& {" r3 {, J5 ]8 [

    * v$ t9 J' X1 Q        This is where the function calls itself with a smaller or simpler version of the problem.: R! J: a9 [. o  t4 A
    ' ~9 L" }' [- m  }2 X' A; X1 j2 [5 |: B
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 _2 c7 Z6 B; u* n/ L1 U( T4 X1 i5 R; V5 U9 o  E% f; x  m
    Example: Factorial Calculation
    1 P3 B9 g% c8 N& t: S4 R4 P0 A) k- m) P; S) w9 t
    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:# j9 c  s" l% x2 B) {' `

    : S1 i# A( b: ]9 Z. F; C    Base case: 0! = 1  C0 [# M+ n- H4 W2 K. ~" Z( t3 ]

    . t1 J" G! t0 ^    Recursive case: n! = n * (n-1)!
    # v0 A, I5 B7 l' i
    ' L  o  j" {- D) b& gHere’s how it looks in code (Python):- s' Y& D7 T. L" M0 @
    python) ^7 N0 h. d6 u- p! Q
    % R7 V8 J& x% b: |3 W, \

    8 B. b$ f5 A, b3 Hdef factorial(n):
    ! }' ?! K6 t& z% }2 D    # Base case$ N3 d: Q6 _1 S% ?
        if n == 0:
    $ J+ ?: s1 Q+ x1 J' ~0 f* C        return 14 k; m+ c  @  @$ Y
        # Recursive case* H/ N" X" k! |8 `. P
        else:0 T; ?+ K9 `0 d4 V
            return n * factorial(n - 1)1 s1 d+ v0 U8 F! l# ^/ S$ C7 f

    , F; N& h* q2 h( z# Z4 {/ |# Example usage; |0 M# L0 n  y4 B% d
    print(factorial(5))  # Output: 1201 m5 P. M7 n; I
    3 R1 F5 g; S& A4 G- ~9 @" [
    How Recursion Works
    8 W- `  H* f/ X0 T/ }! w
    9 k3 c( f) U1 }# H$ j    The function keeps calling itself with smaller inputs until it reaches the base case.6 X, Q4 A9 q' T" m2 M
    2 V+ s: z) C' g6 T/ {
        Once the base case is reached, the function starts returning values back up the call stack.; r: {1 B2 A& y! G4 e

    9 c9 x: C4 D' T) d5 |# F    These returned values are combined to produce the final result.* X. X2 R  D- j3 R0 y$ I% X
    ; D! d3 j% |& l. v3 k" S" j2 b/ O, B
    For factorial(5):
    7 f# V4 x2 O& k0 [1 i1 n: y% g: }

    8 m* ?2 c8 `! H' A3 m! f9 ^factorial(5) = 5 * factorial(4)
    2 L' J- Q' F  |' F$ w- m4 Q2 F& ]factorial(4) = 4 * factorial(3)2 K3 B/ a6 s, f4 A
    factorial(3) = 3 * factorial(2)& O( m+ T& j5 ]( Q  U9 j# \
    factorial(2) = 2 * factorial(1)7 {4 R# x! f: c3 W- F
    factorial(1) = 1 * factorial(0)
    % ?' g  d0 i9 y2 jfactorial(0) = 1  # Base case
    + ?9 z! W. @9 u3 j5 H, T
    " `/ p$ Q& ~3 B3 UThen, the results are combined:/ c+ M  C: d: p0 n3 r. x

    ' W' p9 [: o% y0 q  @! E
    / ?! `) D2 ]. v; I+ efactorial(1) = 1 * 1 = 1. ~$ ]/ B- {: p2 V
    factorial(2) = 2 * 1 = 2
    ; W9 D8 P0 M9 s, W/ b. h; `factorial(3) = 3 * 2 = 6+ B" S) o" c! W' M! K
    factorial(4) = 4 * 6 = 248 N2 v* M& t' @" ~# Y
    factorial(5) = 5 * 24 = 1200 ]/ e8 @# ^: ^& Y6 \; B" [

    " k+ Z0 @9 @) I  p2 B0 `Advantages of Recursion
    $ r2 f- Q6 r! Z. Q3 y1 H
    * U6 a+ U* ^/ L    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).3 X% }3 P$ f# I2 j! o" a
    9 C7 T% F8 C* G, @
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - @/ r" t6 X2 X: [& F$ s
    ) N0 }) l& l3 P$ b; c( B7 b6 @Disadvantages of Recursion
    + y" q" s1 ?5 R. o. Y5 r/ h, I2 G$ l! q
        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.2 }9 B, J6 t6 m8 D# u

    + s* v& v$ i! L  s    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    8 x( e, R' M* T- Y5 N
    / d& `" ?5 i% p' ]When to Use Recursion
      X) P9 g% H2 f5 B: f, I
    - @" w" D, ^! ?3 N- P5 b    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 S- W' E" u. ^( P, D+ P
    : L6 \6 D2 ~% _' K* D: q: |
        Problems with a clear base case and recursive case.. N7 v( l, h: s/ [  L5 M' ?# u: m

    8 v* _) d6 ]: ]; u" ?Example: Fibonacci Sequence
    - L1 I, u8 U# V8 `. W- _" Z& M% P5 d$ H2 I/ q
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ s3 D7 f7 i& O% ~2 F  V; C
    ! N. \4 M' P% n6 [+ u5 U    Base case: fib(0) = 0, fib(1) = 13 F" v2 }6 a1 A7 ~
    % ^" K7 W8 i( v) Q  @1 \" X; }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)0 o* L* `' P: Q' z7 n
    * o2 i; X' C) }- M; c& E9 l0 o/ q5 x5 d
    python
    5 E- S: C$ U+ @8 ?# G! \3 r& t8 f4 ?+ k+ c

    5 w; e' e+ F+ t' \- O. xdef fibonacci(n):# ]1 H6 L1 O2 d. |8 q% n# Z( o, T
        # Base cases
    7 f& L$ f/ }# ~! M! ]$ H! ?' M% y, h. Y    if n == 0:! j: }1 g: B. Z' F/ _
            return 0
    0 O4 G. U" M% Q- C8 O9 K5 }    elif n == 1:/ T) C: Q6 e* _; \) i& i# E
            return 1/ E6 b9 U0 t% m4 ?, M
        # Recursive case# C" `9 r  W5 e4 z
        else:% I$ H$ G4 M' E6 B9 P
            return fibonacci(n - 1) + fibonacci(n - 2)1 O/ {1 X6 K5 M8 D. t/ a

    % i3 ^; p& `9 G3 d* L% f  D# Example usage: V2 ]9 f8 c* W" }, h' q9 J
    print(fibonacci(6))  # Output: 8# j3 x+ u7 n  g, o8 M$ l
    * e0 Y3 `+ e- e5 x3 E
    Tail Recursion
    3 b' c/ |# Q( s. Y6 v. a& d' v8 ^% }$ W/ V
    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).
    & H" t" A0 y2 @! u* j2 \+ r) E! n
    ( {" K" A( k! S% {! wIn 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-1-3 08:04 , Processed in 0.036993 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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