设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 & c: D% Y3 W7 M; p% e+ ?. I% j, H

    , A! w' ]# h" g' a  Q解释的不错
    9 f8 o6 Q+ ~- U; Y5 ?5 y" c) O
    9 Z4 {; Y! M9 A# Q$ N! o递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。( ]! W5 D* |3 D. h/ l/ k0 x6 d/ V
    9 r6 h  G& \+ e- [
    关键要素
    : D6 z( j$ U$ Z+ f1 [1. **基线条件(Base Case)**6 Z: [+ |4 Y; I9 t+ S. Q4 y3 W
       - 递归终止的条件,防止无限循环2 O6 p/ H' o  y4 H: D5 C
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 H3 {( b5 V; x+ ~7 e) y

    9 Y5 |6 ^5 t9 f* l3 l$ h2. **递归条件(Recursive Case)**
    & |! \" p! Z; _   - 将原问题分解为更小的子问题
      u0 U+ |; a+ E6 o; n# R  ]   - 例如:n! = n × (n-1)!
    6 E! ^5 J7 @: E5 k& W; c9 q3 }* ~, g$ Q+ L% N" c, Y
    经典示例:计算阶乘& \, B4 p: ?( q
    python
    # o3 e% i2 y# x1 q' gdef factorial(n):
    0 m: m- L- m- W5 f. m2 J, S    if n == 0:        # 基线条件( x# M( h; I; X% h$ |! \
            return 1  a, |" {0 y' P. b) u
        else:             # 递归条件
    $ Y9 g$ \+ A) V/ ]3 q2 `6 B5 I        return n * factorial(n-1)9 b' r0 N( z* A' v' Q
    执行过程(以计算 3! 为例):
    . T" x5 V5 C! x7 z& i# T; ]factorial(3)" _9 u7 Z% ]; R3 g9 j# @" d
    3 * factorial(2)
    3 Z, I( v# O2 ]/ {# h7 A: D3 * (2 * factorial(1))9 k$ L! N: U/ m/ U- M4 q- Y
    3 * (2 * (1 * factorial(0)))0 G+ S3 \% d8 T6 L* }
    3 * (2 * (1 * 1)) = 6; p$ \% O2 X# ^7 H4 a  L2 k: |
      V" S- A( h3 A, Z* O
    递归思维要点  U" l+ J& j# P7 q6 h0 K+ ?: {
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # G. s9 Y' Z$ i; ?' F& [2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! H& a6 {3 U8 F/ v
    3. **递推过程**:不断向下分解问题(递)
    1 ?3 S( E& _$ z6 E* B. K& E! k4. **回溯过程**:组合子问题结果返回(归)* s% b3 K+ G$ r2 p! R7 H) c( W
    1 E+ J, \; k4 u$ L9 u5 @+ W& V
    注意事项6 R9 \" Q' w4 O! q, E* B
    必须要有终止条件
    3 b: `' ]$ E) ^7 y递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    2 D3 P) _# [6 ^3 J某些问题用递归更直观(如树遍历),但效率可能不如迭代9 b3 F/ t; g) l( @; _9 t
    尾递归优化可以提升效率(但Python不支持)
    % l6 P* s, O3 C3 x: g
      L2 c; q' T* p) Y0 N 递归 vs 迭代0 v0 O! J8 h. u
    |          | 递归                          | 迭代               |0 i% d8 t0 M0 p  u0 d
    |----------|-----------------------------|------------------|
    . ~" s( b2 b+ y- m1 T| 实现方式    | 函数自调用                        | 循环结构            |
    ! ^4 F2 I2 _8 n| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 c3 h5 E  E) `1 D| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& J& z; K) r; f
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 P5 N* T1 ]0 ^" [0 |! r8 r- |% D; q
    3 Z. y- m+ }( F" \ 经典递归应用场景. L# h( D( z1 @
    1. 文件系统遍历(目录树结构)
    0 f' P6 }1 h! a# j6 [! q9 |! [5 a2. 快速排序/归并排序算法
    % c6 P4 O8 P, Z+ p; p: o% z# \  {3. 汉诺塔问题
    * L1 w/ H& s2 d9 |% ^4. 二叉树遍历(前序/中序/后序)
    8 i& F# ]6 i, J; W  A' u+ I8 W$ q5. 生成所有可能的组合(回溯算法)) p0 K# Z& s& a+ X
    5 X$ @' v  ^& o$ B
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    24 分钟前
  • 签到天数: 3106 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : Q+ L* h/ c3 a) ?: z我推理机的核心算法应该是二叉树遍历的变种。9 ]; S2 M- k6 {  q4 q
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ U! ]5 X4 [$ _( m; Q8 n
    Key Idea of Recursion- ~/ D1 k% v: J( g: E
    , x$ @! l+ Z8 U6 \& C; a" i: @, v
    A recursive function solves a problem by:$ @8 S9 E* h3 c8 ^- i
    / Z, z: P0 S( U6 A
        Breaking the problem into smaller instances of the same problem.
    ' c7 v% v! C0 V# ^  I; P0 W
    * O1 y' \' q7 W) y1 z, H7 M5 c2 D2 O    Solving the smallest instance directly (base case).: s% O% v" e; Q+ g( }
    # I4 c; T2 I9 f4 p2 [7 j( ~
        Combining the results of smaller instances to solve the larger problem.1 l( R: R- D! G( V7 H
    % u! P* J! f" e6 F+ e0 R: R
    Components of a Recursive Function- `% n( p8 R7 N6 p5 A% l
    - k9 `1 P% X1 }- a, D
        Base Case:
    . K& A& \* h+ s7 g
    + _5 E( `1 [2 c1 Z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.- y- J# c6 t1 U  ?- I, E

    + r9 W2 C5 L. E- R( M1 y  a& W% B        It acts as the stopping condition to prevent infinite recursion.2 O2 h2 K3 @$ \7 ?
    # t" {- v5 ^8 h. t! x9 T7 z$ H
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 \# [# j# j/ e- N- P5 \

    7 h/ s4 ~9 ~! F7 n' s    Recursive Case:
    $ q# ?% j" _' b8 }; k% q% f% C  J' m) S8 C- p
            This is where the function calls itself with a smaller or simpler version of the problem.5 V" o$ p$ R9 R. B: g/ C3 [$ X
    0 C  t, ~- [' C. [; l& z) u
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." |  b: _. X* m7 l$ {& r

    8 T4 E% `, ?8 ?' o7 ]+ ]Example: Factorial Calculation- s3 k9 m8 E6 n* `

    ' l: c; q! a" F% R3 bThe 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:9 r2 Y; b% D0 B: o+ m
    1 Q. U1 m3 r- [1 G/ e% ]: R4 s& @
        Base case: 0! = 1; P# _/ v+ V. I) P
    & J% ?1 y2 t$ |
        Recursive case: n! = n * (n-1)!
    " `& C0 z7 l' G2 K  [  O9 z$ Z: F- U. S
    Here’s how it looks in code (Python):, W7 |: p" g' u4 p; u6 i
    python6 l" `. i) }) [  H8 ]) g
    / s9 x/ G1 K5 t
    ' o/ y7 C# q( v8 q3 P
    def factorial(n):& P) C! V& b" @7 {% i
        # Base case
    + k! K3 ~6 a# I$ f0 r% @; V    if n == 0:
    . }" B1 K6 Y0 `; m9 r        return 1
    ) P! |- J5 u& \& i1 k    # Recursive case
    + P5 T4 }. G/ y. I    else:
    * z0 x  l, T& q& `, I9 Q8 B; X6 u        return n * factorial(n - 1)
    * \2 p" M' O( o: L$ q
    $ p" ~% O9 x0 q# ]  _1 G# Example usage
    8 b: s* d. t5 W7 P$ m1 d, `6 m# Yprint(factorial(5))  # Output: 120
      E3 P9 ~2 Y! W( J5 ^' A% G0 H8 h$ k8 M
    How Recursion Works
    $ E8 h9 E& T, c0 S0 l5 R- p; W' M
        The function keeps calling itself with smaller inputs until it reaches the base case.
    & V5 J2 s7 g( Z: ]& H5 W, W3 e0 h( O/ W
        Once the base case is reached, the function starts returning values back up the call stack.; n3 [. Q4 B0 ^1 C/ k2 }" M

    ) [, m3 s# W$ f$ r    These returned values are combined to produce the final result.
    % Y- t7 o7 M: f: n4 O+ h
    * ]" h8 c: u# U8 o/ C7 RFor factorial(5):4 @& T! s/ u% V* J1 D1 G
    + b" I, T4 [0 g$ K
    ; ~! s! Z  i" Q3 b5 [5 I/ K9 n, x
    factorial(5) = 5 * factorial(4)
    ) C" ]- ~7 l2 z" C  E* \* V! @4 Jfactorial(4) = 4 * factorial(3)
    % a- |2 \! Q3 bfactorial(3) = 3 * factorial(2)
    ! {) O$ \; r% G. S# l" P1 Ufactorial(2) = 2 * factorial(1)' q) @+ l+ H, r# h
    factorial(1) = 1 * factorial(0)5 ~$ |* ?2 j- P
    factorial(0) = 1  # Base case8 s7 o' ^8 E2 N8 c% M6 \6 z
    & `/ u6 t, T6 i1 _
    Then, the results are combined:* E$ ?5 w: n5 o' [4 f) N
    7 R- r# v; _, `* |" e0 [

    % _; b8 D- o# A# V, afactorial(1) = 1 * 1 = 1( x9 E5 Y2 H7 i' ]) @0 N8 N+ d
    factorial(2) = 2 * 1 = 2/ I- V4 Y9 y" n) f* M* K
    factorial(3) = 3 * 2 = 6( R7 O: p; \1 h* J. G1 K, p
    factorial(4) = 4 * 6 = 245 f& J( \6 [. p/ N3 `6 K
    factorial(5) = 5 * 24 = 120
    - |/ }2 O+ Z! M% m' @- a! \& b- I, x* B3 I4 \! Z% D) G; c+ ^0 Q3 \3 G
    Advantages of Recursion
    9 Z* P' {2 s# w3 m( b. U  C; y8 a. h! m
        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)." E6 @. X/ X- `5 }* d3 n' t0 m. B
    " t3 z( {/ Z* A' a. ?# x! }* q
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - E( f% t+ X5 }' [
    4 o% O5 O, {& P; D# h4 y' vDisadvantages of Recursion
    " }  C; a, m. O6 ~. p
    : H9 c' f. A1 @( 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.$ b0 Z2 H  A, n2 A5 H& T  t
    9 H; i! q* ~9 J  Z7 S/ ~- s. {
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).- v5 W5 o# G2 h: u) [
    : O$ c( x  Z0 f+ ?& Y+ _" }# Y
    When to Use Recursion9 X  u7 _! x0 I# ^
    ; T* i" @3 s2 ?5 ~# n6 c
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).9 i& ^, S$ ~4 G, v2 A/ S
    ! y# e! d% r, P' `# y
        Problems with a clear base case and recursive case.
    6 g0 W6 ?' s$ o* G7 |0 k
    ; |! i6 t; c+ G' g* m) \Example: Fibonacci Sequence$ ~/ h. B3 z  \! Z6 }$ |

      @# e( s, T. N# A& f" ^: |( i3 jThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:! c2 P" P# q5 h3 r
    0 D' A% z/ K0 s3 c
        Base case: fib(0) = 0, fib(1) = 1
    0 ^' J( m! R/ ?1 q
    1 [. b7 K$ F8 p# L( I    Recursive case: fib(n) = fib(n-1) + fib(n-2)* |( s8 B4 P& w% F3 a
    ! T6 {- v- F0 t& ]0 ~. J
    python/ o' P  p& n5 H. h' w
    ( f+ a1 |2 P& Q' E7 N7 x0 Y! F

    7 P7 o8 i& h! ?def fibonacci(n):/ h- }1 @( F* |
        # Base cases/ G" ?; \. \$ k  l4 R5 ?
        if n == 0:+ @, F/ k# R% B; z. T/ A! s5 k% u
            return 0* [( J6 b0 ]" G( z  J5 G8 e7 T( G
        elif n == 1:
    ; E( ~, F) j/ K        return 1  n' G: ?! z8 e2 ]  B
        # Recursive case
    & F: E: r( w* d! q; k% ?! E+ W    else:9 S3 J. ]# {: l! O  p* A) p# Q2 ?5 {
            return fibonacci(n - 1) + fibonacci(n - 2)
    1 R: b: T: Q% O) m0 F
    2 O# O- U7 Y6 U# Example usage
    8 ]5 E* o5 ^& Z6 F3 O: w( D. ^print(fibonacci(6))  # Output: 83 V9 Y+ q& E- [% }+ D
    2 T5 R4 v4 \( x& j; Z7 n- I8 _5 E4 E/ g
    Tail Recursion6 n# I7 v& Z* E
    + n1 `9 ^1 G( d: d: Z. l: o
    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).7 i$ T9 I% C) g8 H
    . o( H) s* J7 S5 b
    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-2 07:53 , Processed in 0.033026 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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