设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    : Z" g& x# `* e* j2 U! n6 A/ ?/ G
    1 V5 r( n9 u0 T解释的不错
    $ I6 \  \; g5 T3 i4 g9 a, w4 Y8 y9 l$ i
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. Q7 m; |! u! k- e0 {0 @, X- F( ^$ B
    7 m  E$ ]( w$ @1 E; r2 g, D7 M/ m
    关键要素
    9 F0 e: z. V. d% k1. **基线条件(Base Case)**. t% ^* z# D/ D$ j
       - 递归终止的条件,防止无限循环* k: a9 Y+ L  _  W/ C2 I) u5 N  g
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1) R* P6 H/ v5 p
    & ^7 O# O9 N6 Y; ~7 i$ H8 s$ v1 ^
    2. **递归条件(Recursive Case)**
    & y4 {+ l9 O( v( o* K/ p( B  Q   - 将原问题分解为更小的子问题
    ) V; H! O% S1 O# a3 n# s' `& G   - 例如:n! = n × (n-1)!. ]/ U  Q# B) [' r5 h3 a1 Q

    , F0 V# g/ t. Q9 l 经典示例:计算阶乘- ^# B; C& f% p$ e+ i7 b
    python
    * T. ]" K; V8 O' rdef factorial(n):2 }  ]5 J/ ?, |5 E# J( v
        if n == 0:        # 基线条件# U$ o# c  k' r: L3 x$ S, P
            return 1) ^( D) [( }5 s# E- U
        else:             # 递归条件
    # l+ ?) h7 W% a+ Y5 Y+ F- ]' b        return n * factorial(n-1)
    . v, g" ?  w& r. S执行过程(以计算 3! 为例):' i- X# ]5 B* D1 u" V8 F
    factorial(3)9 N: i; u0 l2 V+ J. i! q
    3 * factorial(2): {" s! `; F2 p9 F# _+ Z- t( ]
    3 * (2 * factorial(1)): h' A' w( u$ j' q$ H0 U
    3 * (2 * (1 * factorial(0)))8 Q3 A$ ^4 a( T) C0 s, f
    3 * (2 * (1 * 1)) = 61 `6 ~! a' E& M- E

    . @, o- h8 v( n% g, L 递归思维要点
    9 c/ H; u& P7 C. d1. **信任递归**:假设子问题已经解决,专注当前层逻辑1 U3 c1 R; T; `  W; F
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)# b/ x8 G$ f' G; k! U4 V
    3. **递推过程**:不断向下分解问题(递)/ Z/ \2 |5 }+ o' Z9 ~* P
    4. **回溯过程**:组合子问题结果返回(归). h8 k" P3 b0 T: ?

    ; c4 Q# k2 R; j) \: a9 a6 l 注意事项" D& k( i- p( u- M: V) U
    必须要有终止条件
    ' D- Y" J! D4 N! f. x9 A0 {递归深度过大可能导致栈溢出(Python默认递归深度约1000层)8 t7 s4 c8 Y+ D: z1 u) ?& ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    9 O$ ^& v. G8 p2 Z1 {2 k4 i; }7 a尾递归优化可以提升效率(但Python不支持)( a$ F( ^/ A2 L

      }& w- Y8 v6 f  d 递归 vs 迭代5 W4 F- w1 z1 x- V1 ~  ]
    |          | 递归                          | 迭代               |
    ; F) K8 _6 x' ]  ~2 A. l; Y! V: t|----------|-----------------------------|------------------|: B: k5 w& A+ U. C
    | 实现方式    | 函数自调用                        | 循环结构            |2 [6 f/ S- L9 r# Z' A3 P  {5 ^) e2 c
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 t- l$ v& h( H" I' W
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |( y) Z; L' _& {4 o+ g
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % a# s6 c0 F: B8 a) x3 \$ k. l7 i) G& Z
    经典递归应用场景
    ) S0 p* B2 |& d. z+ e' E4 q1. 文件系统遍历(目录树结构)0 y8 t( S+ p2 Z+ j5 A' K
    2. 快速排序/归并排序算法
    ( u. W- V1 I9 S1 f8 B9 a3. 汉诺塔问题
    & k8 e! q; s: z' d4 w* t: i4. 二叉树遍历(前序/中序/后序)& o0 E! c! E, l8 r$ g
    5. 生成所有可能的组合(回溯算法)
    " y# G8 j; c# I" G# A: e
    7 Q7 K; i, f" W% Z2 R, n* A试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    16 小时前
  • 签到天数: 2970 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,4 Z, b. g7 [6 o5 Y  D! d% m
    我推理机的核心算法应该是二叉树遍历的变种。
    3 g7 E; _, f5 B# @6 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:
    * l, m5 F4 H4 s! oKey Idea of Recursion: @0 p3 ?$ t8 k/ d- N1 p
    % g' l( m  {( Z/ n0 V# e1 \
    A recursive function solves a problem by:# g3 Z, ^0 t6 r4 k; |. }

    ' c" `! p/ S  i! Y3 w/ ^    Breaking the problem into smaller instances of the same problem.  ^, C0 n, v9 z$ n( G
    : A7 R, L4 v2 m) f: {
        Solving the smallest instance directly (base case).
    & i1 X9 Z6 B/ ^8 I5 ~9 F
    8 C4 k0 T+ {; }0 H9 {4 J/ ~! x3 S    Combining the results of smaller instances to solve the larger problem.
    # G8 h5 @4 X: Q( d  W% G+ E1 r: g
    Components of a Recursive Function+ }2 K/ T( G+ l/ W

    ! W" a- F/ a1 S2 z1 a! _1 _    Base Case:. Z: S# ]/ ]; Z% J/ `" E

    & u5 \5 o$ k/ N/ x        This is the simplest, smallest instance of the problem that can be solved directly without further recursion." m. Q3 m0 a; p! j

    : `; ?* S( c( t        It acts as the stopping condition to prevent infinite recursion.0 J0 h! {4 X$ B4 n

    % o4 E. x7 Q8 w+ s3 u        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.* L, I! D$ F  k: A; y
      r  u* T/ Y3 l* r. B  [: g6 e+ h
        Recursive Case:
    9 |2 R- {. Y4 V8 D) N" [. i6 a+ O( o
            This is where the function calls itself with a smaller or simpler version of the problem.& J4 {# D4 u( @) _9 l1 x  Q

    6 d8 t8 S% @* J" v3 n$ u, }2 X% W        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)., a  g0 F% O+ [" l9 Z+ v- H/ a2 y
    2 s, q; T6 j( @
    Example: Factorial Calculation9 K# q" q0 `5 }9 Q6 J7 f! G0 E
    " l2 \# F' l1 F; y. E
    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:
    1 g4 Y1 \( ]. l  y# P. u3 o/ M" G0 \# P* u+ l9 ]( g: `6 c. ?  i# b
        Base case: 0! = 1
    & X2 I  _) f$ |1 R8 }* q. {. ^+ O* Z0 D- a
        Recursive case: n! = n * (n-1)!/ j4 P( ]( L+ N1 c4 s) b0 Z
    ! A+ U! [' M5 L* z
    Here’s how it looks in code (Python):2 S+ ~9 _/ Y- z0 d
    python
    # ^+ ]; m6 b8 b3 R5 F) ?' m, ?: e5 O) c5 k3 c% n

    ) J" L8 [& J+ S+ z1 j) ?: q2 ?def factorial(n):  y# Q& G0 x0 h: L. N3 i6 r. l* `
        # Base case
    % C$ B  E3 b" d" n% g' R; g3 @: T    if n == 0:
    5 z# J" K8 {; L. V; I/ }        return 1
    3 f; J* ]( ~9 w2 [- }    # Recursive case2 O$ w5 D6 T$ \) n, w2 W; M- m
        else:
    # P; ?, v9 l9 o        return n * factorial(n - 1)( f# ~9 S& R- {- L' h
    6 w; q2 o# D; h5 V1 F9 P% m
    # Example usage
    . U2 d! g+ ~0 X, mprint(factorial(5))  # Output: 120
    1 S3 I0 W' K3 T2 V) J1 Y
    ( ~( U& P. _% O1 ?& OHow Recursion Works2 G. u& g# m! i: N

    1 k1 J- k, d2 }4 ?7 B2 o    The function keeps calling itself with smaller inputs until it reaches the base case./ [( c9 W  G7 U
      E# y7 x' Y) B+ f% Z, y
        Once the base case is reached, the function starts returning values back up the call stack., v2 ]- u1 T) B8 U
    , U( O9 B8 }& K/ G: d; c
        These returned values are combined to produce the final result.- p- d0 c8 }- X7 N. V( R

    : d' P# F& B7 @2 PFor factorial(5):
    - X0 A& l/ _0 K+ A  d1 l) ~7 \: e$ x0 m2 o

      T! K) k" T7 n1 V6 cfactorial(5) = 5 * factorial(4)" z* j8 p6 D; e
    factorial(4) = 4 * factorial(3)  H; a( |& q5 a9 c1 a5 o4 _) R0 |
    factorial(3) = 3 * factorial(2)
    + A5 A6 I. ?$ E+ w2 l8 R% Ufactorial(2) = 2 * factorial(1)
    0 D# k+ D, S& H0 \, N& ?8 U0 ?factorial(1) = 1 * factorial(0)
      x8 I- c5 l; Z9 G3 W( L7 Rfactorial(0) = 1  # Base case
    , o, z( d! C+ o8 f7 D0 E( H  @" @  N* o5 n. W
    Then, the results are combined:0 E5 n2 ]+ C" Z3 [8 g8 f1 u! J7 T
    7 _6 c, j7 N9 z
    , ]! e/ r1 ^6 l; Y" A
    factorial(1) = 1 * 1 = 1
    2 a# A- `' e$ ]) n+ n8 w% r* G+ ~factorial(2) = 2 * 1 = 2
    ( P) [7 N& w# e# E3 a! U4 rfactorial(3) = 3 * 2 = 6
    6 `2 X, S! r3 v( W$ E* kfactorial(4) = 4 * 6 = 24
    * |' r1 ]) D3 F. E0 Lfactorial(5) = 5 * 24 = 120
    ' e' w2 g" F8 }& a$ A
    6 K; M/ `0 n$ C( BAdvantages of Recursion
    # Y# f9 M7 ~: L$ N2 M
    / y( Y. Q& d$ F* c% W' R    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).+ O1 z0 V# v: R) o# i

    : u+ t7 U/ d( H* G# b" a- {    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , V% S! J$ d5 A, O5 u' r7 a
    / `. T2 p4 y( b4 J- G: eDisadvantages of Recursion7 S( K# c; D- o. R

    5 A, R$ p. \& x8 c    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.
    ! i3 x* a/ z# r9 q$ i' N3 ]# [7 t+ W- D
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ s; r/ n8 L, M' m% ]8 k* A2 e

    ! z" x% K' x' @9 _When to Use Recursion% z! X9 B' p& {- t" {

    6 g$ x% m9 m: ?: S& N* x7 n& ?    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    * ?2 y2 u+ P  v& R: m
    3 _. a' X6 |+ T! M* L, d/ w6 F    Problems with a clear base case and recursive case.. |1 Q/ J: Q1 d# a6 G
    % X" g& e; R0 i4 c/ k/ u$ v3 N
    Example: Fibonacci Sequence5 n% ]- N& |+ S: d: |6 ]! [" t
    + b( {4 k" P& C" T: @$ `5 _( m$ `- h
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 S: L. L/ H* m

    ! f. L7 i# K, Z0 [' K. b- R' }    Base case: fib(0) = 0, fib(1) = 1+ U3 f& ]) k0 G1 E0 z' D
    4 z% y7 S9 ?5 B( `; g
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' Z% g0 m" P6 S; H& L
    9 ]  B3 S) ?; ]: P  ~/ epython
    & t: \+ W; s! A( R5 w; a
    3 S7 V" v2 x+ f7 i. n: h6 c+ G  [0 u' h; i" I/ X2 R* Y3 X, \5 ~
    def fibonacci(n):  u5 K  w5 Z& M! u  [' y* G
        # Base cases7 l8 L5 z) x& Y2 B! }+ U
        if n == 0:
    & [/ n" I) P+ S7 T% a+ h+ ?" H8 @        return 0
    ; f7 {* w' ~) ]; o6 Q7 @    elif n == 1:/ t6 b. ^. c/ C1 J' G4 d
            return 1! Q& d6 ^3 d2 @  o
        # Recursive case
    , s  i: i$ Z: t3 {/ h3 X    else:9 [2 Y. U1 {' P+ h
            return fibonacci(n - 1) + fibonacci(n - 2)
    * g3 Q2 q6 F  {7 _4 w
    / ?; U) b$ j/ e4 U) V& u) O# Example usage: I. t" p; s8 G$ G) V/ q1 c
    print(fibonacci(6))  # Output: 8
    % s" {/ g4 t: M9 p: M6 W* U+ u: }
    Tail Recursion
    4 f* j% \: V  E" L
    ) h; [8 R& E" x! DTail 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 i+ j. B+ e& B+ i* J; A7 j
    $ o  N+ p' |! Q) y( pIn 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-7-5 21:58 , Processed in 0.034675 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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