设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    4 \) T* i  {# H5 V, e; T6 P( }, F" {2 y( |
    解释的不错- h$ T% C5 ~2 h& N: \: C

      Q, {' @9 m. P5 t递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' e3 p- @0 V1 ^# Q

    $ A+ g0 T5 n/ _/ \: A) H) L 关键要素2 i2 ?9 O8 Q# P4 W8 ?
    1. **基线条件(Base Case)**) l& K. Z$ I9 U" i' b
       - 递归终止的条件,防止无限循环
    5 J) W8 @0 V; D( i   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    # Z% p5 r# M1 L- `! A
    6 o& ]+ Z8 q7 q( q2. **递归条件(Recursive Case)**; ?! h8 Y( y8 N% z: H! \2 N# v* p
       - 将原问题分解为更小的子问题+ y4 a9 a& H: o
       - 例如:n! = n × (n-1)!$ L  ^6 y( m* G4 W$ L
    , m0 P' g4 S- u% D; @. N- O
    经典示例:计算阶乘# y0 y7 |, e1 F1 ^& R" a5 h+ W7 W! }
    python# d, ]. F9 ^5 {; ]" t6 @
    def factorial(n):# @, F+ m6 q* O5 T3 T% |5 F; e% j
        if n == 0:        # 基线条件  j# Q8 Z. v) K# O
            return 13 x& E5 Z6 P+ A8 H; h0 F
        else:             # 递归条件/ v* L0 J7 n2 |4 }: x
            return n * factorial(n-1)/ n% r+ |7 b" Z! a8 ?7 q
    执行过程(以计算 3! 为例):# a6 P# N3 X! S$ P. `3 t7 A' L
    factorial(3)- I3 o5 G: M9 b
    3 * factorial(2)
    & y5 e6 E' i  P5 X3 * (2 * factorial(1))1 w3 l# m- f1 @; d: d& ~
    3 * (2 * (1 * factorial(0)))* U' I" o2 q/ Z7 y
    3 * (2 * (1 * 1)) = 6
    3 v) x! ^# G. I: W; Z( a8 p7 {9 M$ n
    递归思维要点( D4 V/ |% ~* [6 K! R. J, x8 ~
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / ~' n3 }8 K0 m" @- d% g2. **栈结构**:每次调用都会创建新的栈帧(内存空间). n4 J. G: `* [
    3. **递推过程**:不断向下分解问题(递)
    - `- q$ q# U: s3 U, M% Q4. **回溯过程**:组合子问题结果返回(归)
    8 Z5 u' j, {7 r- C) E9 n! B, a3 H" z; d! j
    注意事项1 o) ]* H% X& w7 Y+ Z! p' O* l2 d$ J
    必须要有终止条件+ D" _/ A2 H+ ?+ _6 }( u
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 g7 _  p5 @$ b1 ^
    某些问题用递归更直观(如树遍历),但效率可能不如迭代. Q  D: v5 `4 x3 [
    尾递归优化可以提升效率(但Python不支持)* P+ z& U" ?) x- c& t+ T

    3 }1 z7 K7 F& T; T+ D' R: F7 f 递归 vs 迭代& M. F7 ^2 V5 t/ l
    |          | 递归                          | 迭代               |
    3 q" T* h' G; |% I7 x+ {9 o0 n, H; J|----------|-----------------------------|------------------|6 d# J* k# F3 W
    | 实现方式    | 函数自调用                        | 循环结构            |
    # [1 {0 L/ P1 O% k& w| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |; I! w0 o1 P5 q, v! }3 T* A
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 E9 `5 s/ W5 ?5 Q- R8 M9 J) C| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # Q: `6 W" p" S/ X. s3 ~; ^+ Y/ }4 o8 _3 {" q( k& u3 W
    经典递归应用场景7 S' [1 ]* k% a2 w
    1. 文件系统遍历(目录树结构)
    9 j! M0 Q. O) B6 `2. 快速排序/归并排序算法
    " k' z2 {: ]- _3. 汉诺塔问题& [; K0 N" i" H
    4. 二叉树遍历(前序/中序/后序)
    5 ?0 |, f  u) p8 y/ u3 E3 J4 D5. 生成所有可能的组合(回溯算法)% t  c0 {6 |3 Q  ~. j' U

    # |# ~$ V/ s. s2 l- I; W8 {试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    15 小时前
  • 签到天数: 3197 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ P3 j  D+ A* w- x8 Z8 u
    我推理机的核心算法应该是二叉树遍历的变种。$ M- e* N+ [! F% F% Y9 a3 }& @9 x
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    6 T6 ?2 y- V8 R. N: RKey Idea of Recursion
    3 d5 m( l- A" K; A/ f& f% r5 o
    0 t9 Z% j# J$ T& JA recursive function solves a problem by:/ i7 o/ ^( m# M6 T, a1 \

    3 {, _% ~) x  x! \    Breaking the problem into smaller instances of the same problem.
      B3 F$ y7 ^$ x5 v7 H
    ( j# F( {9 v3 i) A3 B; d% ?    Solving the smallest instance directly (base case).
    # z8 E9 s2 h; \. C2 A# U( F% v) m" C( V7 Q1 C
        Combining the results of smaller instances to solve the larger problem.6 M4 c3 X( _* R; G* `
      a. `) g% a6 r# E. h$ L" b; |+ K! s
    Components of a Recursive Function5 y4 Q# t, ^, L$ V3 s# L3 ^& W
    3 x6 {7 T5 C  H8 r- i+ ?9 {+ w/ c
        Base Case:
    6 `( b' T" J% E; m! Y: \$ X6 ^) d$ z" X/ [
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' q1 m0 S9 l3 s  b- L. T

    0 K4 D: _' Z! n' A( v  Y        It acts as the stopping condition to prevent infinite recursion.
    # ]. e, Y' Z6 u$ i
    * n4 B7 X) J% t7 C1 Q( k        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.$ L' B$ z% W/ v& b
    # o) I" l# d# ~# u8 Y) k
        Recursive Case:1 {$ z7 U& I9 ~# b" T1 X# }: @/ b& ]

    4 o) o, X+ x7 I6 D        This is where the function calls itself with a smaller or simpler version of the problem.5 d! h0 G7 O- U: G. b
    ) S* L2 |( Q* g8 [
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).7 u3 U: F7 x1 \$ X2 o: ]  O

    ! |, l" B$ v8 k/ |% K# DExample: Factorial Calculation
    ' M' W& `5 r$ |4 l1 P6 j  p( d) S5 r: i2 V
    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:- h, t3 A7 W# X* S) @
    / H, ^  s5 R. z( G4 a; c7 z
        Base case: 0! = 1
    * Y( u3 x; ?0 Y6 a$ t- V  ]
    / |) A, _) m- @& w6 U3 W    Recursive case: n! = n * (n-1)!
    6 K' k' f, k4 v2 F, ~; h# B: U+ Y5 t4 L2 F  H
    Here’s how it looks in code (Python):
    / O" X5 r* f1 P4 a8 b9 }/ z! |python
    6 Y% [; a9 ^8 }, ~* {$ U7 x, p
    8 B+ ~$ W8 A# @, e9 x* E! r+ ^
    / ]5 I  [3 U# E. n$ ?def factorial(n):
    / W, K8 a) B% P' Z    # Base case" K/ J& s$ B6 Q) F
        if n == 0:& K  J/ X) k' @
            return 1
    6 _$ a2 {( d' l; f    # Recursive case
    3 @( ]' x1 V' S0 i# Q    else:( z9 k' B+ n4 g) M' h3 V1 F$ X
            return n * factorial(n - 1)' O+ a/ ?* j# _2 X
    9 W9 G* g- o' V' z1 j4 t- \
    # Example usage
    7 h1 S* P& g3 {3 i0 h1 a" k0 |print(factorial(5))  # Output: 120* u$ g0 N( a; x3 {- e- L
    " v. [  R5 R; D& Q8 X
    How Recursion Works4 M7 @- r. v6 m& ^1 N0 T
    % Y% E3 ]4 e& c  U2 N* D% `* ~* _3 x
        The function keeps calling itself with smaller inputs until it reaches the base case.
    + a( U4 |. ?3 X0 ]5 d1 r6 e! V3 B9 _2 G/ a8 V' ~2 w: s& W
        Once the base case is reached, the function starts returning values back up the call stack.
    ' n# ^3 |) g3 I- ^3 t9 Q5 k$ X$ P1 p- @/ |: R
        These returned values are combined to produce the final result.
    6 ~/ \3 L* T: j4 i) n  a( I  @% D1 @+ Z2 v' k% u/ z
    For factorial(5):
    2 D4 y; p  @; h; P$ d6 B9 L" p7 {

    , c4 X, Y0 J( `factorial(5) = 5 * factorial(4)0 ]5 v! o4 h* W& U0 U
    factorial(4) = 4 * factorial(3)
    ' `# N5 v7 Q0 i# C3 y2 [factorial(3) = 3 * factorial(2)
    + ~- s  e3 B3 A( `0 Y5 C1 N# \( Ffactorial(2) = 2 * factorial(1)
    & Z3 E5 m. y: A9 Pfactorial(1) = 1 * factorial(0), W) z2 Z; E" t3 l. `. u8 B4 B5 |
    factorial(0) = 1  # Base case1 |& c: e) D" v% B
    5 O* c# z$ j+ {  P+ Y8 l3 `! {
    Then, the results are combined:8 ?/ J+ ^3 J' K( N+ T# c

    4 \, f4 d" k# e6 B3 l7 F! K; ]) X9 w( N; b
    factorial(1) = 1 * 1 = 19 D: o4 k' u0 e/ X
    factorial(2) = 2 * 1 = 2/ _" N) V" \4 e0 _; o
    factorial(3) = 3 * 2 = 6! B4 a/ a, N: h0 u* ^8 v2 O
    factorial(4) = 4 * 6 = 247 O" V- ]2 x3 S3 R  N! Q0 K+ Z
    factorial(5) = 5 * 24 = 1204 _4 M# v: T  X2 b9 O2 p- x: j

    9 x2 Z6 K4 @1 d' N/ p# b( ?' P- RAdvantages of Recursion
      L4 e# N' S, A: ?
    - f: Z9 C2 H( `1 \, ?# c1 G; X    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).
    , s% o' U8 R8 m5 l4 }5 j* z8 f" w5 C, Q7 S* ?! x
        Readability: Recursive code can be more readable and concise compared to iterative solutions.- B8 c, s2 e5 Z9 F1 B

    ' Q% P- y9 M6 c5 ?6 y( CDisadvantages of Recursion
    - k- ?& h  d. u4 k2 E2 J2 V9 F  U/ y4 E( C0 o2 g7 O7 ~
        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.
    1 ~) z" X7 ?3 p1 Q  {  Y
    ! c( r( P! k5 T$ F7 W4 f    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: h: |  V  \7 o/ ?! G# b

    " u2 f  a6 a! E8 WWhen to Use Recursion0 |$ \5 `7 O1 y/ @; i. _/ X2 h

    + R0 X# A5 Z# a3 A7 i9 m. a4 A    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    1 b* C8 ^" K1 Q: h8 o* M" y3 [4 Z! ?6 N, x" M. t
        Problems with a clear base case and recursive case.
    / ^& t1 A* [% f* ?- }7 k9 S, Q" p  W( Z: q
    Example: Fibonacci Sequence: y% I' |; J( }2 W+ h3 a! d- d

    8 J) C' M# h3 M3 [The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& q" O$ i0 k2 m5 D" \+ S
    9 k( T. ~* t- v7 d. x
        Base case: fib(0) = 0, fib(1) = 1( s/ V" i) ?6 N; [. I
    ; c. l- F0 ]. q! _$ v) J/ I
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    / P9 D. ^* H3 ?) x; L  v0 x3 j$ i" ?8 @" D2 l
    python
    6 {1 s& O2 l2 ^5 @& U+ i3 f: y0 \9 E& |/ @, N4 s. Z5 \7 s

    3 H0 z5 q$ a8 H6 B% S7 f' |def fibonacci(n):
    : B! s8 F$ w; ^& j2 L    # Base cases
    1 ^# \/ a3 Q/ ?% m7 m6 e    if n == 0:
    , A& [# q( ], H+ A9 k" {1 w7 G3 q- F        return 06 ^" Q; k/ ~2 A2 v) G( O3 `) y3 n# r
        elif n == 1:
    + E- E4 n5 W! z  [$ @! m" E        return 1
    2 A# e7 k: K$ E& E2 C) |1 X# m    # Recursive case
    ) _/ ^8 X$ j2 h" C; t    else:
    / W1 d+ O2 v/ A# X8 H3 D        return fibonacci(n - 1) + fibonacci(n - 2)
    ; {, l9 ]; o/ E/ Z6 s2 x$ n! _
    : Z3 E' v/ l2 J1 S# Example usage4 a% `) l. O  a
    print(fibonacci(6))  # Output: 8( h0 O% @% s6 k( \% h! }# H. F

    + i0 m9 L+ \& J4 B' ?1 a7 v5 }. g% eTail Recursion6 c1 @# n4 w) Z  M# Y$ x
    ) J6 ?( B' @, u& t4 \
    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).
    # R/ P: \5 N" e/ S; L* S% v
    . v3 W. C1 R1 |4 p* {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, 2026-3-10 23:52 , Processed in 0.058862 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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