设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    $ I8 }, E; k" i
    3 o: r$ ?4 ^" U$ @6 M1 j解释的不错
    8 g+ r$ b# J' e$ L7 H- f# o
    9 s# G+ w" t7 A* \! U" I4 Z递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    8 |$ H/ ~" |+ x7 Z1 k' E4 w0 F5 l' x9 r9 @9 A) }
    关键要素
    1 u" o- F2 N# f( @4 w& N3 q1. **基线条件(Base Case)**# A, v0 C; X, A. }+ i6 u
       - 递归终止的条件,防止无限循环8 k! \0 P0 X* X4 ]6 `$ ]+ n+ u
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ) _+ X+ a( N2 ?: A+ }* o0 T9 h, j/ g9 V( P* l2 }2 G0 s' e# U! n
    2. **递归条件(Recursive Case)**, ]) X$ V% c% m% y, H; ^
       - 将原问题分解为更小的子问题4 U$ j6 p$ U, }7 w
       - 例如:n! = n × (n-1)!/ e4 h& s0 G+ v5 {

    $ w4 c5 v. [8 n3 T6 q( R" q3 ` 经典示例:计算阶乘3 A8 u+ C1 x5 E8 j6 N, L. M
    python2 ~' _' V# |# v+ v+ o8 ?
    def factorial(n):
    / l: S. M: K3 e0 J% A    if n == 0:        # 基线条件
      z$ X6 ~& c" I; _/ d% `, w        return 1
    + P5 p# h( @4 w' p  N% B    else:             # 递归条件* G( L# l6 y1 G0 e) k
            return n * factorial(n-1)
    9 w  {. `2 W" n) a执行过程(以计算 3! 为例):
    - x5 p  ]% c$ y" ~factorial(3)
    6 ~( W3 D* }) h3 * factorial(2)( i, @$ w; W6 E% M9 Y
    3 * (2 * factorial(1))
    & i# c- V0 m1 V1 }5 I! R3 * (2 * (1 * factorial(0)))
    # K8 {, t6 W8 L0 G3 l) g3 * (2 * (1 * 1)) = 6
    ' O' d* h9 o/ q5 h( K# K+ c. m- u. \+ f
    递归思维要点
    4 v& K% w8 F# T' N' w1. **信任递归**:假设子问题已经解决,专注当前层逻辑+ S0 G5 a2 C) j$ o3 U$ f' ?
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)9 T  i) `# o  V* O8 g7 C) F. o% B
    3. **递推过程**:不断向下分解问题(递)$ ^7 A7 f: w1 P, X( J- f) [
    4. **回溯过程**:组合子问题结果返回(归)
    $ ^1 d* e0 q) X% D# ?- k$ u& A  q; \% B" Y. f: v9 U2 U
    注意事项
    ; z3 B+ e! Z/ n, _- u' x必须要有终止条件
    7 t" ~2 ?8 R0 R3 j! |- Y递归深度过大可能导致栈溢出(Python默认递归深度约1000层)' X; f2 X# D8 e# U4 A. o
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , A- a7 S' l& e6 r) B9 b% y尾递归优化可以提升效率(但Python不支持)
    , X0 B1 \3 p# y8 @+ g- O
    : o1 {/ J4 \( h# Z) z8 I4 x1 ? 递归 vs 迭代$ c0 _6 R: R6 }
    |          | 递归                          | 迭代               |
      G! N# \( s' `0 q. T7 Y; ]# x|----------|-----------------------------|------------------|# A. z* H- P) |1 z5 ^% t% ~4 f
    | 实现方式    | 函数自调用                        | 循环结构            |
    $ X& V/ r! D: V| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    - W0 K/ M- d2 E" u| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |, m0 C% L! [, ]
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 r) @+ |8 \7 N2 z

    ) x1 Q: G" t5 S4 s 经典递归应用场景7 V1 @5 C( X0 b; C& Q
    1. 文件系统遍历(目录树结构)' G3 r) `5 L8 w
    2. 快速排序/归并排序算法
    $ V% \0 F) V; b$ ?3. 汉诺塔问题+ K1 Y8 O* Z5 r2 n# _, ^& a& Z0 F
    4. 二叉树遍历(前序/中序/后序)
    * k8 A3 h- ]  e' o9 _5. 生成所有可能的组合(回溯算法)' @; v" p; t1 I: f2 G# J

    7 g6 \& l$ m/ z/ h# d8 q5 ~/ X+ [试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % f3 j- j9 ^0 `4 f* V& u/ p我推理机的核心算法应该是二叉树遍历的变种。5 S* P) P2 }/ Y* q4 ]; ]& I4 W' 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:: D' ^, a4 N6 h) Q% ~6 i& J
    Key Idea of Recursion
    7 [6 T0 l' D# O! T1 n5 l' U- d% {3 H& d2 j/ H0 d
    A recursive function solves a problem by:6 P3 d6 O$ \+ M7 ~. p

    0 V1 t& s/ ^4 l( u    Breaking the problem into smaller instances of the same problem." {" Y$ ?& o: ~% m( y% p# L# z0 k
    * P  M2 j' F! U- f: @2 L# D
        Solving the smallest instance directly (base case)./ `* e; D- K% q5 k; {

      e3 m3 K& \% ~- _    Combining the results of smaller instances to solve the larger problem.
    , _0 P  [8 V0 {- L# \, {
    ( }+ }' B% ?. z- {+ \- zComponents of a Recursive Function5 A) ~* U$ S) |2 M& N
    - _7 U4 i9 p7 z# W
        Base Case:
    1 D2 X, L% T1 G5 D4 f. {: w4 a6 r% _  N
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 t) T" E# S3 c2 g+ a4 c  @2 |/ t3 ]$ c% s
            It acts as the stopping condition to prevent infinite recursion." |2 N+ [! e0 Z  @

    - v& ]% s; O# ~3 Y: e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - n5 W9 [$ T2 F+ }( h8 |7 f; R" ]9 m( r. C% a
        Recursive Case:
    - v! b/ \% m7 [! j/ j9 o: _  K6 g( [8 o1 D- u! _6 `  q
            This is where the function calls itself with a smaller or simpler version of the problem.
    0 [1 n( E% g' q, B9 u/ w% j  k' E% W) P+ ~. G9 U, @
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).8 U; w3 W; N5 L. ~4 ^/ Q% Y0 @9 |

    ! h, |2 {# K. z5 V  GExample: Factorial Calculation- H% h6 f8 b' o' j: F& P

    ' S: ~) a& A/ u& G8 A# 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:
    3 @4 i7 ?6 I/ X$ I3 d
      K3 t) d0 W5 k8 E: M9 s9 z    Base case: 0! = 1# x/ l+ D; ]( ~) C% l! x9 ]6 S

    ( u6 t( N6 W9 ]. e5 l* ]2 ]    Recursive case: n! = n * (n-1)!8 ^' c  G& s7 x" S, R) Y

    " G& z5 b: q% a9 u* nHere’s how it looks in code (Python):
    2 v( j) A4 Z' Wpython
    ( G3 |- z+ w0 o* X
    + k0 H, Z: Q$ C( {: n& d. q8 V) c$ s! G
    def factorial(n):  b& a( z/ r  I
        # Base case
    1 N6 R8 F: z' F5 W. N# |( z$ C) N    if n == 0:; w! Y0 f2 z7 N8 F  a5 U1 H
            return 1! Z+ `5 k9 }4 \* P1 b
        # Recursive case. l) [7 L1 e; W
        else:
    ( W. ?5 P4 P! ?* e& z1 m- ~# t        return n * factorial(n - 1)
    ' Y9 a' u3 `/ y2 g1 o- @
    2 d1 t0 H' d& J, c# Example usage9 h5 d% R# |0 f' I% G* k% D: {9 {
    print(factorial(5))  # Output: 120, A1 A  x8 ^! H+ O" c
    2 N3 d# _' l1 v+ w- l
    How Recursion Works  a: K4 d/ y) W6 L8 J
    4 a' p1 e8 F0 n) q
        The function keeps calling itself with smaller inputs until it reaches the base case.& `& x1 ]) P  P  U) s: s

    ) q, v  E" C- ?! e    Once the base case is reached, the function starts returning values back up the call stack.
    2 z8 `: v% {+ Y' L+ u3 {
    # Z- s' Y1 |# ~    These returned values are combined to produce the final result.$ H9 ^0 a* Q+ q- V( {

    $ q2 A" }/ u! P5 x$ mFor factorial(5):
    9 `2 W/ q1 f7 s& c6 F& \' e. `. O+ w& p

    ; e. i6 T" Y  L% _) l; L# W- Gfactorial(5) = 5 * factorial(4)3 Z, I' z; p4 w. R
    factorial(4) = 4 * factorial(3)
    ) L. ?* t8 J+ m7 G$ N! R3 [factorial(3) = 3 * factorial(2): F) @( f) F9 V$ j9 B; I: f
    factorial(2) = 2 * factorial(1)
    ! P: G' T, l; {& C3 gfactorial(1) = 1 * factorial(0)) a! X. t7 k# y6 N# I
    factorial(0) = 1  # Base case
    4 m) E6 A9 w! x& N) g7 x
    9 t2 f9 Y- V: p; A: p* ^6 UThen, the results are combined:1 l9 @& v  ~3 H, r0 }8 G% @
    6 O6 l+ T1 H' T% I$ y

    ) E- S) c1 b6 w  w8 n* k% t6 @3 hfactorial(1) = 1 * 1 = 17 {# V4 l, r  _9 w
    factorial(2) = 2 * 1 = 2
    9 R' r' O+ S+ S9 j  i: P& u% kfactorial(3) = 3 * 2 = 6! p+ O# l+ b+ h
    factorial(4) = 4 * 6 = 241 B9 c' Q+ J- l$ v4 ^/ o6 r. I
    factorial(5) = 5 * 24 = 1202 @; E& Q4 P& j8 O$ L

      _4 O7 I3 \; e( qAdvantages of Recursion
    $ N8 ^& V; x: {1 m4 B/ F, }7 ?) ^" b) R$ J0 t& v
        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).1 [4 {. t( I% U1 F. p2 D1 a
    , \: |  h7 K1 Q$ M; P
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    + X& ?: s. K4 ]% c( P
    6 J4 }* e7 m* G9 ~& f1 T9 w5 wDisadvantages of Recursion
    + e) b3 [6 M# @5 e3 A, a- p/ G- s
    2 w, O7 N9 E4 |, v+ X3 t) B    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.* n; ?& f/ ]6 j1 F* }" j, U! n9 E! h5 [

    2 M# {$ g, B; N* m    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 D4 f6 x1 z0 a
    4 l! g* \$ h4 C8 N+ M/ E; c
    When to Use Recursion3 p8 s; f6 g1 S4 w: \& W  w4 r( q, ^4 @! m

    ; |9 A* N3 E$ {6 |& a" b; ?    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " u8 C- H7 u: x. s$ F$ \2 j; X
    + ^8 l. Y- q1 H- I9 N6 X( y    Problems with a clear base case and recursive case.
    ' v2 e8 O1 x8 T# ?5 |9 `: o0 M) @% H% m4 m' x6 m) A2 x
    Example: Fibonacci Sequence
    , ~" t7 ?2 B1 Y8 e1 D- j4 Q" F9 D- C% \8 ~
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    * d& T" p, C% B- V4 J. ~
    8 l& E5 h; e5 k% U3 s    Base case: fib(0) = 0, fib(1) = 1
    2 Q5 W" b. c5 a  {! ]+ S0 e$ c. J* v3 h9 l2 S9 Y! j( G
        Recursive case: fib(n) = fib(n-1) + fib(n-2)4 q  N( N. X% n

    7 Z" I1 ]' ^. Ppython. m) u# e* \9 z& Z

    ) v: E6 a1 V* |8 N3 k( _+ y5 i. u/ G5 Q; T- r2 j8 n: Y2 ^& t
    def fibonacci(n):
    & e" \& k5 Q0 ]8 R& X: h    # Base cases
    3 W0 b) a2 x6 R0 q9 J" D1 ^    if n == 0:0 W1 S5 c; I& O/ t
            return 0
    4 e4 C$ i) b6 g  M: t* C    elif n == 1:
    " a( ?7 o+ m4 K% N9 M, S) V        return 1
    9 z" b9 d: ~: S8 f: z9 d4 ]    # Recursive case$ a; z+ t% g9 h' n) C
        else:0 m$ V: f) o5 Y2 c6 y& G: o# M9 ]" ]
            return fibonacci(n - 1) + fibonacci(n - 2)
    3 K& k8 U' X8 M5 M- @- I5 f: {9 W. ^6 K1 Z
    # Example usage
    $ y6 T  o! H. x5 Y9 ~2 n& kprint(fibonacci(6))  # Output: 8
    : b& v1 Y4 Z+ Q* {" w
    6 C& s+ ?6 Y9 p4 p3 t* I3 KTail Recursion1 G; E2 B1 e9 q. e& _, k! x
    6 P$ g( f. q3 ~, X9 v% 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).) `# e% A8 p( Z, ~

    * c& D, B" D1 ]: l! l2 sIn 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-26 17:29 , Processed in 0.057026 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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