设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    5 P" l5 @' @& X" o. d  U9 }
    : S9 m& _2 |$ y: ~+ n解释的不错
    7 j9 N/ _3 a1 T9 f( L! i4 _$ G2 w+ r1 ^* }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& }& t! P  s- b1 D7 p

    0 O9 z2 p  z0 |4 G' S 关键要素
    $ Z5 F; A# _% n0 S1. **基线条件(Base Case)**  Z$ g- S( G  P4 t( u; _, _% k
       - 递归终止的条件,防止无限循环
    & `; G# L( {6 `' g  V- R; b5 E   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 ]  K/ v5 Q$ _; _9 N% }

      s7 q( ?5 K# Z8 H6 r" o2. **递归条件(Recursive Case)**$ M+ V/ P( e: h- e$ C% y$ E
       - 将原问题分解为更小的子问题
    6 `( m( h& P9 x) P# m  c   - 例如:n! = n × (n-1)!5 A/ a8 D6 ?/ H1 D. u8 H- @( h% o. |
    + [# N  T4 Q5 j- B
    经典示例:计算阶乘4 D# D, R; Z7 q& d
    python
    , m# V3 w/ r  }3 }3 }8 v3 w% B% vdef factorial(n):2 L. l3 l( {. e( H
        if n == 0:        # 基线条件6 W; E8 ]5 g7 ?1 \4 }' Z
            return 1
    + h+ U1 a% n/ y. r    else:             # 递归条件
    & B+ T7 s* T8 I% b        return n * factorial(n-1)  C3 K1 x1 I  R; o+ a9 A+ z" Z
    执行过程(以计算 3! 为例):
    ( T) t* L3 Z( ]  p3 n+ i& h( L; a9 ?% Wfactorial(3)
    # k2 q5 Z8 B/ z+ p$ L3 * factorial(2), {5 B" |* S0 s8 ]9 P
    3 * (2 * factorial(1))9 q$ K9 n* `; I) C( @8 g2 K: `
    3 * (2 * (1 * factorial(0)))( h, H/ S' k3 K4 p& m
    3 * (2 * (1 * 1)) = 6. }* t0 M. D& Z; B" C7 p  F; B- G
    * @3 g# ?. R  ], ~3 `! a& b
    递归思维要点7 P2 K5 }( r8 J: v
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑5 n6 b2 U; f" [
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 \4 x, B+ e9 S% ?/ V3. **递推过程**:不断向下分解问题(递)
    ( g- u* h& m" S4. **回溯过程**:组合子问题结果返回(归)% ^5 c5 {! I4 n- d  _) y

    / M! @" S% d0 [: S4 k9 @/ @% }* I$ M 注意事项
    : k. |8 g& n* U必须要有终止条件
    5 b4 X) B; O; B递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' _" o1 `  o# G某些问题用递归更直观(如树遍历),但效率可能不如迭代3 L6 w& Q3 n4 v4 W/ q
    尾递归优化可以提升效率(但Python不支持)4 k$ r0 I* O' n( @- w% r9 o
    - g0 ]" Z* n' n7 C' |
    递归 vs 迭代
    " z( l, z% D6 J& `! q( [4 Z|          | 递归                          | 迭代               |
      I; S) s' B0 D|----------|-----------------------------|------------------|( n9 h& k. q2 Y0 e
    | 实现方式    | 函数自调用                        | 循环结构            |! u; l8 q! L8 L' P6 U7 |- ]" {
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |4 \1 q& _8 G9 R
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |0 B2 G( x. ~- Y( t
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |# q) @+ O9 |# ^, T- W) R

    2 x/ o( |& S' i- [ 经典递归应用场景+ v0 h) q: q4 U# g. O% \
    1. 文件系统遍历(目录树结构)1 M& F& j% X6 M- v& V; i
    2. 快速排序/归并排序算法- ^% N; W! M+ H! ]7 J
    3. 汉诺塔问题7 C" `2 X9 \7 p+ X$ X
    4. 二叉树遍历(前序/中序/后序)
    $ F' ^2 m) e2 \) k. [  _# y5. 生成所有可能的组合(回溯算法)5 w" ~  q# Q" h. V& S! d2 Z) Y
    7 }# h8 }1 `+ ]. l
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 07:29
  • 签到天数: 3203 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  L2 P3 ?% d5 n1 N- A. {- B
    我推理机的核心算法应该是二叉树遍历的变种。: C1 ^9 K4 \' Z
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
      |3 ]# Q8 n4 i9 }; m1 PKey Idea of Recursion
    7 |& d. A7 p& n: N8 v3 V
    9 @  B! D& U  N1 KA recursive function solves a problem by:/ x2 k3 k4 Y6 G8 ]

    & o1 \" C! [5 A9 x7 K" W    Breaking the problem into smaller instances of the same problem.% [4 u; L  t: \& f

    ! m# H8 @9 u: j) X    Solving the smallest instance directly (base case).
    7 C* x1 A/ M4 K+ X
    8 o! Q, p' Z+ |9 K1 n    Combining the results of smaller instances to solve the larger problem.  a- D1 x- b- r2 R$ `4 e
    ; R5 _5 {# g& X* s: a5 X2 u1 p
    Components of a Recursive Function! k3 B- V& e; K7 j+ u
    ; M5 X9 `) k3 k. U
        Base Case:
    3 ]6 ~* H3 q8 H" {$ P, |4 i
    * _* g: h9 j# x0 n9 b        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* n( R$ W* I( n  `. u
    7 _! t' W6 f" s, T7 L( c2 Q2 u
            It acts as the stopping condition to prevent infinite recursion.4 O( b7 G7 t- r) w8 T

    ! S7 j8 `- {# B% r. T% e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 N6 U" L1 ]! V2 D" ?

    & b- \- E( X" o7 P    Recursive Case:
    4 U# n. o9 Z# q1 Q( C) g. {. q' m5 D/ O! F# o6 N
            This is where the function calls itself with a smaller or simpler version of the problem.
    3 _0 Y) q9 K  o# \/ e3 k- A9 g% }
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 b% q6 l3 g3 W2 v
    3 r& u/ a% C3 E8 J5 P! Z- BExample: Factorial Calculation
    3 Q) E% V- e, M$ ^: X% L& v! g
    + p, h# d. W( V7 I3 [. o& aThe 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 K7 i( R& C: ^2 P, f
      H' x5 t0 S& H  j+ u3 H0 _    Base case: 0! = 1  D! ^! ?. l2 `+ D+ u* m4 B
    " U% N' h$ r: V
        Recursive case: n! = n * (n-1)!
    5 G) L$ Z1 S! j6 {8 P+ q& \
    6 m2 i: x4 l. l/ JHere’s how it looks in code (Python):' k+ L" I) s) H- e
    python' A9 _2 B+ z5 e' O/ ?, F6 C
      }% L- S1 |4 J

    7 T3 \8 G8 K! Q  u, Idef factorial(n):
    2 Y6 M) d4 |% b    # Base case
    8 J+ f/ }1 R7 i    if n == 0:
    2 x  x& I; N# @! r4 L2 z1 k        return 1
      P1 k- n% X( t: z' K    # Recursive case7 z& P1 v( t' w/ {. F& Q) N+ H- q* i
        else:" s/ S' F4 t! h) q4 z- h0 u& x; M
            return n * factorial(n - 1)
    * o* U( S; k! Z; [) f, E# u* k
    % o% L$ ]0 d( K# Example usage
    ) Z$ W/ T( L" Q2 Wprint(factorial(5))  # Output: 1206 C5 _7 a- z, y/ }: q8 {6 c
    # l: l0 f+ t" ?0 Z' d  f
    How Recursion Works
    8 z- Y# y% c& T5 w! b7 M' k7 c
    # m9 l0 Z& ^4 {7 K: _- h  s* g# G! x    The function keeps calling itself with smaller inputs until it reaches the base case.  m( R, T9 W) b

    ( J: @3 L. y) R2 z    Once the base case is reached, the function starts returning values back up the call stack.; z( A) m# v; h. a( A8 B

    # \+ F% _# q+ f1 B    These returned values are combined to produce the final result.
    & B7 l: S) @* j; r3 D# ^; T
    $ O0 ]5 B. U8 H% \3 @9 \For factorial(5):  C0 ^* K5 {% C8 g* X$ N5 W
    * ^* w5 P1 O0 }5 A) @

    7 Q8 t6 P/ @1 O% n* `& Pfactorial(5) = 5 * factorial(4)
    3 I- A8 i& c  gfactorial(4) = 4 * factorial(3)0 e2 J: C1 E4 K' u9 c4 K$ T# y
    factorial(3) = 3 * factorial(2)- O7 K/ x7 m+ X1 F3 Y' L* p
    factorial(2) = 2 * factorial(1)' L% V4 E$ I# Q, Q* X2 \
    factorial(1) = 1 * factorial(0)
    $ H( B- ?5 _2 b- V0 j: G- jfactorial(0) = 1  # Base case
    3 I' Y: V7 u' r! {: Z9 b  C" D6 i; }8 H' \* C
    Then, the results are combined:
    ! e' w) A# C6 T/ v  t, C3 X
    3 x! N4 y' q" k( O: i/ O$ p; O* U/ O
    ( x, n8 Z, Q0 p8 Efactorial(1) = 1 * 1 = 1, m; i$ o2 U8 f1 N( x
    factorial(2) = 2 * 1 = 2) P$ g- ~4 T( p; d$ L4 f0 {& _
    factorial(3) = 3 * 2 = 6
    . B8 `4 b$ I: B: Pfactorial(4) = 4 * 6 = 24
    & ~: E1 ~6 m) q; Vfactorial(5) = 5 * 24 = 120
    7 O" G% H* `) ^( f' S& s- z
    ; L$ c8 E; r' @7 P3 g# SAdvantages of Recursion
      K- I9 e2 X1 h6 l) J
    , K2 a% y2 E1 b) J* q    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).
    & h' H) a+ W& \/ s9 K, M* r, E- w/ E! F, g+ S, I2 w) _! g
        Readability: Recursive code can be more readable and concise compared to iterative solutions.. t/ ]6 I8 }% H

    ! ^! |+ H# L7 e5 x" E8 ]: lDisadvantages of Recursion
    5 F/ @" H5 R, G3 t7 b! ]/ W! q7 p2 F1 I% t' J
        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.
    . Q  O7 d+ l! \! y8 m0 U. l- w5 g4 n8 J
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    $ O7 M0 a; Z, k" V5 D3 H! W" |1 b1 ^6 a7 j, \5 H1 B
    When to Use Recursion5 x) f& L# h& Z# s2 g$ {& s

    ( K& Y5 I' l9 d  M    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " v9 o  O  z2 a- x' K4 y- K/ X
    . e9 k) j; p+ I/ n6 Y7 f    Problems with a clear base case and recursive case.1 _$ v0 k$ V0 T2 g
    2 x3 n- y7 D8 I7 P$ j5 q
    Example: Fibonacci Sequence
    2 q8 c0 v3 w. T/ S& H# |! [, Z: l, M- B+ L1 a
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    " `" p( J0 @) {0 j
    2 W  l. _% M, @7 ]" {    Base case: fib(0) = 0, fib(1) = 18 Y$ A: ]; P- t) ^4 t5 x' m
    ! ]+ L) D, w8 K' Q
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    3 n4 B: ^1 ^1 Z- S  T$ P3 f; K" y5 a* w) H
    python5 j( B/ V2 I3 R$ n  h5 \9 S
    - m3 V1 ?6 l  h4 f; d
    1 d& b/ U$ m  K$ @4 m% @
    def fibonacci(n):$ P" z# ~, W. d! c# h
        # Base cases
    . l- v( H/ M2 \! W8 `    if n == 0:$ N- ]' M) E' O- s8 h5 b
            return 0
    ( g' J4 n5 D, g4 y    elif n == 1:/ V" u- l) }) m4 b. f
            return 11 O/ g4 Q- n/ M7 ~  Q
        # Recursive case
    5 \0 A' \8 E* v  Y! }4 f' D/ {" s) R    else:, s7 M3 A3 N+ D* t8 L/ a3 p
            return fibonacci(n - 1) + fibonacci(n - 2)
    # M6 C2 x5 j: H% H# X
    , D; X8 X0 S( F/ ^5 i1 ^# Example usage; I: ?2 P' |3 _4 b3 x% c" g7 ]' r% j
    print(fibonacci(6))  # Output: 8
    " j4 D- o$ k8 p8 n: c6 F% b- K# y& F! {- a; r; |5 b2 ~# q
    Tail Recursion
    8 k1 b4 t7 S8 r+ g, ?9 _8 m/ f# `
    7 o2 |5 O' ?! M/ `8 ]& FTail 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).; c1 y& n; x7 c) d( b+ ~# O! ~5 w
    . ~8 K! G& `; |: y# O0 v7 D  S
    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-3-17 16:02 , Processed in 0.062825 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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