设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 E" v9 F" g2 W3 I2 Z; g
    ! N# h0 l9 m  \3 O/ \( C" y
    解释的不错
    ) x% [* j1 w8 }" M0 @6 S" q1 N! k4 V" c6 s, w! o7 J
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    , Q  H3 c& c! l; ]! y$ ~4 q, J+ J+ H+ B6 ^5 p7 \3 M4 C! z
    关键要素0 K3 V0 u/ o: D# G  L, d- _
    1. **基线条件(Base Case)**6 Q1 \( S# _  }7 i. u
       - 递归终止的条件,防止无限循环
    8 y4 @! N0 R/ i. X; i( K$ M: Q. d   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' G  V; t6 H+ y$ L2 r& T5 [

    ; X( ~' k% o8 t5 s: ~1 @2. **递归条件(Recursive Case)**
    , L6 S) p8 v& I/ }+ ]* p   - 将原问题分解为更小的子问题
    : I8 q$ ]) Y- A; Y( Z   - 例如:n! = n × (n-1)!! P* `  E. T$ {7 M

    . m, J; f, M' v& \- o( n 经典示例:计算阶乘  z! `# d6 q; P  j4 `; \8 \. X
    python
    # P$ m, J4 ~7 X1 L6 {def factorial(n):& r6 g" e: F2 ~8 U
        if n == 0:        # 基线条件
    " l; `1 \$ q+ @$ s4 U        return 1  i) q% k: H+ p; P" ?
        else:             # 递归条件
    ( t: j" J8 z% H8 ~+ T        return n * factorial(n-1)- b1 O: v. s# G5 ~( Y% T+ F3 z4 q# T
    执行过程(以计算 3! 为例):0 g6 d5 e% w" R3 }9 _7 |
    factorial(3)* g( f3 f; z; S7 ?& w
    3 * factorial(2)9 U0 z/ T! k( q! S8 x5 }- E" o; D
    3 * (2 * factorial(1))8 y% v4 B% b& a9 ^  v+ A  a+ _# d
    3 * (2 * (1 * factorial(0)))! l0 o6 Z% _: f  j: F
    3 * (2 * (1 * 1)) = 6* ^; y8 v9 W0 F; i
    3 d" ?$ U& W- V( C9 y& Z( d
    递归思维要点
      J: y+ z& }- D: t. d/ |1. **信任递归**:假设子问题已经解决,专注当前层逻辑. n  X# v. q" c0 y' x8 I
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)& \( F# t; w+ }( E: Z
    3. **递推过程**:不断向下分解问题(递)
    1 O+ m3 d# ?! W4 k0 i8 Q9 Z4. **回溯过程**:组合子问题结果返回(归)% b5 C, X0 ?0 r, B

    - F" S7 e" H1 E; J! j 注意事项
    / W$ K! D; v- w% _! e必须要有终止条件  c. ^, a6 Z- t6 X7 d" L% o% X) r
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    - H1 b$ O& d) d某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * E  ?! s( [, U$ C6 F尾递归优化可以提升效率(但Python不支持)
    : X, x; v0 {0 h# A* K: G, T
    : O0 B0 {; z% d" t/ ~, ] 递归 vs 迭代$ [7 u8 C7 R5 P7 ?
    |          | 递归                          | 迭代               |9 n) S' C) t4 a, I/ O
    |----------|-----------------------------|------------------|
    + M$ l1 A/ N. n/ o$ {| 实现方式    | 函数自调用                        | 循环结构            |0 L+ T  U7 V: p1 W
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    0 f" v8 |, W( H! z| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 `% Q; X' T' u3 c, h| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |; Y6 N7 Y  G+ x4 U8 `, J7 d- l$ V5 t

    9 O' q+ Y9 {) V0 t7 z 经典递归应用场景, U! ~" J2 E* v$ d: C: P# G! _# ^
    1. 文件系统遍历(目录树结构)
    " X4 z- K3 A7 T$ h) A3 [2. 快速排序/归并排序算法
    3 m- E9 ?; R1 ~6 B5 ]3. 汉诺塔问题
    9 D3 w4 A; m4 t7 }2 a4. 二叉树遍历(前序/中序/后序)
    & k6 T5 ?7 m3 L3 z. h5. 生成所有可能的组合(回溯算法)
    3 }1 `3 c5 p; U8 b6 ~
    2 m! e2 u  j, d# C- |7 Z% S# ]试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    12 小时前
  • 签到天数: 3017 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    5 M2 Q2 @2 L0 J! ]3 H! x/ Y我推理机的核心算法应该是二叉树遍历的变种。
    ( m5 P5 L6 p  I+ b- q: e2 P另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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( ^0 _, A$ n2 Y9 ~/ c+ G+ NKey Idea of Recursion
    # A) P+ A. v+ ~- E8 U
    % T- s: M0 R# B2 o, m' B- X0 TA recursive function solves a problem by:
    - P4 h. l" y' `
    4 @. s/ a7 A4 t    Breaking the problem into smaller instances of the same problem." t: y! P; V- \5 H* F

    : P: m  p* @- y  {. K9 g    Solving the smallest instance directly (base case).  f( B- ~0 F! _% h# l+ m

    . G" w8 W0 r7 W& e3 q/ _' Y    Combining the results of smaller instances to solve the larger problem., v9 }: Y& _" D' n! M- @2 \

    1 v: K' Z' U, w$ e3 q: @, JComponents of a Recursive Function. c' `1 A; S' _

    ( A1 p/ i( @/ ]; [( j; B    Base Case:3 ?" J/ j& S( c

    ; j- S) D3 d, k0 {- [7 O: D        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.  R# ~7 w: A! Z7 K7 S; {

    ! _& T4 k/ U+ G, p) A4 {        It acts as the stopping condition to prevent infinite recursion., u8 l1 s& h7 ^, N0 Z! B) j+ R
    1 k- u/ m  R* U
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' M( D* C' o$ g9 ?; y6 e9 A, t1 X, V  M) b
        Recursive Case:5 e( s% ~: K5 A  _" n
    ) E( P  M- e$ `6 t  [# d% G3 y; D
            This is where the function calls itself with a smaller or simpler version of the problem.5 D' R* f2 K% O' A" l0 l

    : S2 W' X/ s: j) H; ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).) B0 P/ a4 |  o

    3 ?$ e, k0 ?, AExample: Factorial Calculation
    5 [) o  z- Y; k2 E4 \" ?! x9 E/ U* f- L8 x1 h' ]/ Q/ f
    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:
    5 i6 m7 A2 N; [* ^! w- i, Z- y; w& e2 U+ p, D
        Base case: 0! = 10 T4 _% ~5 E! W2 h( ~

    7 i# ~+ X2 L( z% Y% k    Recursive case: n! = n * (n-1)!2 _% N: s5 z  |  B8 @* x
    % z# q1 _) X) K$ X
    Here’s how it looks in code (Python):7 L* W9 @" Z" b
    python
    $ }; t# o/ ~, a7 h  v" d' G# q' a+ X$ s! b! Q7 n2 B7 r) u/ \
    $ N5 H5 F* U6 Q: ]
    def factorial(n):
    ( [2 j: w5 t5 N# g+ H    # Base case$ u8 k! [- g: L1 o, l( w
        if n == 0:
    / F& B/ d, K0 {9 L. P) @3 N# V        return 1, ~- y" R3 J* v* K, I% ~4 P
        # Recursive case
    3 Y5 r4 I: r' K* d' _* p* e    else:
    5 k3 u3 C6 Y2 o7 s5 G        return n * factorial(n - 1)
    3 B  x# m8 D- s- B6 n; s  ?/ H1 _; Q( h  A% T2 p
    # Example usage
    0 L, t' b* B# M# `, ]print(factorial(5))  # Output: 120
    ) R3 C. g, Q. J7 g  y+ \* \2 x' ~. Z; e  j, |
    How Recursion Works
    . F, O) ~7 S% s* h+ M2 ^* i- c: b! O& J( l
        The function keeps calling itself with smaller inputs until it reaches the base case.
    / l) @% p3 R$ m! W6 Y# m8 s- \2 N+ a. T0 w' z+ Y
        Once the base case is reached, the function starts returning values back up the call stack.8 |3 _) I+ E" E$ W
    7 \1 A  T. Q# @) m2 W- C1 M
        These returned values are combined to produce the final result.
    . h$ I2 @- h1 w! M/ i8 p3 O+ i
    ; W- X7 N! J3 ~5 s$ O8 FFor factorial(5):( p( t' U! y( k+ m6 `

    " U) A5 U7 D# }2 T) C' E9 N( E) \5 ^, U* P! \2 J, N7 y
    factorial(5) = 5 * factorial(4)! q* d* M6 \# F, q
    factorial(4) = 4 * factorial(3)
    1 @: H3 }0 w" x9 S5 |factorial(3) = 3 * factorial(2)
    ' M) M; u4 P- d( hfactorial(2) = 2 * factorial(1)
    2 ~0 Q3 B8 d; Q& H  f) e; m' v) S/ @factorial(1) = 1 * factorial(0)1 S1 I7 ^2 B: N: M9 P2 e3 D& H7 S
    factorial(0) = 1  # Base case
    7 L) a2 h) U; l4 X
    * b( g1 \4 Z* P$ t/ ?! {0 sThen, the results are combined:, N0 f2 P- i3 n! B0 P4 j# I( q! C
    2 @& _0 J8 Q2 |5 U7 T) e5 V9 t8 ~# O! b6 D
    # K; _' h2 @' I# Q$ W1 @
    factorial(1) = 1 * 1 = 1
    3 V! H7 [/ V6 A! m& _5 s9 n3 Dfactorial(2) = 2 * 1 = 2
    4 l+ \* p6 K% `. tfactorial(3) = 3 * 2 = 6
    5 Z" g' q% i; t  f* Z! ffactorial(4) = 4 * 6 = 24, K9 n' K* _. W% ]3 N
    factorial(5) = 5 * 24 = 1202 `$ ]' z6 K9 @8 e/ z5 I4 H) @( [

    : l! _4 J: ~" Q5 C  e: ]* o1 f2 oAdvantages of Recursion
    5 s8 f$ Y7 r0 X5 j7 e& Z; e4 T* y
    + z* u1 @7 E; e1 w    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).
    0 S0 H# H5 I3 b. G, L; f% Q8 j, ~+ x
        Readability: Recursive code can be more readable and concise compared to iterative solutions.2 m' w& L: h3 S5 h! T

    . _( y$ t* n) M' p& T0 W' A) sDisadvantages of Recursion  C) t) Q2 f, x

    ; |" [2 N# g; P- \; N4 R    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.% t: V" y( [+ F7 H2 j7 ^3 x; G

    * g+ |! `6 n! w! ^: j$ Q    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    % c4 w' X7 }( P& y: g$ N. c
    ' j. m0 w0 D0 w# H. zWhen to Use Recursion# D& ~+ X/ O: O- q2 C

    ! F" W, H/ x0 n: k: |    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 b# T; o1 z4 j/ Q# W" U, }; C- p
        Problems with a clear base case and recursive case.
    ( u9 W- O( V- \3 {+ |
    ; ~, U4 `- Y3 X- N& b1 ?& WExample: Fibonacci Sequence8 ]& h' L2 t4 ^  X3 I/ }! v' i

    # F8 W8 W) ^* W! J- R7 ~The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    + R) _+ Y. H) t! K/ Z6 N8 V2 z
    8 M2 e0 k3 R- }6 o7 D& M- U    Base case: fib(0) = 0, fib(1) = 1- o7 W1 _" q8 v% }% l# x* D, y
    ) F1 c6 g+ p. a6 x
        Recursive case: fib(n) = fib(n-1) + fib(n-2)6 I/ m; Z8 Z( I5 g  S
    9 q; M$ o+ a5 c$ D1 C7 U
    python
    . N5 d6 k5 K3 b8 G: p  i9 ]. m& P& ~
    * D+ d. d' t! y: Q# L
    def fibonacci(n):
      I+ Q9 ]! e* s+ a3 y% P) [, Y% j* W    # Base cases2 i/ J$ \: j" C4 i7 x8 v: y, L
        if n == 0:
    % Y7 \3 X3 v+ ?0 ^( ^        return 0
    % J; `- }8 X: \    elif n == 1:
    , q! k+ G3 M3 l4 Y! d* L: u        return 19 a% W6 c, M5 A
        # Recursive case
    , \  f  L6 J, K9 B  E4 o- q/ b" W    else:% n- `& X, A' v& {) G2 \6 P4 h
            return fibonacci(n - 1) + fibonacci(n - 2)
    + U( _9 M- }7 C& J+ U+ _% g
    , u. P# @* R3 ~9 F, B0 V( F2 }# Example usage  |) a" a" k- j' W) k' N& \
    print(fibonacci(6))  # Output: 8
    1 m; C3 l( [" e! ]. y* @# R7 S* L5 b8 f* n3 W
    Tail Recursion" ~8 i4 R# T/ H* Y* c2 L. |- [) @

    7 `0 b) F7 X8 u1 _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).
    & w$ x: _: ?- F& ?2 o
    + }$ n2 f- w5 O0 H. i5 F8 ^5 Z$ A( 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-8-22 18:44 , Processed in 0.045458 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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