设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 ~$ [. \; }$ ^9 C

    9 H+ |% @* o! b, h8 v解释的不错; }( [+ w1 m/ n$ Q0 ?

    * y* S5 j1 k5 F; c# a  Z递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 {# X' {* l8 N$ w# p/ j, S, y/ z) m8 u# ]
    关键要素
    + S3 ~6 @/ O9 f" K1. **基线条件(Base Case)**4 ?) q7 c" E1 K4 ?7 L  L7 |
       - 递归终止的条件,防止无限循环( J. }& k" k9 b- _
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 14 p! o( F7 a% |' E7 v2 m( j( N" z1 J
    : C9 M9 [+ k; N+ p  u3 K' Q9 s
    2. **递归条件(Recursive Case)**
    9 _& {! S8 L% E" J9 T   - 将原问题分解为更小的子问题! {* _( x/ H9 U7 b$ d# t1 q" U" u2 V
       - 例如:n! = n × (n-1)!
    * w' H, _8 [2 v! ]$ i7 ?  i1 n
    ) B. V  [" R: H9 H" ] 经典示例:计算阶乘
    4 V$ S7 Q8 A; o0 `# s" \# T0 [python
      G& l6 K3 e; ~- m  c7 R. wdef factorial(n):) v$ ^! W2 s* [8 ^, o
        if n == 0:        # 基线条件/ @& v; E$ r  M# B
            return 1
    0 \! v; K, m, o0 c4 E    else:             # 递归条件
    % Q3 N5 f0 w9 M2 I9 Y        return n * factorial(n-1)
      M- O8 P. `$ V0 G. n/ ^6 t执行过程(以计算 3! 为例):! O: u4 d7 \  m0 O% C! z" g4 W  z7 T
    factorial(3)( Z5 l3 }/ B; B1 {
    3 * factorial(2)
    " T; u$ b/ l+ P8 E3 * (2 * factorial(1))
    $ m8 D& I. V! B: W3 * (2 * (1 * factorial(0)))
    . @; O+ S9 ?9 k  Q* K9 w0 g3 * (2 * (1 * 1)) = 6& Y0 E' d3 V& U9 X3 }& a% k
    . y3 M8 G. S4 T: F/ O8 |. b, O
    递归思维要点
    % [; V  M) F% ?- Y1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! d4 u6 ?- [$ S0 k3 z. w! \2. **栈结构**:每次调用都会创建新的栈帧(内存空间)% Q; c* p7 w6 f& Z
    3. **递推过程**:不断向下分解问题(递)7 }1 F0 n) }+ W+ F- c/ I2 P
    4. **回溯过程**:组合子问题结果返回(归)4 Q' B: |5 T7 z* w1 K1 l
    + j8 D% P! U# I/ {# ^9 ^# C
    注意事项9 C& I  r7 k$ n7 `1 b! I
    必须要有终止条件# H" [( t/ u: w4 \& ]+ `8 }
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 r$ h, W+ S0 J6 j某些问题用递归更直观(如树遍历),但效率可能不如迭代& y  n1 O4 v# h% t( D8 o/ F
    尾递归优化可以提升效率(但Python不支持), n, t$ ^8 f7 C- R7 E* W1 y; a

    7 [7 X8 B0 y- L1 ]% M( g6 g; o 递归 vs 迭代* g4 X) R& h7 w1 E
    |          | 递归                          | 迭代               |2 I7 E/ M, p- f& A( B' _% }
    |----------|-----------------------------|------------------|$ ?' ?4 k$ \' F! N
    | 实现方式    | 函数自调用                        | 循环结构            |1 w5 l, g- X0 ~" l
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    + ]8 j+ E! A1 x) g+ w, h| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! `& ^, g6 R$ \6 Y  t$ i2 H$ x| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    * I  _% C/ M' t! a: z' [
    6 V$ |  h' R- u# b 经典递归应用场景$ T5 @/ Y% O* `! d# s1 J
    1. 文件系统遍历(目录树结构)
    5 u- E4 j& o: ]1 A0 t* r+ S. p2. 快速排序/归并排序算法+ k' A5 z  a/ c2 Z! L8 g, ?" \
    3. 汉诺塔问题
      D2 R# e3 I( `) n4. 二叉树遍历(前序/中序/后序)
    + {+ h% N- O- r4 b  Z2 W8 `. o/ h8 P5. 生成所有可能的组合(回溯算法)2 `- m7 |5 N( s

    ( m& @& ?" ?' K6 [: [5 ^试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    前天 07:04
  • 签到天数: 3377 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 M- a# M' ~0 A
    我推理机的核心算法应该是二叉树遍历的变种。
    : k# o9 o! s& O- T% C: x+ |7 k另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, Y4 m- Z" b6 O' P7 B; \
    Key Idea of Recursion
    2 L) ^: ^/ O! B1 ^: Z$ W- h3 o( S8 S+ q( N6 a  O. _
    A recursive function solves a problem by:
    1 h* y/ s6 D: i( |: |
    ! Q# ]' ?2 b& K' [8 ^0 j    Breaking the problem into smaller instances of the same problem.
    ! w) X2 ^- b% z' U# W* @* n. F- {
    ( g- o% X9 d8 }, N! }    Solving the smallest instance directly (base case).; i* c2 G" F5 I0 Y, o
    " K) `$ k. D4 ~0 S
        Combining the results of smaller instances to solve the larger problem.2 q( a6 J9 m) [0 F9 [2 O. q' [

    5 X2 s" t0 r; ?Components of a Recursive Function
    ; N. J" e4 L* u" f: W- i2 V! M- J, k1 Z; Q
        Base Case:
    0 |9 }$ w" i- l; A- |% H  @) K8 V2 g1 J0 R. h
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 V8 A7 N( Y, o  @8 @; V4 }$ A$ r/ }

    / U/ i& d& ~1 ]& \' W2 @        It acts as the stopping condition to prevent infinite recursion.! Y# M! `' y+ C

    3 W0 l/ u5 E: j. D# |8 k6 M        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.4 i* U! d6 ~1 L. |; u8 b# y
    # f; f9 b. P+ \- h2 }3 c, n6 k
        Recursive Case:
    / K% U1 ~3 {- P5 O! ]+ E' ~7 r
    - b: G3 B/ V7 v7 t7 L9 K  u6 ?        This is where the function calls itself with a smaller or simpler version of the problem.0 K: v) Q( b0 g: C0 [& P, M
    ( H6 [. ^. r# K4 R
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).% n7 W9 K( }; N! ]0 T" g. @
    ; n8 K3 H% J: B4 x7 |
    Example: Factorial Calculation
    $ l( c# Z% b/ D7 z9 ]. I
    3 P( Q* ?5 P. p! K* l8 f: g" hThe 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:
    2 b8 X- U! I1 {$ I( u" m: Z- W$ C5 V% N! B2 S# V+ j
        Base case: 0! = 1) v) E; N8 e  e. |) ?6 G
      T! y* l* \7 b6 ^. z
        Recursive case: n! = n * (n-1)!
    $ k2 J9 X+ J  \$ |- z5 s* N9 \6 u  }9 P( {5 n0 l
    Here’s how it looks in code (Python):0 w9 F4 q7 f4 ]/ D4 G. g
    python
    % ~. F, c8 T; ]+ R8 ^1 A& C0 @$ v0 o* f$ j! I. G
    # e9 @; _7 i6 u4 v. {
    def factorial(n):
    ) f1 r" M: M* f/ v' B# d    # Base case
    ! @8 M8 h, m: y    if n == 0:
    % i- T5 |1 T! ]3 o/ G        return 1+ b3 `7 {' k, ?5 V4 S
        # Recursive case" U) D% T# Y4 e5 K/ T! b
        else:
    * c2 `/ v* u6 u# J/ t$ ]# u/ [        return n * factorial(n - 1): z0 q0 z  b! ~$ }; B  n

    # u7 K/ `% D3 a! o- t8 s4 j8 L# Example usage
    * m6 C% e; H1 X  s0 c" Rprint(factorial(5))  # Output: 120
    + k" e1 J9 A4 ~# S$ h2 u* S" w
    How Recursion Works& b7 o# P: {) N+ z! h

    ! p1 a( k/ j' b( Z* t    The function keeps calling itself with smaller inputs until it reaches the base case.. [0 L# h8 x" a; s) h& M6 X7 ]

    $ e4 c7 E$ f! V2 g5 [0 D% V3 W3 |    Once the base case is reached, the function starts returning values back up the call stack.
    9 c' G* @8 [5 e3 Y8 O5 H
    ' r: D% c( {0 T) \& _    These returned values are combined to produce the final result.! ~, ^! ?8 R. x& B/ W* P$ f/ w

    : q$ s$ M- N: m! {+ W7 }  n* PFor factorial(5):
    3 `7 m- l' A$ O6 b8 f, e5 a, ^6 o: {
      ^9 n0 O" b/ z( h; v* A* ]3 N) p# b) `9 L) U
    factorial(5) = 5 * factorial(4)
    : |& G: {/ f, }$ m4 o4 Yfactorial(4) = 4 * factorial(3)
    3 h, r3 \7 Z6 x( K, y0 ffactorial(3) = 3 * factorial(2)+ u4 F; a6 o/ b9 u5 q0 X  p
    factorial(2) = 2 * factorial(1)+ |9 p8 V: P' l$ Y8 b/ M: M0 r: m
    factorial(1) = 1 * factorial(0)
    & w8 l9 e7 c6 R3 ~$ x6 i" Jfactorial(0) = 1  # Base case
    ( B& G3 W" c! m
    ) G5 v5 \  P4 uThen, the results are combined:
    * P$ R1 F, b; R# w) X2 G
    1 n; L, W/ B- v) `" T
    / _2 X% |& p4 j/ s$ ^4 wfactorial(1) = 1 * 1 = 1
    ) [+ Q% m' k$ Z  w5 C1 N( S7 Sfactorial(2) = 2 * 1 = 2
    ; O+ y3 f  I2 [% Dfactorial(3) = 3 * 2 = 6
    : Q: j) ~+ E1 jfactorial(4) = 4 * 6 = 24
    0 r# Q% h8 \( Dfactorial(5) = 5 * 24 = 120
    1 |* q' N5 C3 x% q  a% p5 G5 M
    6 O6 [6 r0 K9 pAdvantages of Recursion
    " A$ X; |5 Y5 D& w  c1 p7 C% L4 i' v  \5 K5 v: r
        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 [; W0 B- M- Z. V/ w/ E1 k" z0 X6 g4 u* u  K: g) Y, l
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    5 a8 O+ D- y( s: y& {9 O8 \
    ' C$ Q7 u# g* N) @. E' qDisadvantages of Recursion! l' l6 P7 u7 l- \- J/ D
    5 F0 F) d8 J1 O1 }( x) L& 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.
    $ z  _+ z) @, c* E5 ^  i$ x! Q3 R% k5 T, Q0 G
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 ~1 @6 e# g) W# {7 f$ k% N. B
    2 S' R0 F& |: `  f/ g  x1 \0 ^When to Use Recursion
    0 |/ `% F% u) G4 t: v, Q; r& {, e% C3 \
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ( n0 m; R& i/ I" {
    + p' y8 F2 _# M# W; k    Problems with a clear base case and recursive case.0 F9 U" [  V8 h3 {  [

    / R" T2 K5 }4 h/ ^  |$ s! I  Z) O0 aExample: Fibonacci Sequence# f% K7 n7 Z! p, u3 j7 r
    ! k: [9 i& E  {% C5 r) T
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
      S# _* I- x* V. @5 g5 [( x% e6 |7 R, d8 v7 q+ \3 K
        Base case: fib(0) = 0, fib(1) = 1
    7 O* R+ b$ f! f& M+ U4 }6 B) L& v! q' y  T/ C: u
        Recursive case: fib(n) = fib(n-1) + fib(n-2)( m' V* S( {9 r, ]6 K% j
    + A1 r. s8 k5 g+ z) v
    python
    : v9 [) T3 T( L2 ~6 O9 d4 s& [6 y5 h7 c- @) p& h
    6 l0 ^& k- a3 f7 r" N
    def fibonacci(n):
    ; L" @' w. X+ f! X: m    # Base cases
    ) P6 ^) \9 c4 p+ p  Y' ~% G    if n == 0:" Y: X4 G0 X1 j% z- l
            return 0
    4 G) J( U7 O; \. C1 K2 W% H    elif n == 1:
    . x. H! |9 [2 ~/ E2 A! h% V        return 19 T' t# p/ u2 u3 R1 c
        # Recursive case
    0 L. T/ J5 w' g5 R3 G+ j$ ?    else:3 {& y* Q% d% M9 N. P; C
            return fibonacci(n - 1) + fibonacci(n - 2)
    ; i: u, c: p' A! i( x# {
    4 R. [' P, \, c  Z3 B# Example usage3 x" I2 d9 F( D4 Y( n3 ]& a  Z
    print(fibonacci(6))  # Output: 8  w  G, |3 z$ F7 s4 Z
    1 J2 ?( O: C/ @7 C3 v
    Tail Recursion
    # s- }' u% u) ?7 o) O% r/ M, d( B9 f2 i5 L- O) ?6 P
    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).
    ) a4 q3 N* O1 e! s$ _5 F+ p- v# T$ Q& f! L6 O9 w- }
    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-10-14 02:32 , Processed in 0.057480 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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