设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    # p. P6 n% o5 p) H* q' v* ^9 b! T
      s9 v6 q3 @! R& O解释的不错
    ) R2 k: H; h* s$ ^: U* J$ N, ?  y  K7 g4 {; r
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' m: L3 X; G  c2 J

    2 _$ O& b9 L3 T8 s8 r 关键要素
    ; e  c$ J8 b+ n9 w; l1. **基线条件(Base Case)**
    + k5 \3 T6 J$ s   - 递归终止的条件,防止无限循环, a  X# i- t, F3 D
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    : ]5 |; g# ?4 _$ h" \/ H
    6 W( \! I+ p" }3 [- X/ q2. **递归条件(Recursive Case)**
    7 ^. I$ u; ~( h# c/ Y. t   - 将原问题分解为更小的子问题
    % ]8 y( ?5 g: l1 D. g  A   - 例如:n! = n × (n-1)!
    * m) V( F4 b0 k1 d- u* x
    1 Y8 |) t0 h( V; `9 m 经典示例:计算阶乘
    2 \+ i$ h, M4 D/ T' }7 f2 X% `python1 Q$ v3 I. c5 n& W
    def factorial(n):
    7 i9 k6 p% p" ?+ q6 W* ~2 }    if n == 0:        # 基线条件
    $ ^7 X$ q- V  u3 X5 W$ |5 V        return 1
    ' F+ d' v2 g. K! P4 c3 I    else:             # 递归条件9 g; q, W. c5 c) W) d7 Q
            return n * factorial(n-1)
    $ o9 g/ B: ~4 W+ J, R执行过程(以计算 3! 为例):8 n4 n/ F: B$ L! z( f+ D
    factorial(3)
    ; v2 F# Q  y, n8 |9 Y: j3 * factorial(2)
      i: X" J# c% k6 \, |$ u: H' K/ ]3 * (2 * factorial(1))
    2 a9 E' E) Z# V1 T2 \3 * (2 * (1 * factorial(0)))
    ( _1 `$ p8 \& x3 * (2 * (1 * 1)) = 6
      t: z3 W: M+ L: T0 G5 I
    3 r  E) r& R: a9 s+ A 递归思维要点# Y" `6 O- p4 u0 a  a
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑8 n# i0 ]9 V2 j: i3 g& r
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    # f* |% y  c0 }2 Z1 |' [3. **递推过程**:不断向下分解问题(递)
    3 b7 J0 [( h  D; d& T' ~4 x$ ]1 o4. **回溯过程**:组合子问题结果返回(归)" `- e# D% Z: S0 t* O

    ' V" X/ M% G. B# k, Z- |3 A6 E3 v 注意事项5 W$ R# f% C/ @& v
    必须要有终止条件" U0 v  u1 k  h, o# Y( k
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' N$ I$ N. M8 Q8 E  ^4 g某些问题用递归更直观(如树遍历),但效率可能不如迭代
    3 h# G3 B2 L1 H+ Z尾递归优化可以提升效率(但Python不支持)  w. P# p' k) n" t; L3 x

    $ v6 v4 N% I) p5 ^2 s. d 递归 vs 迭代; p+ f( @. a, ]' s0 H0 E' S
    |          | 递归                          | 迭代               |
    ) b$ u9 c# L, C; O1 T2 z  i4 A# {|----------|-----------------------------|------------------|
    7 d; ~) l! N: @| 实现方式    | 函数自调用                        | 循环结构            |! R( j! H, [+ _7 Z. ?
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |# ?% y) V, N$ I5 R6 \- a
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    : i& ?9 q' R. E* T: \| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    4 S6 i! p% H2 c' l4 c) A' j9 `7 l2 Y. M* ?
    经典递归应用场景
    ; @* ]- Q  F  T+ V* q1. 文件系统遍历(目录树结构)
    * V/ I* ?0 x5 ]* ]5 _+ s4 t, R2. 快速排序/归并排序算法. V+ ?, w& M9 s  ?+ [
    3. 汉诺塔问题
    4 E( {9 M8 T% h8 e' O4. 二叉树遍历(前序/中序/后序)6 e, t- t! ~- j% e; V$ v6 o
    5. 生成所有可能的组合(回溯算法)# h. B. E( V3 e. |( G4 ]5 f: }

    * Q/ z4 F) I5 {# @4 [8 t试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 [5 x( \1 }5 z
    我推理机的核心算法应该是二叉树遍历的变种。# E2 _% X% N/ d! D
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:( p% _* Z2 K, W$ \% z% o1 c) t
    Key Idea of Recursion- b) h; e# T' U4 ^; Q
    4 K3 r3 [; p1 t6 U( W4 _) Q1 [
    A recursive function solves a problem by:6 Q6 O( r5 u  l2 d- Q! j
    ) r0 t6 }% I* e. j
        Breaking the problem into smaller instances of the same problem.
    ; i6 c  R* X4 h- O- l: ^
    & I% v% V( W. X3 R  W: a) j    Solving the smallest instance directly (base case).
      {7 H, Q1 a) b& s) [) n" O2 b, S% {# w5 f% J* f/ w, o
        Combining the results of smaller instances to solve the larger problem.; k3 m2 v: D! d

    % r& K' r, }' V- k' ~/ uComponents of a Recursive Function9 E4 C) S- T9 R* H  w! o" i3 [
    $ t3 q- r4 q' S+ u: p, W
        Base Case:; n6 }$ K/ C+ I/ w' {9 Q; A

    % S9 ]7 Z4 b  d+ K        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    $ g+ n# ~5 M0 Y" o8 e+ Q5 W, O  \' K; r/ ^( Z8 d9 ?
            It acts as the stopping condition to prevent infinite recursion.0 j, G# x) X6 o0 x
    " D1 p& o3 U% k
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    9 v  z  [- l4 W- |3 w6 y7 `& ~
    9 J/ `; Q  [( k5 v2 ?# B% h7 B    Recursive Case:2 ~7 ~1 `/ Z3 c( b: z% U- V$ ], ^

    1 ?4 c% o$ H* S8 z2 V- j8 \5 ]        This is where the function calls itself with a smaller or simpler version of the problem.+ E& X$ |8 j2 z% o, [
    $ |& \$ h% q- H, D- P3 ]
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& y  {) ^) t; B: \% B- l

    , [/ \- i" S! S" Q8 c6 `& V' @% MExample: Factorial Calculation
    7 Y( o; A5 }1 c: T  J! ^: Y' B& R: Q
    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:  v: ?  x- P8 N6 p9 L$ l
    6 L9 T% w" l1 A; {& y# C- R
        Base case: 0! = 1
    # I, N4 O. |" d+ L, X; N$ ^- W) z* Y+ n9 D8 o
        Recursive case: n! = n * (n-1)!
    ( F/ o, Y; B. r+ x$ m) @5 D  [+ ?" L% [' ]$ z! E* i
    Here’s how it looks in code (Python):7 I& Z  }7 T+ b4 n' q
    python. t+ k+ u8 U8 g

    % c" i5 y7 m# q7 Z4 m
    9 @% X# c6 F! t8 J* t3 Udef factorial(n):
      i8 f5 C6 g# ~" Q- z    # Base case
    5 y$ O/ Y- {* C5 j5 }# T4 L    if n == 0:
    " ?6 i3 B# G8 n+ s& X; v; ~        return 1* k2 X7 \& K) K
        # Recursive case
    ! O6 J5 \4 G  O& C* |    else:
    $ R1 L9 g+ H1 g( A6 a9 d. C9 M        return n * factorial(n - 1)& D7 u& O0 I! R* U

    / i( i% s3 _0 x, o. A; ?# Example usage
    9 x  [' r- `( B; j: {- O' [7 `( [print(factorial(5))  # Output: 120+ u1 m; B6 M5 _5 I+ L
    ) P: W* o2 X. S3 Q* S# s6 f
    How Recursion Works
    $ {) e/ C' z+ C, u
      a3 P( A7 w: ~/ j" l" Q- k    The function keeps calling itself with smaller inputs until it reaches the base case.
    3 Q# b% l! [8 J; t" w6 F$ _1 d; a: M, \) j
        Once the base case is reached, the function starts returning values back up the call stack.- i* e: d) m6 ~: b0 L8 E8 I
    ( |, ?# z3 U" A9 \- b
        These returned values are combined to produce the final result.+ V: E1 |0 I" {# [% t) T( H

    0 V, s  H' \. X  l) l/ J. B- x4 m) oFor factorial(5):
    6 x- A# L5 Q9 |6 }- }1 o& Q  T8 M  W9 I! l/ B/ x) W
    % F, A+ C( }0 E  d: {* Z* p7 W
    factorial(5) = 5 * factorial(4)4 O7 E2 y5 r( i2 v. U1 I, P
    factorial(4) = 4 * factorial(3)
    8 Z1 k* T7 i7 @$ Z, \factorial(3) = 3 * factorial(2)* V" q+ J* j$ w4 u* T
    factorial(2) = 2 * factorial(1)
    5 [2 Z2 }7 q& i! c0 ^/ D6 M0 `factorial(1) = 1 * factorial(0)  A3 ?) q" S5 E# a! g: B7 L
    factorial(0) = 1  # Base case
    0 E$ }$ N0 I  h# h+ |% v5 a/ T5 [3 u& k
    Then, the results are combined:- P; l7 T& i6 Q8 g- ~% L- s
    7 V% H+ y9 u3 ?$ J, `1 m; [

    ( z$ z& w* o" ?7 B3 efactorial(1) = 1 * 1 = 1
    ) P  L/ n8 a/ N( U' D3 t1 ~0 Cfactorial(2) = 2 * 1 = 2; [5 ~  |! g# p) ~9 q8 ?
    factorial(3) = 3 * 2 = 68 G& X( ~# D) O, ^% z
    factorial(4) = 4 * 6 = 24+ n1 i' R' g- \" K9 J- I) o
    factorial(5) = 5 * 24 = 120
      ^3 m0 {6 D7 ?! ]  [. t2 V4 f# x: n8 y9 S; c; D9 K
    Advantages of Recursion' G! i$ L  ^8 |/ L& R2 A! g6 I
    ) Y7 H) m5 g8 l) ]$ 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).
    : k$ o0 F5 e8 h' w0 m9 \
    * Y+ Z( d" ^9 ~    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ( t% F3 I- S, Q9 \; J( G; c3 y& ?' d7 K$ _
    Disadvantages of Recursion( W8 a% v/ c7 d) D# D, t" z
    9 D9 \; Z( F- u
        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.5 V" F  d! }' m  b: R

    7 O. i( C: O7 @, K    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . R$ }) R8 Y0 B/ V9 ?: T4 c4 D( l1 D( U- ^
    When to Use Recursion
    6 a7 b" Z6 E$ L& [. n- l% z  Q0 k* V+ y* U; N9 I
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    7 q" r& h7 P$ `4 K( J2 C! k  t/ A" Z) _9 J% C- A7 m9 q
        Problems with a clear base case and recursive case.
    $ j. f& h5 a( h; J. |/ z
    3 U5 R8 c, @7 o8 z/ @4 rExample: Fibonacci Sequence
    6 k! K, L4 J/ o  \+ N
    , d* P8 K' [. D9 R+ sThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:' r9 ]" o' v" Y0 ]  E6 D4 e& i/ X

      `8 u  }+ [' A    Base case: fib(0) = 0, fib(1) = 15 u% u) e# ^8 R5 l( {

    0 L/ P6 x* F, X/ ?* H$ f! p5 a    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    7 H4 l' W2 T) O( q9 f3 t# p2 n: l/ |
    ; e9 l2 {# {+ J! Tpython1 g3 ]4 ~# r% X; E2 p, k) z

    * H: `* c! [2 a' Q
    $ c& [: E( b8 q, c' }% |* y, vdef fibonacci(n):& G  C5 v3 |1 y$ z
        # Base cases
    4 c2 F  a% M: O( X    if n == 0:: @, }1 t2 }$ ?' ]" l
            return 0" O- n$ C5 ^8 }5 Z/ d' `0 a
        elif n == 1:  ^1 u5 A3 s5 _3 [3 r
            return 14 r7 i: h0 |6 @' m9 W4 g# A
        # Recursive case
    7 e. X" `1 c- R    else:# L/ C# _$ ^1 b
            return fibonacci(n - 1) + fibonacci(n - 2)
    + ^! ]" ~# T: c: d
    ) g* R4 U4 F; y# [# H* V# Example usage" |0 b4 [" e: o: f# {$ L8 s" ]
    print(fibonacci(6))  # Output: 8
    , o2 O1 M" z4 J+ H, f. q
    # \+ S$ I, P. y+ ^Tail Recursion
    6 y8 C( V# [/ A3 n. Z8 m3 H3 @
    2 h; K( j+ W7 U& Y/ ZTail 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).
    $ V  r( q7 o" ^! ^9 B9 U! u- I: B- L6 W; D2 y# v
    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-1-13 23:19 , Processed in 0.031938 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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