设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      F% a' Z8 S& c' v. c6 U
    6 \* S8 M9 }8 O解释的不错
    % [( a+ ?$ w' [% g9 k: R/ ^( l, A* b) \
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。8 |4 N: v% q- |/ W) q
    : V7 a- p! u0 M6 E
    关键要素, r" F3 H- ?& d' k2 N$ i( D2 l
    1. **基线条件(Base Case)**
    # @0 p5 c: R* H& P  T( p* }, J   - 递归终止的条件,防止无限循环5 L  V2 k# E6 ~3 v
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    3 h  t  n- F4 p/ E$ l
    $ ^# n: C6 c, I" `: n' S- u2. **递归条件(Recursive Case)**
      R( u: l: E3 g& x2 a; m3 \3 B9 G   - 将原问题分解为更小的子问题. P  w6 |8 L) {+ X$ v, H3 h1 s
       - 例如:n! = n × (n-1)!0 w; M: D, g  O7 f3 ]' D

    0 W5 G7 g- E' i+ J; o8 K/ u 经典示例:计算阶乘: n. x0 v1 y1 K# ^; M
    python
    $ o9 B4 v; U! P9 Y5 S* F% Rdef factorial(n):" |/ ]/ ?8 W& c- I0 K3 N( d  w
        if n == 0:        # 基线条件
    8 `8 m: S# l$ Q; Y+ `& R        return 19 k" S  ]' `% T! ~
        else:             # 递归条件% D& e5 G: u( M) B2 q9 t" p7 E
            return n * factorial(n-1)2 K- t0 ?. b6 d7 k) Y6 w% ~4 T
    执行过程(以计算 3! 为例):- |6 h" C' N, R7 w- s, F
    factorial(3)
    2 l8 b/ P; z) D: p) q, G- Z* D3 * factorial(2)
    # G/ S% T& h& u+ \6 ^; E0 S3 * (2 * factorial(1))1 j$ b2 L- ]1 a& j* g
    3 * (2 * (1 * factorial(0)))+ Y! b9 @- m+ Q
    3 * (2 * (1 * 1)) = 65 t  C2 f/ y* {: ~5 e  b# R
    1 k+ ?$ \8 _8 ~  @2 l
    递归思维要点; P; z$ P5 q+ ?
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) j* {2 Q7 S" C2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* n5 w7 [- V6 E0 N8 p+ E
    3. **递推过程**:不断向下分解问题(递)
    5 y* \, a" j5 Q4. **回溯过程**:组合子问题结果返回(归)& r% U" }) l' |' h4 _
    # r' b7 V; w' N6 |$ _
    注意事项
    8 I; ^: S8 E$ c$ b  {9 [+ J0 Y必须要有终止条件# ]& b6 b  ?2 R% R# o" }
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    % p7 K& ^, f- P1 p7 W/ `3 c某些问题用递归更直观(如树遍历),但效率可能不如迭代0 B. o. s& D/ i  ~, }+ A
    尾递归优化可以提升效率(但Python不支持)* a/ w' z8 ]( H

    ; O& c; _( w* i6 L& Q) ?1 O$ S 递归 vs 迭代8 f1 h2 C* \1 F, M0 M8 ?
    |          | 递归                          | 迭代               |
    3 z  C0 O: [# A, ~3 h0 T" @6 S|----------|-----------------------------|------------------|. q8 G+ F) S& H, Z6 D; X
    | 实现方式    | 函数自调用                        | 循环结构            |
    # j! X( o, P7 o7 U* `6 P| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |4 }9 k. G: p. A% }3 S
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |4 k5 r/ f% s4 K  I
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |  f& {# `( a+ ~9 u" ?: J

    # L7 Z; i0 F& L 经典递归应用场景$ Q+ _2 a8 a* Z+ z7 Z0 P6 M
    1. 文件系统遍历(目录树结构)
    1 e. s- _$ s6 L/ u2. 快速排序/归并排序算法$ [/ N' {5 z* g( {
    3. 汉诺塔问题& h) a0 W5 g9 W% C" g  G2 j
    4. 二叉树遍历(前序/中序/后序)
    & d, v0 ?; k, {0 C' y5. 生成所有可能的组合(回溯算法)/ j' X. P9 Q! @: ^) O4 r7 j. V' m
    # ?  E# Y3 j- B: |3 o% [' M' k- o
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    7 小时前
  • 签到天数: 3128 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    3 u! n9 ^) x) z5 q我推理机的核心算法应该是二叉树遍历的变种。; t, B# M6 N4 A2 p& Q3 i$ y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; d1 \* U. e1 K6 R
    Key Idea of Recursion3 h7 C% f- t) z1 h6 S3 I7 L

    7 \+ N6 ^. j: o5 n7 o, Y6 I$ sA recursive function solves a problem by:
    0 S6 [: }8 @" X" B+ |; B  O, i5 Q
    $ Y8 ^  L' P. F3 H" Q$ Q5 D* s6 e' r    Breaking the problem into smaller instances of the same problem.
    2 M( ^3 z4 [* f2 S  b  s) I( p/ D
    0 Y# o. q" [$ G+ O; W    Solving the smallest instance directly (base case).
    / f2 w- J# D- R9 J& Z
    ' m: Q( b+ A, f# ~- J8 \( C  Q) T    Combining the results of smaller instances to solve the larger problem.
    # K0 f3 w9 S/ E! G1 e9 m
    + G3 x9 \! L+ T# P2 FComponents of a Recursive Function
    # v, E" U/ p% O* b1 @& r5 a" Q* k( C) d6 \  F& r( e  p2 p
        Base Case:
    * [8 S$ Z# g- p$ H; e4 W0 ^/ ], ]7 B( \) l- {/ J6 c5 ^5 P
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.1 i7 E* H, y  x. d% [
    $ a  ^  G+ M/ C6 D0 n+ |- a
            It acts as the stopping condition to prevent infinite recursion.: e0 G3 m& L) a5 K
    & H- m5 W2 F# s; C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ R% }+ o2 \# k

    9 ^1 D7 P. I6 n# I1 s1 \5 }% i) z    Recursive Case:
    + [& i# J: L/ Z( E2 S3 o! z
    ; l5 L1 c2 V' ^        This is where the function calls itself with a smaller or simpler version of the problem.  E' K% P8 g( X( U

    : a4 \1 d9 S; I+ C; z' z# `0 u' Z0 T        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! h. O" V$ k, k) F9 q' Q
    3 J  G7 g* ~# D9 ]7 F& ^
    Example: Factorial Calculation/ t: u& U9 b" _+ E6 s9 ?/ b5 I0 Z

    " [6 @6 D& X1 y$ O) tThe 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:! b# K2 g9 u: t/ ~' [
    8 A1 p% p! H% A8 ^* ~4 t* x
        Base case: 0! = 17 \1 n& |7 O! M5 N6 B

    7 L2 @( `1 x" D% h& E3 F! p1 U    Recursive case: n! = n * (n-1)!6 g! k3 |' U& R1 L
    0 z3 k9 q& s  U) F% K
    Here’s how it looks in code (Python):
    + j' A- F  l5 P* {+ u; J& @8 bpython
    0 L- K! [5 Q! v
    & S5 `# \  E7 n" v8 f5 R8 ]  p4 `' D" f1 s, R3 v2 A
    def factorial(n):
    , v, ]) {3 l- l5 V/ D. T6 r    # Base case7 a  _) n* r, D  Q
        if n == 0:
    1 K" A- C! E9 Q: D  E+ ^& i) r        return 1
    ! `: @- N! R! ]    # Recursive case2 |7 |/ l# _! u* \1 m" x
        else:6 g% ^8 Z% v/ j* l# O- J
            return n * factorial(n - 1)
    * C, a$ G  v9 L2 u9 d, y2 X" n2 _( G  F7 {% ?8 d! \, Z
    # Example usage
    & }) E8 P- G# J9 Bprint(factorial(5))  # Output: 120
    6 ?: @( G: z! S+ V- ^
    ! Y# N0 ^$ t6 c: ^How Recursion Works
    6 i+ z* G; _% O4 C$ \9 W2 U% [( K$ Y1 G
        The function keeps calling itself with smaller inputs until it reaches the base case.
    7 E" r  U* p( m- g4 B! \' B( }8 X. D' z" s' l5 E! W7 u
        Once the base case is reached, the function starts returning values back up the call stack.
    / P& [, H1 j; z9 _; d+ v8 f
    9 M/ H9 ]0 f, e/ T    These returned values are combined to produce the final result.4 [: e3 Z$ `' x" d! a. w

    ) f% v- U: R, |5 pFor factorial(5):0 E( S# y$ `" Y- s$ G0 l4 P
    $ {% N. H: h, E4 M
    3 d7 a6 u0 }+ Z1 [' }
    factorial(5) = 5 * factorial(4)
    + Q0 N# R( k5 n6 y3 q( T9 Gfactorial(4) = 4 * factorial(3)! G) E7 j1 w1 j' S
    factorial(3) = 3 * factorial(2)
    * b2 B6 [( g+ v! e) @! L6 ofactorial(2) = 2 * factorial(1)
    ; Y/ T4 M$ j9 u" Yfactorial(1) = 1 * factorial(0), P. L. x4 R$ n3 J8 n4 w, n$ Y0 f
    factorial(0) = 1  # Base case0 o2 {) |$ A2 Y1 W' {+ Q2 F2 J; W. N
    / f: i7 i2 H5 B4 \. @, u8 h/ `
    Then, the results are combined:& Z) S5 F7 o$ m: j
    3 o6 ]" o& N& [  k  B" H, g% A

    0 ]4 v; ?( L, X$ A+ J9 a: _9 {% A4 Wfactorial(1) = 1 * 1 = 1- n9 n1 k1 x' u; J# D. U
    factorial(2) = 2 * 1 = 2
      [% m6 j. b5 u' S2 P, s' j: `$ }factorial(3) = 3 * 2 = 6
    3 u' C2 d4 K0 C$ v  [# d9 Nfactorial(4) = 4 * 6 = 24
    ) O- {) {& C2 C6 o- jfactorial(5) = 5 * 24 = 120
    7 |/ k5 x* \7 e- f8 X1 I
    - ]& K/ j2 A8 CAdvantages of Recursion
    $ D" U: X& c* F0 ~6 A2 Z- o. k% S
    2 u' R( T& x$ ?& S" I, B    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).
    8 B4 G, ?) E' p: v( q& _3 W  S
    % w2 {5 ?- w7 N$ f% W& C' W    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    + X/ _% S$ A/ w5 [) E2 @
    $ D# m7 ?: p$ v( {! {Disadvantages of Recursion( _  ]$ b: |3 S! u  H% a) }; e

    ) v+ C$ g, D, l( X    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.
    6 e* r2 G: b3 I' d
    ) u7 f  O1 X8 p    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
      A1 I/ N1 E2 o1 h7 S7 j  q* x, k8 L5 S- H
    When to Use Recursion0 e% O; L* P0 h+ }, k8 ?
    ) t6 s9 K! c6 e& m; \  Y
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# G6 I6 c" R( Q2 t5 K
    3 `4 x! r( E5 ]8 O
        Problems with a clear base case and recursive case.
    $ @! E* l/ P0 G( I2 h& s' C4 K) [6 f8 l& `
    Example: Fibonacci Sequence- c. u7 {$ j8 o( E# u: z
    & `: S' @6 X$ e$ _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:5 Q0 u& K3 Y9 K7 r5 j
    7 b4 E, D. a0 r; M2 |9 d8 Q) e& K4 O
        Base case: fib(0) = 0, fib(1) = 1
    8 j3 N" n9 X" ~. ?, ?- ?5 l+ h8 A
    : m: ~* y7 ^4 U- P  w    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    0 e, f0 p+ R- h* ?
    ) @3 W( z1 i( J# Spython9 i- s0 a8 \5 J3 B9 B0 p

    4 u# c. a8 Q( ^8 j0 U( N1 p4 h# x& U. _4 b/ g, P4 o0 j
    def fibonacci(n):
    - N- T( p! ?& I- w    # Base cases9 [( R, V( Q! {/ P5 E/ T
        if n == 0:
    - r3 K8 e2 m" j7 k& W/ V        return 0
    ) x5 t+ N8 z( i: H9 y- b    elif n == 1:" p* ?+ W# T- C4 |* k+ `
            return 1/ k: r* x3 ?& h
        # Recursive case
    , }" [0 F5 t" S! w+ O    else:
    7 X; W  p* C$ j9 Y8 ]6 k        return fibonacci(n - 1) + fibonacci(n - 2)
    ' s7 {+ W8 B( P
    ; H1 Y. i" B3 ]! |- X4 \# Example usage" Y$ S* X2 j% ^8 U' v0 j% w; ^  K
    print(fibonacci(6))  # Output: 8
    / i$ F: B: }5 n) b: \6 `* x7 ]" S
    % l0 z. Q( T: v) q/ HTail Recursion
    & ?* G% |/ A6 H' d- I6 u& ]
    4 u% c. C9 A" H: p$ w9 i" K  lTail 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).4 S4 {( J# p/ }8 Y& t0 i

    9 h3 K! L0 Q' _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-12-26 14:12 , Processed in 0.030802 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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