设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / p0 v/ J4 D! X9 p$ H, _
    3 @: R, @: K6 k! W+ V& C解释的不错6 X! y& y( X7 V
    7 D& A: G8 n; `2 \0 \3 k1 z2 y
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ ?+ O; _! Z9 q  q7 Z1 J
    & _# _* U3 e* n2 j 关键要素2 E8 O6 h! O8 \* V& a, z
    1. **基线条件(Base Case)**
    . H& C& h7 k' z3 U+ k0 n; F   - 递归终止的条件,防止无限循环3 y. \9 P- E8 a! o& S- k" Q
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 B% A/ o7 p) o; [$ |* p

    - Q# p* g( B1 r( U# J2. **递归条件(Recursive Case)**
    % |' ?3 L+ V' u) b) L% y% u. D   - 将原问题分解为更小的子问题$ ?$ B$ q, Y6 S+ m
       - 例如:n! = n × (n-1)!* R# Q4 [! D4 ]) |. A4 g9 m1 {
    ' ^/ y$ ~) {/ L' J+ a4 L) j5 V2 m- l
    经典示例:计算阶乘
    ) r+ U9 x. @% p/ Z6 Vpython, @& d- t: w" f8 v' n
    def factorial(n):
    : A0 C- C9 W! r  m0 @8 Z! i    if n == 0:        # 基线条件2 ~& m4 O- g/ }5 K& _& S
            return 1
    ! S8 n' R+ T! g9 X6 t    else:             # 递归条件
    1 x2 W; o. T$ z! Q+ O" I, B* k        return n * factorial(n-1)! J" \3 ~3 F6 ]/ Z
    执行过程(以计算 3! 为例):+ {3 z9 n) J4 k' K- ^9 x
    factorial(3)
    " F9 x, b3 u! c9 v) c3 * factorial(2)3 B3 b1 d! m3 z, T9 |
    3 * (2 * factorial(1))
    1 {/ X. m: A* l3 * (2 * (1 * factorial(0)))
    ! `0 [4 U7 s/ u3 * (2 * (1 * 1)) = 6) c6 ^+ }6 Q# m  H- Z: ^% g9 e2 ^

    1 f( F, N+ x( O' W6 o 递归思维要点$ _/ ~+ w) f, ?/ l' ~; v
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / _: Y3 |2 ^# n0 n4 B* z1 i2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - E0 ~% x4 a& P% G& \. o7 B3. **递推过程**:不断向下分解问题(递)1 K5 x4 L* Z. v9 O7 |
    4. **回溯过程**:组合子问题结果返回(归)
    / K3 L- E2 H1 U$ x5 J# T% k9 M1 ?( o- W$ @+ |$ S8 h
    注意事项
      M! I, U, @% A6 ]$ ~必须要有终止条件
    4 ?- l4 G5 v3 r+ k: d0 t递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 p0 e* k2 Y9 i6 H$ i
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    7 j$ h# N, D5 x% j: t尾递归优化可以提升效率(但Python不支持)/ P! `# L0 X' Q' p

    ( R( ^3 ^% r9 {- `2 q5 o) f  u 递归 vs 迭代
    / V. m2 \3 y2 s/ A, `( `6 a0 I|          | 递归                          | 迭代               |
    ( w, s. W' @4 W2 P|----------|-----------------------------|------------------|6 `3 Q6 D6 \+ Z0 T7 g
    | 实现方式    | 函数自调用                        | 循环结构            |
    & n4 ]0 Y/ t0 g# M" ]& ^, x1 v| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 o# o4 n. @7 y: o+ M/ j+ q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    " \4 M5 E3 V* [, C| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    " \( G( |6 p  E  X: C. _& a1 |1 F. d
    经典递归应用场景  @; g4 c9 Q3 H' x0 F' u5 S
    1. 文件系统遍历(目录树结构)$ D, F6 Q+ |8 p4 U8 o
    2. 快速排序/归并排序算法3 g: W* F; E# P& I  L
    3. 汉诺塔问题) B. p3 A+ \; v2 b! b
    4. 二叉树遍历(前序/中序/后序)
    1 l) v: I5 R  |+ g' y/ r9 w$ P5. 生成所有可能的组合(回溯算法)
    ' D  Z# c: O( z) L) y
    + T) m- n( b! {试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    前天 07:29
  • 签到天数: 3203 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. A: d' [6 C& I
    我推理机的核心算法应该是二叉树遍历的变种。
    4 i4 U& @7 u3 M1 P, D另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    % Z0 f. s+ F: [Key Idea of Recursion9 M( u6 N8 [% E9 z  g

      \% V# j+ r# @+ t# D7 `* @A recursive function solves a problem by:5 K" S  i# k* S+ n' W7 u3 k
    * Z: _+ g4 n6 R% L, n" I
        Breaking the problem into smaller instances of the same problem.- o  l  Q1 @, Y7 y4 z9 c

    $ Y. r' d9 `" Q' J% i! k5 ?    Solving the smallest instance directly (base case).
    - p+ l2 [1 I) g% M' z+ r/ x# b, F$ z8 i
        Combining the results of smaller instances to solve the larger problem.7 r+ ^) j; \. E
    0 d" S( _* r0 A* B3 v4 ^
    Components of a Recursive Function+ X$ Y& R1 u) i2 k! y
    " }8 F3 Z; ?; L# l. d
        Base Case:
    - U- P! T2 h; o- @% V
    - N  L1 O; r( a        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / ?2 i9 e( M' U1 t3 J* R4 q" o3 X9 u" e% o& f/ `$ ^
            It acts as the stopping condition to prevent infinite recursion.+ U- Z1 z, T9 X7 l* l

    : y1 T! s6 ?/ E" @        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 L* |( Y# I# `$ z; ~" i
    ; j% D4 Z' V6 O5 G  k6 y7 q6 P    Recursive Case:
    3 V6 l# w( A2 {) @$ d- e
    ; E3 `$ d+ b+ k$ Y# ~: V% G, k6 S) h        This is where the function calls itself with a smaller or simpler version of the problem.& T/ u, E% g* T( A' m

    ' f$ @3 R- v6 w/ u7 j, ^        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).( G/ k5 {; A, x$ l) T9 M
    1 T7 b3 ]( |; a+ D$ r" f
    Example: Factorial Calculation
    # T) X( I! K4 `5 I
    - W* r# ?- c  {5 z$ ]) |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:
    ( |8 t# t: `7 V3 ]$ z& [
    ; o0 ^0 ?7 X' M! L) s: K. h    Base case: 0! = 1
    % J, c1 |5 ?4 W2 @$ G
    / S6 C6 C6 _' K2 g3 I1 h    Recursive case: n! = n * (n-1)!
    & c: `, j$ Q1 a" S9 S8 s2 t1 @8 o* z& w! i. I  ]4 r
    Here’s how it looks in code (Python):
    ' B3 d7 h5 g9 l0 d& jpython
    7 ~/ `' {6 A, n0 A$ u
    . x! |6 o9 x; A# l0 n, y8 e& [, r1 K1 }( |, k
    def factorial(n):
    8 f# e/ s* O  h2 s7 ?( `5 ]    # Base case0 U6 `5 y# d+ s+ y' h" ^; m- n
        if n == 0:0 Z/ E, Y, c4 Q
            return 1
    , c$ K( A& q5 R0 i& H  U  o; ]0 h7 l* J    # Recursive case
    ) P, i; c2 X$ c$ p" J    else:
    0 O" v1 m/ W7 u0 H' P3 `* M0 F4 N+ ]        return n * factorial(n - 1)7 [/ h# V; r( B# A

    4 j3 h: g8 B( k" u+ h$ N' h# Example usage! U* {( M1 w1 h6 |0 D' q# k
    print(factorial(5))  # Output: 120
    4 J  Q2 B2 H. t1 g6 r
    : j; d0 T. k6 x# ?How Recursion Works7 r7 k* p2 x. X, r/ g, |2 n9 [

    2 ?/ L7 E6 h% Z7 x, T+ j    The function keeps calling itself with smaller inputs until it reaches the base case.9 T: V6 X& Q+ ?6 ?0 f0 @3 @
    7 e  Y  V; o) B( }* k
        Once the base case is reached, the function starts returning values back up the call stack.6 T5 Z7 R: y% O/ _- B! w) H
    ( k& L, u; A9 n' J+ u9 B8 @+ [
        These returned values are combined to produce the final result.: K+ Y) J: U6 G, i7 `8 ~' S# B
    ! \  f) c3 V4 A# F  w" D
    For factorial(5):! K; ?  j) i, v" F% C
    . G) ^. c3 O# F
    3 j: p/ V( R1 g: u# F2 j; ]0 u7 u
    factorial(5) = 5 * factorial(4)& [: e. d, h3 e; T4 l% V$ l+ [$ y
    factorial(4) = 4 * factorial(3)
    6 P) R  M% Z% D9 G) Tfactorial(3) = 3 * factorial(2)
    2 g# j% V" d  c% H7 tfactorial(2) = 2 * factorial(1)
    9 ]4 S4 T! @, |0 r/ n2 [) j2 bfactorial(1) = 1 * factorial(0)) }  n) V/ @. l" F; z* J5 S
    factorial(0) = 1  # Base case
      |7 l( s0 Q+ n+ r
    8 s7 @! c( s8 IThen, the results are combined:
    ( L/ g/ o$ t! f! ?" K; ~8 f/ J# B- o: ^0 B
    , n% Q0 k! R7 w$ Y8 P+ W
    factorial(1) = 1 * 1 = 1% W+ U1 V/ R: Z  }% y& f
    factorial(2) = 2 * 1 = 2
    - C3 c* k9 Z5 Bfactorial(3) = 3 * 2 = 6
    ; r  }8 m9 O6 M$ l7 ~3 Q( E5 mfactorial(4) = 4 * 6 = 24
    ( o# `: ?3 A; @& B  Z# f, A6 efactorial(5) = 5 * 24 = 120# A) L& o+ ~4 ?' L* f

    $ |4 U: W4 D4 Y' K; p% b" zAdvantages of Recursion
    8 a% }. q/ u: l( Y$ ~; H8 {+ F0 `# t& n9 v: v. M5 z4 f8 v
        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 y/ r; }: d
    $ B' i) b# \& E: J% u0 U: Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.4 E( S! F  X) |: j& ~

    ' Q# Q( T$ x5 wDisadvantages of Recursion
    0 v+ B0 o" R+ `/ N5 J9 \; S! t' v9 F. B3 m7 }5 O8 h
        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.
    3 E% X) o& ^7 ]3 s/ o. z- @' N! s, D0 b( n
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 M7 n- O7 O9 m: Z$ n* y! b

    1 @0 f' a  N  P6 h1 r( F4 YWhen to Use Recursion( l0 m. D5 K) h$ K
    % b5 X/ q, w; N- K- U
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    - |( F! }" f$ E
    # U, ]" Q# O7 s    Problems with a clear base case and recursive case.
    7 ?( _' j, b$ M7 v4 W  S2 `5 }, p, ^# }9 n4 r. B2 d% H2 ]2 n
    Example: Fibonacci Sequence
    ; U2 K; q- F7 ?% w+ Y' h: k* [0 t# X- |' v
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& t$ s' M" F: ?/ j5 B3 c

    : h' }& Y# c  n. F/ E    Base case: fib(0) = 0, fib(1) = 1
    7 T- U5 V- z- l7 v' g3 G
    % p- f, A5 N) J    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    - p1 [" i4 c% W9 \" ?+ _
    ' y! ?7 X: Q4 A& [9 opython7 U9 ^6 ?' F& U- M1 E

    0 X: n; L( Y: `+ b" j
    , _1 @* W9 g0 d4 N+ Y5 O/ z  ydef fibonacci(n):
    & e+ d2 M4 {) k' y5 F/ t    # Base cases& h2 n6 B) C8 K5 Q
        if n == 0:
    & J4 |6 b2 T0 A$ F        return 0
    5 X" J5 D, m1 _  A3 X9 u% F/ e    elif n == 1:
    - ]2 i3 I* G' J$ X        return 1
    - `- `' `: b4 O+ g    # Recursive case
    3 f+ d, h3 {$ [8 \4 A    else:
    & B- g) }- y  M1 K        return fibonacci(n - 1) + fibonacci(n - 2)
    ; Q* o. [7 i5 C& @/ E& A7 _( F4 _* }# Q8 s) f" N& N/ Z2 ^9 r
    # Example usage
    3 s4 g1 C! ?5 N: `7 ^& P; w1 v- x2 cprint(fibonacci(6))  # Output: 8
    4 w" F0 u1 o4 u. {7 A& I- h: T8 z$ o
    ; ^8 i% Z* z4 rTail Recursion
    / h& N' p4 z6 T- L1 Y" {8 b, f* c: M4 l  t0 C2 ]
    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).
    0 U) T( T; N- K8 Q/ f
    . ]1 U* B. N. Z  HIn 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-18 01:48 , Processed in 0.055181 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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