设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    - Z7 L: B( o2 z# c" V+ c+ f
    0 J" p3 M, S2 x( G- o! l* f解释的不错
    9 A, V; t: V* x3 x/ ^% m- i' j8 u3 J6 p+ @8 c4 ^  F/ `8 H
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! k* [. U6 m7 D$ G1 t! S; J% U
    5 H+ g, m/ Z6 R6 m1 C6 x
    关键要素; `% v, A8 ?' R; C! {
    1. **基线条件(Base Case)**' i+ @  m* b' X! ]/ E1 x" }
       - 递归终止的条件,防止无限循环
    & e' J. E% M* t; X) I) u/ Q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# R: i9 |/ i) F1 D7 i9 I
    ( S) p8 Q* Z( Y+ M1 k$ O
    2. **递归条件(Recursive Case)**
      c! M$ ~9 z+ @6 {6 a' U   - 将原问题分解为更小的子问题
    ( M6 F3 m1 n* {0 h   - 例如:n! = n × (n-1)!
    2 h* ]6 H8 N2 W" l2 x" n  Z5 u' l' L5 T. ^
    经典示例:计算阶乘# |# ~% H9 S* [# \
    python
      h, t; Q0 H4 z+ C6 P/ o8 i/ sdef factorial(n):$ {. D- `: s% u- y& O" Y
        if n == 0:        # 基线条件& x1 u* Z3 _, ~& v2 g5 O, ~0 f3 v
            return 1( Y2 P# b5 h( w
        else:             # 递归条件
    ' l; _: x" q( O) i        return n * factorial(n-1)
    " ~; |. }) x; j$ T0 p执行过程(以计算 3! 为例):8 F# P0 ?8 e1 m+ }; u
    factorial(3)
    . b' D- E9 ]4 F  S8 o8 m3 * factorial(2)% _; v3 t1 ], ]% b+ r5 j8 w
    3 * (2 * factorial(1))
    8 c+ V2 D- B5 b3 * (2 * (1 * factorial(0)))! \( w' k# y7 a4 m
    3 * (2 * (1 * 1)) = 6
    4 [5 @% P9 r. s  t
    6 V) g! C6 j0 r 递归思维要点! G, X1 L1 X& @* y$ d
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    * q% i" @0 H0 k( N/ e2 `2 D2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 S8 b; n% c3 R( g3. **递推过程**:不断向下分解问题(递)$ r( {8 V5 ~) j0 [5 N/ f' _
    4. **回溯过程**:组合子问题结果返回(归)
    9 C% x$ @+ `+ i0 k+ P% I; o5 @6 Z5 @  N
    注意事项
    6 v# }: K" {) b* ^( P7 G. W必须要有终止条件4 r/ p" n0 ~4 V. @* [
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)2 u4 i4 Q! N" `0 r, J8 S0 j
    某些问题用递归更直观(如树遍历),但效率可能不如迭代; J" X5 U2 L+ y/ E. G7 r, |$ X
    尾递归优化可以提升效率(但Python不支持)
    * ~$ ]1 h: v) J
    8 Q' P! o# e. Z5 K5 @ 递归 vs 迭代. n" ?% d9 E. h; s' R
    |          | 递归                          | 迭代               |* B% j4 s2 a( o
    |----------|-----------------------------|------------------|" {6 A: @  h# L5 p1 E
    | 实现方式    | 函数自调用                        | 循环结构            |* D0 _  c$ u) i( h/ Y: o5 F
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 X7 t; ^8 y: d8 B1 M- z| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 J$ t  g- B( J/ _| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) t  i: v* W3 U5 c/ v" L; L
    ' y, ?. `! ]+ h 经典递归应用场景
    / h9 \. O# E: ?& c1. 文件系统遍历(目录树结构)
    8 J6 R6 ]9 o( v" B  v# M( K" |$ o2 Q2. 快速排序/归并排序算法0 N0 t2 X8 S/ Z0 d+ A% t) \
    3. 汉诺塔问题
    , L& k+ ^; Z2 I8 Z4. 二叉树遍历(前序/中序/后序)& \; s$ v8 C( t
    5. 生成所有可能的组合(回溯算法)
    + s0 L4 `/ O, S3 g" F1 ~' t! {0 ~3 }) Y
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 06:13
  • 签到天数: 2840 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ ^/ C: u( M+ p; a% e( m
    我推理机的核心算法应该是二叉树遍历的变种。3 R% i3 P8 V  f5 p4 m! c1 @5 h
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    9 C$ C6 u1 w: }# q. ^$ eKey Idea of Recursion
    ; U8 y! _3 l# j) y, D1 v/ e6 |: u3 N0 i
    A recursive function solves a problem by:2 p8 E" [/ J/ P! Q6 c4 @

    6 g" I4 Q3 U- ?# x# P; J, d. ^    Breaking the problem into smaller instances of the same problem.
    * Y5 k' F* F6 g) B) O
    & D  r' I1 t6 ?. V, N. P2 C    Solving the smallest instance directly (base case).; C) }" H) |+ @3 z( |+ y+ U
    ' I3 v! w& Z8 k, A$ z
        Combining the results of smaller instances to solve the larger problem.4 G. d3 L3 Q. f" m, `, J

    , X2 @/ h) K5 p- u4 i. P3 [Components of a Recursive Function
    % b$ q4 b3 `7 \: D8 {7 r7 G) d: h0 I! u8 n* v* z2 [+ P
        Base Case:
    - l) [: \' @8 }6 J: U% N
    0 ^# b% A0 X) C9 p, Q& r; M% P) t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.$ _- O9 T1 y& i

    % |: R( R5 ^" }: p' Y        It acts as the stopping condition to prevent infinite recursion.. ~" {$ r2 ~- `
    2 R' e/ P; n0 x, q
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( y" O' d2 b6 |1 v' W1 z0 E0 h/ s$ Z( F
    % I4 D- r. s1 h+ P
        Recursive Case:/ E/ Y; H( b9 H. N

    8 n* _, X* K* }% h        This is where the function calls itself with a smaller or simpler version of the problem.2 R& f  b5 B$ b: v6 {. t

    & U& ^+ \4 _! S' |        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).. [! _; g* U# x6 z& G# r$ i! g

    4 Y  d4 D- v& I7 UExample: Factorial Calculation
    8 `" X  y7 R3 I% W, x  g7 n6 `1 \) c* k# l* p+ a2 S
    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:
    9 |: @- H6 c  M$ O+ `) V7 x0 P  F+ \8 k% B
        Base case: 0! = 1" i' W- d6 k- d4 ?5 L

    . n. N# S6 j2 F" G. c" s+ q! b' V    Recursive case: n! = n * (n-1)!$ V7 n& d; {, F. q
    - }5 S! E5 I& ?( Y
    Here’s how it looks in code (Python):
    ; C' I# m1 i/ lpython, A2 R# [) @$ h' r/ Q
    / ~# c) s' O7 r

    $ b9 v$ D2 X: ldef factorial(n):
    ! M# ~8 D/ W8 c2 J( x2 q+ Z    # Base case; [. n- `4 a4 }9 s# g
        if n == 0:
    " \8 a/ C4 L' D  V3 p' m/ M: t# v- X        return 1
    $ g" X$ u8 y8 f! l3 _    # Recursive case
    7 E/ B4 j, c/ _, ^) g& }    else:: m% \5 N! u5 f
            return n * factorial(n - 1)# j1 \: q* U4 A  z* G7 r. H; f; l

    9 K& z' n9 I# N+ I1 z# Example usage
      |9 L0 x* ^: p8 Kprint(factorial(5))  # Output: 120
    ( f7 S3 k( {, A4 F
    ' n$ a" b1 h& F% N' A1 h7 s+ PHow Recursion Works
    7 p3 `* x( P0 `# V2 M
    & a" G6 t9 a5 E3 m, t    The function keeps calling itself with smaller inputs until it reaches the base case.( P, z) h* ?2 @! W& b4 ~1 u
      t. ?9 U0 L9 s( _
        Once the base case is reached, the function starts returning values back up the call stack.8 x& U9 `! \3 i8 A7 z( [

    8 y1 v3 x# y. g+ Y    These returned values are combined to produce the final result.
    9 V" p4 v' ]2 y, k
    " \, [1 @) c0 L0 d8 JFor factorial(5):: Z) e' \* D' H$ L9 b, ^
    / T/ ^7 D9 v4 m7 ]# {4 `3 o" c
    + R, o% g0 A7 f
    factorial(5) = 5 * factorial(4)
    0 L5 H' R/ o' n, V1 M4 Ffactorial(4) = 4 * factorial(3)
    ; u- c# g2 i& R4 }9 k- o* w% ofactorial(3) = 3 * factorial(2)
    3 y+ ]3 l+ D8 K6 B5 t1 Q+ X- n+ c  F; [8 Nfactorial(2) = 2 * factorial(1)
    5 i, o* m+ f( _' o. Gfactorial(1) = 1 * factorial(0)
    + k" x, _( P0 T4 O7 Vfactorial(0) = 1  # Base case
    $ w) n% @+ h5 E$ S& M, |+ q' `  M2 e$ {; [9 B
    Then, the results are combined:$ N% n2 C  \$ K& I; P1 u# h
    ! J2 v- u( I9 x- L. I* D& t
    # o  k$ I8 t1 ?3 S$ x; h- w
    factorial(1) = 1 * 1 = 11 V' v- Z9 ?) T+ m' g! x
    factorial(2) = 2 * 1 = 23 C2 K  p' B- y$ Q+ {! l! H
    factorial(3) = 3 * 2 = 6
    9 S7 `) }  a4 R( h2 zfactorial(4) = 4 * 6 = 24
    ' L1 M! m* e1 `( a+ e8 Mfactorial(5) = 5 * 24 = 120
    / k5 b$ y0 M3 N
    , _" _8 F: k8 sAdvantages of Recursion
    ) \8 }) l" l. p% F) h) t3 `4 M. ~, W& R: z5 M6 _( j& G! ^
        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).
    ; G( u4 u" v% m3 T- ^9 i3 R* R, ~! j/ d0 V  j$ I
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; L8 D; O+ R% k; `' ?, @
    2 Q! I2 ~9 e+ v. j2 b/ J3 uDisadvantages of Recursion* w! I5 n( n- }) `' p3 C$ I6 Z) ?: ^. ^

    % e6 l, e: m9 d7 e/ f    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 K: ]2 |) I, W1 u( z

    " h) l- ^* H+ {# R0 h: |    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& W$ E- [6 [3 q, i; Y  ~6 D

    $ v, k& `8 @  x% L/ T$ gWhen to Use Recursion' a1 }7 P" Q8 Z: k

    . e; M- D0 z' m    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ' ?0 ~3 b, t  X) ]3 T# w
    ; k8 e2 Z9 M/ C) `3 h    Problems with a clear base case and recursive case." p% |4 W+ X7 ?) P# p6 F

    0 p$ S  F% t; U& QExample: Fibonacci Sequence
    4 t; V) `- V9 a7 C, X. d* n: K
    ' O0 n, [9 t8 m6 UThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: J/ E7 r/ R2 u) S
    + l9 ^+ \8 l9 g1 {! a
        Base case: fib(0) = 0, fib(1) = 11 Y+ d4 l9 q$ \# [
    / F& u' ~  }7 I1 O& j
        Recursive case: fib(n) = fib(n-1) + fib(n-2)# g' S" H6 k: O1 c* w2 P

    " P4 u. c3 l/ a5 P5 R2 `. opython3 b& Z* X/ H- x9 A3 ~
      c; p' i  n7 ]8 |3 ^
    8 J; `9 Q: [5 m) \2 L, ^. Q
    def fibonacci(n):* J/ G7 U# b) F
        # Base cases# H" h) M4 a9 f3 k# y: @$ C
        if n == 0:& C3 h: G+ h. w. H: N, [$ T' J- j
            return 0* R3 m/ D" F: Q+ ?' w, J% F
        elif n == 1:5 d* Z! b' C1 o) S! W" g1 E4 e
            return 1
    ! J: J" ]( {# {. `1 Q    # Recursive case
    3 u( @! G% m; S  j    else:
    & U) L: N6 y  U+ e        return fibonacci(n - 1) + fibonacci(n - 2)/ T5 F  Z3 _5 h( K& b) E7 ^

    5 i6 \! Q8 ]4 O5 i# Example usage5 c) |0 o- U" K" k
    print(fibonacci(6))  # Output: 8
    9 R( V2 V3 w4 a  O7 _2 L
    2 H$ W: m; v2 b0 G# {; b; u( u2 STail Recursion
    ( C. X! V0 d4 a) I
    ; _( ^% j- Q$ v( N- D5 |! \( GTail 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).2 h; S, s% j4 l5 n5 ]+ ?
    3 R; S) S$ G4 {. A  D6 Z
    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, 2025-2-23 01:19 , Processed in 0.038045 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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