设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    + }0 t( v; r8 l. ]$ m( Q0 N0 d9 i* Y
    解释的不错
    # v6 }2 U/ u7 d4 X( }6 O
    ( N- x: m1 n, i% p1 g递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    1 b, P1 B# m' A& A$ f$ }3 `1 V7 H- z
    关键要素3 O6 w! d$ U$ t6 S6 {- f0 A0 v7 I
    1. **基线条件(Base Case)**) c9 D5 S% o& f0 }
       - 递归终止的条件,防止无限循环( ?9 J* x* r0 D: G6 [6 |
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! m& F: C5 V% k7 g! n9 A
    ( U' F! \, p# i% w: L6 c, b- K! X
    2. **递归条件(Recursive Case)*** m7 a" k8 Y( z  s" Q4 n6 X! x) T, a5 E; D
       - 将原问题分解为更小的子问题
    4 _4 ^3 W$ P4 N3 @. k: |7 V. S   - 例如:n! = n × (n-1)!
    7 q' }3 _: e* T! E! S3 s8 z" o5 m* b- ~
    经典示例:计算阶乘
    ! y8 B! `% i1 f: q+ M- c' Y' hpython9 l% V& _6 b/ y9 F
    def factorial(n):
    " a% q3 q2 N- P$ Z. i! v1 G    if n == 0:        # 基线条件
    , y2 W, f. o  y) m% j        return 1+ f* A8 X/ F! H7 y' H6 @
        else:             # 递归条件
    3 i+ F* K( A; e$ W/ V  p. U5 b        return n * factorial(n-1): W3 k/ t& B/ Z5 x
    执行过程(以计算 3! 为例):
    2 v+ p3 |: i+ M& z5 F( t7 Tfactorial(3)0 V, s) q6 t+ c- Y) x) @
    3 * factorial(2)+ ?- i9 a2 c1 p* m6 V+ `
    3 * (2 * factorial(1))
    0 m2 v) s* s# X* w9 z3 * (2 * (1 * factorial(0)))
    # ^, r/ t  D; W8 ?2 k' q- F0 L3 * (2 * (1 * 1)) = 6! Q( a3 h' f$ R

    . g/ g  e/ F; h& Y7 l* M& A 递归思维要点
    ; m3 G4 Q0 K3 H$ c1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & {  ^8 W* z9 r& K5 o; s6 `8 K2 ^2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ' w+ E/ f: d, h" w  d3. **递推过程**:不断向下分解问题(递)0 L6 k/ {/ U1 |3 X# E% E# M( k% B
    4. **回溯过程**:组合子问题结果返回(归)
    2 {% K6 p: b8 }* K" T( Q& N) O2 u. o$ z3 F
    注意事项
    ; y4 [' W, v# \+ p% W2 ]必须要有终止条件" Q" a" b' r: ~( z& ^' W! _6 N8 L/ O
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' C' s' H* P# X. e" P某些问题用递归更直观(如树遍历),但效率可能不如迭代* G% ^; c8 k# C2 u
    尾递归优化可以提升效率(但Python不支持)
    $ P* P, r# P& E" h
    3 ~: j1 \1 U# P% v* P- L0 S 递归 vs 迭代: d8 x+ Q% Q' D) k6 ^$ T  r; @. u
    |          | 递归                          | 迭代               |" E$ m/ [5 X# z$ O6 y
    |----------|-----------------------------|------------------|
    % O' b: I  ]; z  c4 S/ Z2 i| 实现方式    | 函数自调用                        | 循环结构            |. c  [( I8 T8 U0 L6 h/ [
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    6 I" e5 {: e* l' s| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ( R- \3 l- m9 Y7 M- i| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |: Z7 `$ Z6 k2 }1 ]- V. D

    2 q) ?7 f6 B4 n 经典递归应用场景/ B3 a% x* C  _2 m
    1. 文件系统遍历(目录树结构)
    - r4 f/ `/ h  i0 P! {# |( [2. 快速排序/归并排序算法
    2 ^- e4 F) ]( U) }& V1 [+ S* f* M) B3. 汉诺塔问题
    4 ?8 T3 ?- ?  F: x  H0 F; [4. 二叉树遍历(前序/中序/后序)
    9 |, X% C0 i2 T! ~5. 生成所有可能的组合(回溯算法)
    0 T2 ^2 ^" i& \) Q' U! n  i" Z( |2 @# u1 R9 Z+ f$ a
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    15 小时前
  • 签到天数: 3138 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    7 O% l% _' \1 h6 W我推理机的核心算法应该是二叉树遍历的变种。; B7 d9 z: w# T" g5 m  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:
    ' s) R( O! y1 E, J7 e: gKey Idea of Recursion
    & ?4 V( f0 t6 F% p# x7 R: R! j- H
    A recursive function solves a problem by:
    4 ]6 M0 m9 q2 u( B9 m, I$ D- Z# x" O1 x# b" K8 v1 _+ U
        Breaking the problem into smaller instances of the same problem.
    # T+ L1 `  T7 `# c/ s2 S6 w' L5 C$ G1 I7 K
        Solving the smallest instance directly (base case).
    ' q, V7 s% H  }7 \
    % C4 w/ n7 r9 M/ |) }- }    Combining the results of smaller instances to solve the larger problem.& \/ f" N% s$ j% h! x; J

    ( \6 ]' \* T4 RComponents of a Recursive Function9 O5 T& k- n/ S8 a, X+ V* v9 v

    , W8 U) h! D9 v, p* [    Base Case:
    - a( F+ U1 I  p( C+ D5 j' j$ u/ L' x) l
    8 u: ^8 u! L6 s( x        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 w/ u9 {8 w9 E
    * m' T, s. i* q* t) C* f+ h        It acts as the stopping condition to prevent infinite recursion.
    3 \. ?7 ^& o$ P+ k8 z7 A! X
    # f  s3 }& Y, {! z! W        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! ?4 _5 d3 \+ Z' O+ k
    % ]- A/ {0 J/ Z; Y, s
        Recursive Case:
    ( W/ U: O- a+ S
    6 ]# x9 t5 P/ J4 M3 x/ \1 ]        This is where the function calls itself with a smaller or simpler version of the problem.
    , I, Z9 H- Q* l9 l3 B& k  ]0 E; w; N! M8 V0 u
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 f4 m1 f6 J0 p! t" U7 w: a

    6 X; D/ z# t6 q( PExample: Factorial Calculation
    2 F0 X( i2 Q. f0 e" D( e; C4 p/ I: 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:
    % {2 A) ~( t0 V
    0 _$ L+ k$ }7 j: b    Base case: 0! = 1- F% B  ^9 m) ?

    ; |& @5 L" @  r2 P! l; c/ c. B    Recursive case: n! = n * (n-1)!# x1 @. T9 M( V- v8 y# G

    " p; @4 V' \; \: w2 t0 dHere’s how it looks in code (Python):: g  \, R, C0 j6 a, |0 e+ j6 [
    python
    1 R- Q& k" K& C2 g
    1 c& a0 q$ z! T  Y
    ) W7 K: l) k4 S; Bdef factorial(n):
    3 [  O, F/ f5 p% O  F    # Base case
    6 i# M, j. b4 `/ _: l: ?5 h    if n == 0:; m3 \* d$ {* h, m6 c
            return 1
    0 a8 H( P) L* w  e" y6 |0 C& W5 o( B, c    # Recursive case1 p1 \' X$ ]  n2 f
        else:, S6 _  M6 Q% f- p2 V! }2 q1 l+ e
            return n * factorial(n - 1)2 r3 N$ b5 O8 n
    2 M  T( l- L: r& t
    # Example usage
    % f4 v6 Q: e. U+ z+ Sprint(factorial(5))  # Output: 120
    ! F: Q, I  v/ a+ P
    - V+ I- ~7 ~7 B: e- NHow Recursion Works
    " G, c# y, @7 A1 y; t1 B
    + F$ U: L! x: O( k    The function keeps calling itself with smaller inputs until it reaches the base case.
    : O  h6 x$ ~& `- H" o" L
    4 S% q; a) W" y/ H! {& u& u, E    Once the base case is reached, the function starts returning values back up the call stack.# g9 [  O( ]7 b+ v; E

    $ |9 b, K7 r( F+ ^7 |    These returned values are combined to produce the final result." {% N5 X0 p' b) A$ j& Q
    5 ?, F' G: P6 \
    For factorial(5):7 F7 o; W, N3 `3 C( z2 t) y, x/ o6 z

    6 B0 r& g# |/ e7 D, s7 I* [! [) d8 U: m' s" L
    factorial(5) = 5 * factorial(4)
    6 {+ u" \. k$ P' Ufactorial(4) = 4 * factorial(3)
    2 N7 v! a! h  U8 l' vfactorial(3) = 3 * factorial(2)4 H$ a6 b7 J1 @+ y& Z9 J
    factorial(2) = 2 * factorial(1)4 E3 B( J0 g. ^# M$ ~$ v2 o
    factorial(1) = 1 * factorial(0)
    " Y  Y! E! x( v" a( A4 Wfactorial(0) = 1  # Base case
    : B3 X/ @: K( p6 Z, m; C8 b; y$ w% ]! M
    Then, the results are combined:3 R  w# P0 u9 d1 ]6 C
    ' N7 Z. y* f2 u( U/ n( l

    ) e+ e5 i( f; o* t4 O, sfactorial(1) = 1 * 1 = 1
      p" X' [) K8 o/ Nfactorial(2) = 2 * 1 = 2
    / k; |3 W9 Y% d8 Ofactorial(3) = 3 * 2 = 6
    : m" l( z+ y- X& m( K! }& w% i% A6 V; lfactorial(4) = 4 * 6 = 24' ~( M. j: j( z) [. f2 E$ p
    factorial(5) = 5 * 24 = 120
    9 v$ A: i) S; ]7 k$ o) O; n& Y0 ]2 F7 M
    Advantages of Recursion
    9 u+ V1 n) J. r& v: |$ D* U" ?, g4 F" H+ H; ?7 i4 G( L6 c, h
        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).
    5 \+ |+ u3 S. R) a# u. J2 l4 \: Z7 J; |  t) H1 D
        Readability: Recursive code can be more readable and concise compared to iterative solutions.  b1 R$ M# W. ?

    # _) {6 [% t! @5 d& K: r! i- f& GDisadvantages of Recursion* l# {7 g4 f# e$ c1 b

    # N* e# _4 S1 b: ?, L1 F) ]    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.' M2 ]7 H) ?6 U) M* f$ C

    ! w* f  @( j  J- E& F    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 Q) _4 v2 E: |! R$ {- K# e
    + k& w. ]! ]9 l$ N& G! `When to Use Recursion
    5 G) Z0 g. T1 X2 {9 [1 U+ \) S. Q" o$ j
    7 r) ~4 ]2 \  w3 N    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + g) Z7 B( V  I6 w4 B# B
    7 @( `5 H4 N: u    Problems with a clear base case and recursive case.1 s' ?) W* z8 W" Q" I7 e3 F

    $ s/ v  i$ u% U+ {- p; D! SExample: Fibonacci Sequence
    4 ?- k* O6 @. R/ b2 I- v/ Y& |: G/ ]* z! R! O7 ]# S8 X0 r
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - J- s6 a( V  ]9 @9 K' T( H7 Z( f# ?8 `8 \$ k
        Base case: fib(0) = 0, fib(1) = 1
    4 X$ K% h. S+ i. Y( L& M! |7 U5 x+ n3 e  X& _. r  g* G# w$ c
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    : ^$ P- o3 R. x6 P+ Y( O: B; i
    & Z+ v+ s8 c; O7 q. c0 \python
    ( E* `$ }  c$ z. y- Y5 d
    8 T" _/ b: A; e$ z) f- z$ Z0 k$ x: ~) ]& o' d
    def fibonacci(n):+ E5 i! w$ x3 \2 C
        # Base cases
    8 ]) O- L- e+ y  T    if n == 0:
      L+ ~7 T1 a( |/ w        return 07 @. Q, N2 v1 S1 S% y; R, h
        elif n == 1:
    + e, [& Z1 X' B  p1 U; _        return 1
    . f! M1 T* p% E* l    # Recursive case& D/ L" Z8 ]9 g
        else:' G2 [" m/ M8 x: K; B6 Y
            return fibonacci(n - 1) + fibonacci(n - 2): E! ]: F, H* b% O5 H
    5 E7 ^4 Y3 Q3 h7 d
    # Example usage
    & K* Z5 F! s( L: A% H& K3 jprint(fibonacci(6))  # Output: 8
    ' t6 L* N0 f5 u% E1 L# P  @/ ?
    $ J: f8 d0 S! G- M+ l3 W6 M: I2 R( fTail Recursion
    ! N, Y, H+ [6 p% |/ g5 E. S& p3 t% o5 a" \5 P7 m% }6 G
    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).
    1 K5 {9 b/ h2 c4 Z% [
    0 u4 a1 Q' s& {! h6 r0 L- n$ V; zIn 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-1-6 21:43 , Processed in 0.036795 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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