设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % u3 m9 d9 M/ Q# ?/ b  f
    1 v4 f. }/ l) x3 \
    解释的不错
    1 _" g* r; n/ S; d0 K0 ^- K! z* Y( a& |  J: I) v
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& D7 G+ t3 H1 U7 p) {: L: V' E

    # x: R6 @+ c! i, M8 L 关键要素
    5 ]) w+ G- a: T$ W1. **基线条件(Base Case)**
    " S4 I; B* o7 e   - 递归终止的条件,防止无限循环% q. f7 Q- o5 I" G
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 `/ C7 S# H2 f0 b# B, o
    . L: {) L0 @" X. b7 X% Z
    2. **递归条件(Recursive Case)**
    * h6 h, i; G3 V9 G: a4 o. k7 J   - 将原问题分解为更小的子问题
    " ~! }/ k' K: ]: |   - 例如:n! = n × (n-1)!! i5 ~7 M$ Q+ d6 l
    & o4 i, {0 \" G/ D$ a1 U; B# C& w
    经典示例:计算阶乘
    - \; Y0 Q: d# q! w+ Upython& M+ N1 I. l4 K: l7 b6 V+ }$ H
    def factorial(n):
    8 `* L* r0 V5 L) Y# r6 w    if n == 0:        # 基线条件
    & V8 z& E1 b  J% q) m        return 1
    9 k$ l& T' x# q6 t1 F* N/ T    else:             # 递归条件9 M' h& c/ B; ~+ ]7 g) P; f
            return n * factorial(n-1)
    0 S3 N+ A) w. F. N6 L执行过程(以计算 3! 为例):
    8 r8 I) {* d( Hfactorial(3), R8 b5 `9 ^" I' ~/ ?
    3 * factorial(2)7 l6 A5 q/ O: z* W. V0 S
    3 * (2 * factorial(1))
    2 B$ d% w! G8 S- k: t4 l! M) P3 * (2 * (1 * factorial(0)))' x. i, {6 u0 I! V* d  B# W8 B
    3 * (2 * (1 * 1)) = 6% l, c: a5 [8 I" t: j

    8 K7 S: V5 F# x; a& d 递归思维要点
    7 x) y( x! j' m- |3 I' r1. **信任递归**:假设子问题已经解决,专注当前层逻辑- M  ^$ L% H. b/ i
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ! F' q( f( {1 L. A' Q3. **递推过程**:不断向下分解问题(递), B' F- y& R9 }  o" T
    4. **回溯过程**:组合子问题结果返回(归)# d7 {$ m* }& C& x" S: H0 e1 k

    $ o! E. Y0 L/ G" t( H6 V9 K 注意事项
    7 [$ ~. |; d. L1 R: x必须要有终止条件
      c/ V( q# n$ r( Z) ^6 Q# o递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    7 j9 [3 K/ B  F" ^8 B# o某些问题用递归更直观(如树遍历),但效率可能不如迭代
    % ^% |" w( `8 ~2 F2 ?0 d尾递归优化可以提升效率(但Python不支持)
    5 d, K# u( A; Y8 j- C( z* U& |5 c6 j4 C& G# y8 S
    递归 vs 迭代: k, v& s- U6 E# \
    |          | 递归                          | 迭代               |* ~/ X  ^- v7 X$ R' I
    |----------|-----------------------------|------------------|2 I2 T, E, T7 s$ a3 E$ _! D
    | 实现方式    | 函数自调用                        | 循环结构            |
    , h3 z/ J7 F0 b) T| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / Q! S; b7 @, d7 @| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    % T" l6 {# a, d$ g/ h| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    5 m2 S' j: k8 I# G- A9 d
    , u4 o. g* v8 h, A8 W$ G, E 经典递归应用场景3 i5 ^0 k9 N; |5 ^8 y) R* @
    1. 文件系统遍历(目录树结构)
    ) y3 f2 P; `/ C6 I2. 快速排序/归并排序算法- g' S' z3 P" f, Y
    3. 汉诺塔问题  S* q0 r8 G7 g2 S+ E/ p0 v
    4. 二叉树遍历(前序/中序/后序)
    " s  |/ K1 ]( ?" C2 X6 I: X' n+ [5. 生成所有可能的组合(回溯算法)
    2 |9 F4 S* Y4 F( C3 p1 }  K7 p6 a2 P3 X) Q3 \8 i  E
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    5 天前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,* \: |) b( W" P$ o8 E* ]
    我推理机的核心算法应该是二叉树遍历的变种。: I9 b' R& v6 x. t+ u
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:6 _' F% L/ ]" l. I5 q9 {( Q
    Key Idea of Recursion
    % c4 |: P7 X, ]: @
    # i5 w  R5 G* d4 \8 J/ iA recursive function solves a problem by:$ z0 q! h# w! f2 }7 U( v8 B

    * Q8 `1 o9 C. x* U6 `5 e    Breaking the problem into smaller instances of the same problem.: T( a, G4 J+ }6 I5 U1 |1 L
    % a) `5 C9 C1 c  j3 R
        Solving the smallest instance directly (base case).% h( e0 T6 @; |9 ~
    , s% p4 m6 N! N- F
        Combining the results of smaller instances to solve the larger problem.
    $ z7 N3 m! r0 G+ T5 I% ^9 q7 p0 L  l( F- ?2 s. R# T
    Components of a Recursive Function) t- ^+ @1 @' V9 I1 ^

    9 ^% _5 B9 o0 P    Base Case:
    2 S  ?( D# Z5 z; n6 P0 V3 {2 q# T
    4 M& N4 e" f2 |7 @% b0 t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 [& V! a6 |8 ]& g# X
    , o: I* ]$ D; a7 V        It acts as the stopping condition to prevent infinite recursion." J) h7 ~. ~& z

    2 v. `' Y5 D6 B* f- c        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.) j( p' k3 V& I8 f! C2 A" a

    5 h3 _( M6 y2 R6 A0 ]    Recursive Case:; z8 i. N& h" ?3 }/ c. ^  a( j+ r# z
    ; |7 g0 _, d% S6 [) H& v8 F- |: V
            This is where the function calls itself with a smaller or simpler version of the problem.( w% g- }* d9 [3 v% ^% Z
    & e7 l# W" S+ f) n9 ~8 f% F
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 q- |2 }! A) _0 F& t

    " e) Y, S% B& D- |* _Example: Factorial Calculation
    4 J2 k/ A$ ?2 ?& A; i3 M# M4 P! S( a5 w/ R" k0 b0 @# 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:+ v9 `$ x2 ~( c$ e( s! \  U$ R/ Y
    / i# I; D4 p! E  q( @, T
        Base case: 0! = 1
    9 Z5 z# ^8 `4 P  {) T9 w1 W& n  Z) W: B4 n6 E
        Recursive case: n! = n * (n-1)!
    - W8 b' q+ [; {  m
    5 T/ K5 Z4 Q& P5 o6 M- ]' Z: T* _Here’s how it looks in code (Python):8 z% f- U1 K( A0 ^
    python: a' l7 j( ^4 I" p/ C! X0 l' H  p
    9 w9 n, f$ @5 T! F$ ]7 V

    , n8 }+ J6 E1 Mdef factorial(n):/ l$ C, W) A* ~1 M7 Z
        # Base case
    ' q: C$ E$ `" j. M6 u    if n == 0:
    " K' {1 A& }, U" d        return 1+ l1 E2 o" T" z/ M) H- j) P, F
        # Recursive case, d; p8 N* y6 V$ o
        else:7 d9 l9 `9 Q! X# h( \
            return n * factorial(n - 1)6 y, J# q% s  b% X3 ~& ]' l

    5 n0 t: D4 f4 D# Example usage! \% X. v3 G6 I" y
    print(factorial(5))  # Output: 120
    4 d4 D* w/ W/ t& T( I/ j4 ]7 I& Y' H* ~& Z& j
    How Recursion Works
      f8 w: e; y% s$ e- r' ]
    / t! E9 ]! ?8 E  ]. H/ K    The function keeps calling itself with smaller inputs until it reaches the base case.7 G" q4 H% G2 U+ L; |

    & k: ~# ~0 f% n6 ^3 j    Once the base case is reached, the function starts returning values back up the call stack.0 Y' I  f; j* @# i- ~! M
      e7 l5 {, F& q# W" f$ J
        These returned values are combined to produce the final result.  e4 A! T6 G# }* U9 L8 Q6 G  k* X
    7 h9 v7 c5 F- n8 o- U
    For factorial(5):3 Z$ w; J4 U# d: C4 H# o
    / s) A( [, n4 A1 P; k4 v
    2 O1 k+ S. C' s, [1 f9 C3 Y
    factorial(5) = 5 * factorial(4)# ?; W) \) W1 q+ U. q! K
    factorial(4) = 4 * factorial(3)4 y/ P$ b. Y$ G. k# U
    factorial(3) = 3 * factorial(2)
    " k! n+ o+ a7 w) m# P2 Yfactorial(2) = 2 * factorial(1)/ W4 x% S( T& L" P% n% p' H
    factorial(1) = 1 * factorial(0)
    " D; ?+ l5 j% ?/ d. Dfactorial(0) = 1  # Base case
    - @- z, ]: t! [" d8 o$ @
    + r* Y0 G3 h+ J' i3 C( @Then, the results are combined:9 T$ S1 D6 t  z6 N9 @5 o4 f7 t6 Y
    " x1 N3 @* y1 a, C" g

    4 B5 u3 m+ g. V. B: o$ ?3 T  Ofactorial(1) = 1 * 1 = 1) E! g% d* k' _3 k4 H
    factorial(2) = 2 * 1 = 2
    2 ?& P; n1 t& ~# K* e, Ffactorial(3) = 3 * 2 = 66 U$ W# c8 T' o) o( ~8 e1 q, }' B
    factorial(4) = 4 * 6 = 24
    & q; T1 O  ~+ r, G8 efactorial(5) = 5 * 24 = 1200 ]7 v) d. j5 Y, ]9 [( i
    3 B& z' u4 S  j9 P& f1 t7 N. ~
    Advantages of Recursion$ H1 Q3 @" F! J( c; @' V. c

    4 M+ f0 n: ~4 C6 F! [  ]    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).
    4 d+ l, s: l) g* b. Q+ W* z' s2 h9 ]$ V; [
        Readability: Recursive code can be more readable and concise compared to iterative solutions.. |! Z9 {& K2 |* Z( \% Z' C2 B7 R

    ( X+ b% x9 E4 ~* BDisadvantages of Recursion
    ) {, ~6 l0 N% ^' k: z; p* i
    8 i  L2 ]. n2 J, j! u) L    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.3 G" l5 ~  h" a, P: L: ?8 q

    4 i% q" f* s' C4 P    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + ?5 `$ a# G. Q& R( b: a- L: j% T7 @/ ?
    When to Use Recursion8 W8 V; p6 ~9 [9 b

    / s, s. V1 F* }" N  L    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ( ?( {2 w- T; f- c; w
    : K8 W" o/ s4 K* Q, u' D3 h    Problems with a clear base case and recursive case.2 o. }, }  g2 O2 F. s

    + R0 ^0 o+ ?1 L. l& D/ ?Example: Fibonacci Sequence
    ! g4 l* V  r7 _& |& B
    8 S( J; D9 t% qThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ o) j7 ], Q$ a2 V0 P3 U* T
    4 n) ?! X9 O7 t0 i) g( k1 }8 v    Base case: fib(0) = 0, fib(1) = 1* R; w$ F# a. c( ^! c

    / z! _' o# Q5 x4 _! j4 P    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    * M' W! e( J; p7 M$ M
    # R: q5 v; }- b0 r/ }7 v6 j; g. zpython
    2 J5 U* i) |/ P: C/ h
    ( V3 n. g8 I7 D) ]# V, ?* C$ r1 G' g
    - R, u" S$ L- idef fibonacci(n):
    $ W1 D/ n2 R  J    # Base cases3 `  I& A6 h' Z' Y; z/ M
        if n == 0:) a, N# ~! p' a/ v, C/ }
            return 0
    2 c! g- |4 i6 n! _1 I; |5 v+ Q    elif n == 1:( K* W9 S$ G* \0 v' r# d' h
            return 1" R* i' G; j) Z, k3 f+ ]* @0 _
        # Recursive case
    / q0 k4 A7 r/ _' a    else:
      a  {' M5 G# y% R8 {6 V6 K        return fibonacci(n - 1) + fibonacci(n - 2)4 _# C& g! z0 x2 \
    & m% @% Y# Q4 G. r5 o' N+ C' [  B
    # Example usage: }+ E* L" h1 I# J, ^
    print(fibonacci(6))  # Output: 8
    * u! @0 r# H+ L4 L& g4 l) z7 u$ D
    / e9 H9 o4 Q% a; h* O2 a! GTail Recursion
    $ {+ r. _& ?# d: |. @5 c! a; c8 ^/ f% e" o
    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 o, N" p! X8 q; e) u  j7 [- |
    2 H" o5 I2 q. _  zIn 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-28 12:40 , Processed in 0.060165 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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