设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    6 m  f4 G2 Q7 c' [. P
    ! T4 {" t  ]( I解释的不错
    2 j. n  A2 u+ G: s3 V+ y( F* |; G
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ }/ b  ^7 }8 O! i

    $ r4 D, q2 L+ E+ F7 x& U, W3 Z 关键要素
    # ~3 v( ?: I' J& v1. **基线条件(Base Case)**
    % A3 H+ n3 {: B6 Y+ p   - 递归终止的条件,防止无限循环% T2 L# f7 P4 h" R* [' ~0 N
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 14 {# C) ^0 Z: o; k
    6 Z  k) D! k" O- r& b- \* H# Q. j, Y
    2. **递归条件(Recursive Case)**
    . P2 \9 h1 I* W" z$ W: X   - 将原问题分解为更小的子问题
    5 i$ I1 R6 S' J   - 例如:n! = n × (n-1)!
    ( M( U: Q7 F' X. ?: n  D- x6 Y5 z1 Q9 N& |9 ^* m
    经典示例:计算阶乘
    . L6 r* _% h+ x* ipython
    + K! j+ G) _" ]/ i0 Bdef factorial(n):
    8 a: o, m2 R: }) f$ S- v: M8 W    if n == 0:        # 基线条件1 o# d" U9 m* _$ n+ _
            return 1
    " V5 ~7 w9 `7 }0 ?  z% D    else:             # 递归条件
    . M& g7 C" V  v. O8 K* }1 n& v        return n * factorial(n-1)  }  u5 F/ T8 B
    执行过程(以计算 3! 为例):" a" ~8 M- m: f
    factorial(3)+ J; Y$ C, r& _/ \6 ]8 k8 Q$ L
    3 * factorial(2)# m4 ?2 \# [4 K: s$ j+ Y2 o
    3 * (2 * factorial(1))% t2 ]+ y  ?6 F+ f# B. c
    3 * (2 * (1 * factorial(0)))! _: z/ q6 r+ z. Q3 A2 a. @! K
    3 * (2 * (1 * 1)) = 6" V" h( N. e% K: p

    3 v# b( Z: }! P  r6 g 递归思维要点
    " P! P# e0 \7 j" @: R1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    $ F; N  n/ S3 k: y% y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)/ M- S) _! T$ E& E6 u" h8 s1 s
    3. **递推过程**:不断向下分解问题(递); y4 u4 ?: F2 f% R, e1 g
    4. **回溯过程**:组合子问题结果返回(归)" f, d1 W$ M0 `1 [

    3 o1 U$ e  b6 O- I" [/ x8 V! E 注意事项
    & H1 H2 J( K+ F+ L+ T% {: K必须要有终止条件
    4 P4 l4 b% d% L: Y- B8 }9 `递归深度过大可能导致栈溢出(Python默认递归深度约1000层)2 n) ]& a  v1 L
    某些问题用递归更直观(如树遍历),但效率可能不如迭代& x. }9 F8 E; r0 _3 F; j' P5 d! ~
    尾递归优化可以提升效率(但Python不支持)5 `5 D6 J) W) `% I

    ! Y4 S/ \% U" }/ x- ~& p/ \ 递归 vs 迭代1 b, \& K8 m7 q- q
    |          | 递归                          | 迭代               |" R7 d. }. w: x0 R  T8 R: u1 q
    |----------|-----------------------------|------------------|" r% k2 s7 W% g7 h& v
    | 实现方式    | 函数自调用                        | 循环结构            |
    $ d$ I$ V! ]1 C! \7 j| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
      U; s0 O) r9 @/ g1 d# G. J| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |  t8 B4 V- y* f$ m7 g# \/ L
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    & U, O# Z' |0 Y0 B6 e1 |
    2 S, A, \8 m( L  z2 h- Q 经典递归应用场景
    2 B# n* O" Y: Z5 b' n1. 文件系统遍历(目录树结构)
    5 n, R4 C  |) O8 g! D" l1 [2. 快速排序/归并排序算法
    # V6 A+ N" ?! D* @3. 汉诺塔问题
    ; X: \9 x& l3 ?3 D% k4. 二叉树遍历(前序/中序/后序)/ I1 K+ P* c# H# Q
    5. 生成所有可能的组合(回溯算法)
    , R. e3 a8 o' k. ]$ {. n; ]1 p
    1 w- Z  C6 V- h8 w, m试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 3108 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ [$ e1 v% I/ J* m( l
    我推理机的核心算法应该是二叉树遍历的变种。
    + t& @2 z4 O" Q( {  M另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ) Y3 c7 ^/ O" YKey Idea of Recursion
    2 w) |! r  K- h! x. r. v5 p) a, M" x. _% }
    A recursive function solves a problem by:) c$ M% D; T7 y) a4 T
    3 m- a2 w, R& Y1 u; e
        Breaking the problem into smaller instances of the same problem.
    4 Q2 r! H! e2 C5 n! l7 r0 p- b- a( \' f- X& i/ x! s" }* _
        Solving the smallest instance directly (base case).$ {8 q; `8 k* r" ^

    & t, ~6 N$ [  J4 D4 d    Combining the results of smaller instances to solve the larger problem.
    0 [8 V7 \; H9 ~+ d7 }6 [5 b$ e/ U
    Components of a Recursive Function
      W% i  X7 n" y/ y8 a. _  q3 i: f
    ' u& u) U+ H2 i6 Z; r' P0 q  D    Base Case:2 L  L1 Z7 _3 q& P- ]
    / Q/ N- w4 F! l0 L( {
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.0 N: [* U2 K5 G

    + }0 w% \* X$ K5 A* r% J; I% |        It acts as the stopping condition to prevent infinite recursion.
    ! ^  F# p# }* v, ^. d' g1 x9 {3 ]$ t2 C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    3 _  Q6 ?4 V; V+ O% c& e4 x9 i7 E- g. O% K
        Recursive Case:
    ( ]. T, A+ Q# U( [' h; T2 o. a: M0 u+ M
            This is where the function calls itself with a smaller or simpler version of the problem.
    - J  D0 B) O2 B' ~4 ]; |
    $ ?: e+ E4 @& U& `5 ?) g! z2 g        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
      n, l  W9 b0 X/ e* m4 q1 S
    / Y4 S; |2 C6 [" B, XExample: Factorial Calculation
    7 u% |0 F% G) e; I% T9 j' r0 R1 S' ^3 }; F2 ~. u
    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:
    8 m' `* q, |7 g/ y0 i( T# S6 R4 \( z" Y+ M- y
        Base case: 0! = 1, w: L6 v$ D* g7 F
    * B% v" N( m) l. v* h
        Recursive case: n! = n * (n-1)!
    ( m* ^# i3 O7 a; I# q# `9 e
    * Q. W& h; S5 LHere’s how it looks in code (Python):+ l% o3 J$ A. \1 O2 H; l
    python
    , `3 m2 O' b2 ~  G& B8 H0 ]/ O$ _6 A0 C' s2 J

    + c( e- Y; |. g2 {* H2 R, s  Hdef factorial(n):% P/ ^) _+ |7 g& ~5 U( J
        # Base case4 V3 @$ x" D/ \+ i+ H, c1 W# o, }( j4 t
        if n == 0:
    / P/ ^) E3 O3 O5 G% \, K        return 1+ y$ G  }$ h, J8 U7 D
        # Recursive case
    2 ~/ P" R: |! r! l; b    else:1 A9 I, U# c7 v2 k. c' N# J
            return n * factorial(n - 1)
    5 e  C, S# L8 ~+ A( s8 Q$ q) e1 v  I$ d' w) o3 u( N# f" D. x
    # Example usage
    ) N' q! T+ V5 L) [; J) x0 Jprint(factorial(5))  # Output: 120
    3 e% ~8 w0 K9 U& F  {, r. R/ N% T2 Z- I" ~2 t9 }* e
    How Recursion Works- D; [* e2 S7 q# p

    $ z; ^: J% A( p$ r( L" {9 p    The function keeps calling itself with smaller inputs until it reaches the base case.% T) f. N. Y5 \* z

    % p. |; _& M* l7 s! q( E% N. m    Once the base case is reached, the function starts returning values back up the call stack.
    # s; k) L: P" K! w% C- L0 Z2 u( V; h/ g, |( f$ E0 E9 u# G
        These returned values are combined to produce the final result.8 N  n# W/ d) W% r* d$ i

    9 U; j0 R8 ?' O* WFor factorial(5):
    ! U5 o% _9 W8 `1 }
    ' n' B. t8 X0 w9 E- k! e1 ]1 d' m5 a) U, d
    factorial(5) = 5 * factorial(4)2 t  d) ^5 b* t) \5 u2 @" z- H
    factorial(4) = 4 * factorial(3)
    . X) G+ r' T: Yfactorial(3) = 3 * factorial(2)" e! k" A5 I2 ]6 D
    factorial(2) = 2 * factorial(1), W3 K, _( t7 X( d
    factorial(1) = 1 * factorial(0)
    % \% r5 h0 S# M# e% hfactorial(0) = 1  # Base case: _# f$ [. s8 r, x1 m

    4 P9 a5 @3 N! ?: }' Q" M$ jThen, the results are combined:
    ( F1 D0 e( x! ~. D! X4 N$ X2 `, i# Z- b5 h, Q5 Q, w3 y
    / n' i9 ^( y& y. R, j/ ?& }
    factorial(1) = 1 * 1 = 1! }- p  a4 E, ]2 g. V" p) m' i
    factorial(2) = 2 * 1 = 2
    ! ]6 e: Y! E' Ifactorial(3) = 3 * 2 = 6
    7 b8 }, h9 O0 @9 R$ x9 g" cfactorial(4) = 4 * 6 = 24
    * L$ U8 {+ k; dfactorial(5) = 5 * 24 = 1208 f$ a) z0 r6 p8 ~

    $ Q. d  H# D7 x" jAdvantages of Recursion
      P4 @$ I# j& }/ y4 b1 X" v3 i+ }4 t: B* u, U! 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).) d( b1 l4 ^2 ^8 ^$ Q" F) {
      L% {' v2 K  k. w' O# d
        Readability: Recursive code can be more readable and concise compared to iterative solutions.8 m& q5 O/ L* b  q: V9 v

    ; m& O$ x: `5 p; }2 s1 \' {Disadvantages of Recursion2 }) `! r# P9 ~+ Z8 F
    9 [/ e9 P/ V0 _: [& P
        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.
    ! E" E# T' ]0 Q/ Z. s; x: e' y. J3 B
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 r$ b# F, w& n" u
    ) S8 K5 _4 c$ E, u
    When to Use Recursion2 ]: r6 @1 G* w0 F$ k
    6 D3 \; S- F8 R, V! S, R/ T
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).* ^+ w. p7 F' U

    4 J. H! ]* E3 e& p+ d9 J    Problems with a clear base case and recursive case., h+ k/ ~; C4 [" q% {' Y

    3 L. |" X: R& x! q/ B/ m- GExample: Fibonacci Sequence
    5 M6 m8 N$ x4 i% l+ L& b% t' P6 g7 C2 V, C
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:' O4 K" a8 Z; \  h
    0 @5 Q' n) ~' {  J% G  c
        Base case: fib(0) = 0, fib(1) = 1
    6 y5 e1 g0 G& D
    ! [9 m" c5 A; C) B) }( Z5 ?" }    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ! P4 w9 w2 t# A; N' Y% E4 C$ T/ P9 i' ?7 a0 p2 a- d
    python
    : Y# o$ A% |& r' M4 Q1 V/ r, U. w3 ^3 H* [4 d

    * d/ B* Y+ ^( }' u9 j# C  \/ x' v+ z7 D/ zdef fibonacci(n):# f" d3 O0 J) Y( u
        # Base cases% `- v7 a4 C8 w# a- P
        if n == 0:
    + n+ g; }# ^6 d8 Y        return 0
    ( R# l2 g( M* j, e$ f+ X    elif n == 1:
    ) k5 Y4 V, F' w" X% u5 o        return 1, k$ W) H. O: K8 O- i: D5 o4 N; \
        # Recursive case/ M$ B. W: L+ B( N  n" R6 j! ]
        else:4 U& V/ w  B/ _4 ?' `  i; Q
            return fibonacci(n - 1) + fibonacci(n - 2): @; f( P0 n2 Q+ B- h2 @8 h
    5 C3 C' s8 B9 W. ?- O# o7 [
    # Example usage9 |2 r- u' J; C/ k8 u& i- a
    print(fibonacci(6))  # Output: 8
    1 r, ^; q3 n: S
    ) m1 }$ C% J  X" E& sTail Recursion
    7 U. m( T8 E$ N7 Q  V  u% ^
    : X7 v) P8 s* N' v* `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).. b4 {2 X6 j5 F% ~, x& z. r' o

    ' b" [$ S* u& J5 ~% o. ^. OIn 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-12-4 23:16 , Processed in 0.032089 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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