设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    - e8 e' c6 \) E5 U7 B6 m
    * }8 q* m! s) k! F解释的不错; y3 w8 s. m6 d: N8 y" J! a  W

    ! U8 `" `. c3 n9 f递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & g- u7 n: r8 `7 ]% m% C
    * c$ \) J" A3 i- X 关键要素
    1 X9 G0 z3 q) n. E6 h- M1. **基线条件(Base Case)**
    ' _+ s" C0 z& o8 q/ g   - 递归终止的条件,防止无限循环7 Q' A* W" Q' ^1 g
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1& s* r1 P/ C8 e' f$ A5 l( D

    / ~! j& U# P: u& K7 x7 H2. **递归条件(Recursive Case)**2 O# C* R! {4 j$ ~
       - 将原问题分解为更小的子问题
    9 [% w; V( W+ R/ C$ G4 b. O   - 例如:n! = n × (n-1)!0 C3 Z% Z, c5 w1 s
    / H! f5 s. \$ [# Q  x
    经典示例:计算阶乘
    ; g' o1 i' L5 a- e0 Vpython
    $ m) O# ~3 u' l  ~7 pdef factorial(n):; j1 \1 g. g$ T0 E( P) B
        if n == 0:        # 基线条件/ b& j  M3 U; i
            return 1( @5 N$ O, A  Q4 a. ^0 f5 M& H
        else:             # 递归条件  L5 J$ T" b) U& n2 R
            return n * factorial(n-1)
    9 D1 X7 p2 V0 M9 I6 x7 p! x# B1 w5 _执行过程(以计算 3! 为例):
    . B! k* |5 p4 e/ l5 d3 t: [factorial(3)" D1 h& F5 R1 B& M( `+ T: c) I
    3 * factorial(2)2 y/ e  [; D  R% j
    3 * (2 * factorial(1))
    : C% F6 c1 a% ?+ P* l3 * (2 * (1 * factorial(0))). D$ F  J6 k$ L) _; T/ o
    3 * (2 * (1 * 1)) = 6
    ! ^4 n: H1 V# k% u, [; x/ S* P$ d4 [' ?* G7 T5 j
    递归思维要点
    ( ^+ @4 K  |) J- }+ x' ]1. **信任递归**:假设子问题已经解决,专注当前层逻辑' @. R1 h4 s2 m) g( Z1 v$ z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" H7 ?2 d/ u, h: |* n, Z
    3. **递推过程**:不断向下分解问题(递)
    9 [6 Y$ P- r5 _6 r: i1 n( s4. **回溯过程**:组合子问题结果返回(归)
    ) H; A) Y& V- r7 j2 l- [) I+ I5 r6 e0 s) v6 Z
    注意事项
    ) J+ k( ~: V' s# [1 u# S; d8 A必须要有终止条件5 }" y8 C/ p, |% q/ Y% M1 k
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层), [: C4 s. {, P0 R' [
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    6 ^$ P+ q; s  |$ G- G尾递归优化可以提升效率(但Python不支持): q5 }3 ^. }7 b( u, Z

    , O: M" k* o) y- w 递归 vs 迭代
    # E, V+ ^$ E$ R" T' d# ~, ]3 M|          | 递归                          | 迭代               |& l+ P9 I; p4 h. ?; t! `
    |----------|-----------------------------|------------------|% }) L0 l1 a% s7 k" \- ~* G
    | 实现方式    | 函数自调用                        | 循环结构            |9 E$ J. G' k% c+ m
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- j! c; L+ d  H' i8 E) o9 Q
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |7 \5 j( y9 X0 I% [6 z/ F. @
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    : y& ?- ~* H. N7 m* A, O8 K
    2 |: R1 F' @5 ^9 c 经典递归应用场景
    $ j* m" N* }3 m: m% ?% s1. 文件系统遍历(目录树结构)
    % H( [& c* @5 c. k& N2. 快速排序/归并排序算法1 E' B( R* h2 h$ Y4 l. ~5 M( H
    3. 汉诺塔问题8 d' J+ Q( C+ h- k& |0 V- b
    4. 二叉树遍历(前序/中序/后序)
    ; o" r) \2 p  _. y9 C! Y) b6 J5. 生成所有可能的组合(回溯算法)& R7 w5 F+ v' T

    0 V5 j; a6 y& R1 Y, \5 G$ a试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    4 小时前
  • 签到天数: 3113 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,4 v! f  u6 K* ~
    我推理机的核心算法应该是二叉树遍历的变种。
      t4 |+ ?6 ?5 @: D9 r6 D3 X, ^9 l另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; C  Q* `' F9 _/ e0 R
    Key Idea of Recursion7 f9 Z8 W+ i; h5 p, C/ {* D! m. i% x/ Y

    / I% J4 ]. f: A, o! l* \A recursive function solves a problem by:' F+ f; n1 X# v2 S, T

    ( `' N! v" n& c( e! @) T    Breaking the problem into smaller instances of the same problem.
    6 F" O% g1 I8 _2 I1 [! |7 p; u, u, }/ f& C
        Solving the smallest instance directly (base case).
    3 i6 a# e/ ?) B- O% H( B3 ?9 Q2 w# V, \( {# \) p$ _' J
        Combining the results of smaller instances to solve the larger problem.
    " K5 u, r5 T% F" c! I& N8 q
    / Y- Q2 s: J1 {" {. d5 E6 M6 L% xComponents of a Recursive Function. A4 O3 E: \9 U& K* ?5 }
    6 D) j! f. S2 r/ E9 u& Q, r( s
        Base Case:
    6 g$ j. v' n" i& Y1 @4 l
    " x# Y+ j8 S, ~' e& T& N' |  t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( g" e9 R2 @( A8 b! x7 b- [4 f8 d
    * s1 X9 b) {6 B! {$ z2 m. j* `        It acts as the stopping condition to prevent infinite recursion.
    7 C5 _' [, e6 l9 Z
    / F0 b) ^# ?% ?$ @) H6 v        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    " v. Y" j0 U9 s* ?( [2 K# V6 ]- P, A+ I
        Recursive Case:
    ) f! N" {' {' m' _( k
    / B- {" K) \# A& q        This is where the function calls itself with a smaller or simpler version of the problem.; ]3 i' g5 V! ^* c

    : s7 ^1 M4 E, ?" b2 w0 U0 v# Z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    1 w4 `) U7 n9 x$ {4 M$ c/ d6 ~, R- C: e
    Example: Factorial Calculation/ @# F- o' _! c. D' F
    3 V' r1 s7 s2 k% |9 ^) y7 R
    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:- v* l. [' t: T
    : L& m) x  h/ K: C/ K3 a" E+ v1 w4 n
        Base case: 0! = 1- N( B) r4 x$ ^+ F: U; V7 s- e

    6 U3 J% A' t# B* P  f. `2 Y2 k    Recursive case: n! = n * (n-1)!
    9 k+ W- J- Z  T* ?, y: C
    4 Y# ]* j* c9 ]6 r% wHere’s how it looks in code (Python):
    ( s% _9 q5 p. O( e7 K- H# D( k& ^1 M, wpython/ b1 v2 I( K% D( N& t0 c

      t9 R" _( g& D
    8 e7 S! O4 m! cdef factorial(n):5 [- a& C0 p" S1 p( k& t; R9 J
        # Base case$ R' S3 m/ R' b% M2 `' F
        if n == 0:! r) |# m: @+ ]' ^, [6 N) Q
            return 1: ]; l5 D; ?5 u4 n, s+ }% q
        # Recursive case
    # o3 J  r' K% D    else:) z2 {$ U0 e: I* X$ @% B
            return n * factorial(n - 1)
    ! A+ E% F5 U2 j7 F4 k9 i$ S% X( c" G5 L- u$ }
    # Example usage! T+ p% I( q& O& g3 r6 {' w8 h
    print(factorial(5))  # Output: 120( Q- H8 ^+ Q5 B

    8 u' _7 N4 m- wHow Recursion Works
    ! p. q* F8 Z  q6 {6 S, E! \, j  o) [! v! a  ^
        The function keeps calling itself with smaller inputs until it reaches the base case.
    " R" F- v: r; G& e
    5 u4 ^0 Q# S7 {- _& c* D8 O7 S' l    Once the base case is reached, the function starts returning values back up the call stack.
    * H( Q; D& D8 L6 {5 R  E3 U3 i" h5 T' a2 A0 e3 w6 T
        These returned values are combined to produce the final result.
    ' t/ y1 y" q. c: O! \# I: J7 z+ `
    For factorial(5):
    - [0 T# \+ @, J1 [  |
    ' J2 q4 j/ {$ U3 z9 ]- l! [9 g. S3 |
    factorial(5) = 5 * factorial(4)
    $ ]0 R! Z" x' W; Y: W: [- R) r0 Dfactorial(4) = 4 * factorial(3)
    * p9 }& k  p9 S# k: S8 O( Mfactorial(3) = 3 * factorial(2)
    ) P; O$ ?" U) X9 Xfactorial(2) = 2 * factorial(1)( J, Z5 ?& w$ _" K1 k4 _
    factorial(1) = 1 * factorial(0)
    $ w9 }8 ?1 h# N$ Bfactorial(0) = 1  # Base case! J% e* y9 p" p( x6 ^, Z

    ; B8 c7 y* ^+ Y; n1 qThen, the results are combined:
    3 p! M7 G6 Y- y! C/ F, v$ t+ T# _9 O3 R% V" M

    ) f9 V" I  y( _: A3 c% B  Q; {factorial(1) = 1 * 1 = 1
    + j" w7 Y4 U; Tfactorial(2) = 2 * 1 = 2, U4 ]( C) e& g; Y! J! ?  \) ?
    factorial(3) = 3 * 2 = 6
    ; U5 F  u2 v$ E6 G+ C" G( }( xfactorial(4) = 4 * 6 = 24
    + Q4 M  u; i5 n" H3 Xfactorial(5) = 5 * 24 = 1201 s4 n% u& V  `* T
    % |( M: J8 i% S
    Advantages of Recursion
    . C2 ^4 t2 u! s- `8 S. u# E  ~$ S1 n) F( J% n' S8 L
        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).
    ' R0 T/ P; a% J: W7 ]3 _
    4 J0 s- m) v2 z) f' o$ I5 |1 V    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ( {$ ?6 O2 P4 w# n
    : M; F% ?1 c& b- S: l- jDisadvantages of Recursion
    * I0 b3 C) E. g+ |
    ! w2 Y" A3 |  }- L    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.) N6 ?9 D$ e9 H! L; H6 |9 q
    * D' R; `. m+ x
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. f( a& C, F& E# V& J1 r
    " f* x# {$ B5 M  m  a. \2 I. I: w
    When to Use Recursion8 q( v/ Y" n1 _$ P2 L
    . C% I$ S- p' A+ c
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( y( S2 T, ~+ t5 }
    ) K: M! H* C4 E. U0 |
        Problems with a clear base case and recursive case.
    ) n/ `, ~$ ?9 w" ^' v# p% @5 g8 z! J6 i
    Example: Fibonacci Sequence
    , W# Z% B7 {' W% o5 l1 Z' ]: K# B% u5 Q$ ~; E+ `! e4 }
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    : a* F" y# P: s- r3 |  [8 L1 Y0 p* x
    5 S- A9 h4 I5 b  j9 n4 J3 Q    Base case: fib(0) = 0, fib(1) = 19 r6 n" W& I# H  L. s; F

    3 s( X5 W" p# H# ]9 V" z7 k    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 }0 H7 P& O5 t* z% V7 W$ }0 o7 m
    8 Z' X& I4 b% f: q/ `python
    8 r) H* L3 Z4 Q/ P6 e
    * b9 w5 M/ b4 n3 _' b1 F$ d( T
    def fibonacci(n):5 d  ?& k2 R2 ^% K% Y! M& i
        # Base cases
    4 b; V) `3 L6 K    if n == 0:+ X' g/ G" d) l5 b6 D& O
            return 0
    # d' v1 e5 `( s/ b4 B    elif n == 1:% F6 I1 [4 ^9 i3 u. a( j
            return 1
    : T4 ]! C' \+ H% F    # Recursive case" s( H8 o$ Z$ s5 R/ H
        else:- i7 o7 L3 \+ q$ A* a( {8 ~
            return fibonacci(n - 1) + fibonacci(n - 2)
    ) x3 ^7 S  q( @* c5 O: r: }$ X! c9 n. }% V
    # Example usage
    3 }0 `4 ?3 @$ `: M5 M' n3 zprint(fibonacci(6))  # Output: 8
    4 E. l  [! O  l9 M. k
    + X/ `8 h1 U2 G! eTail Recursion
    2 }- m7 E* H5 _  L/ @  y+ v" D( f0 A. D" \2 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).
    ) _. X1 w: U# h, X' }
    4 o1 L* J1 Z$ RIn 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-9 12:02 , Processed in 0.030670 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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