设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 y5 ~1 f7 w, v$ v

    - O4 M  l- u' ^* u9 S6 _( Q% z" o解释的不错
    4 y8 y% D- b9 p' a! r+ D# u5 s0 R- \$ `+ {! \* j
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 o8 n. @7 j# ]" M
    2 r3 c# U4 U4 @* U- S
    关键要素3 L/ U# K" K% X9 M2 {* k) x( ?0 q
    1. **基线条件(Base Case)**
    0 r* e2 K% A2 {. p   - 递归终止的条件,防止无限循环% l5 m9 H/ m/ u
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    / \# w, {( \) g6 e' s7 |! F$ m" K1 Q; R; O" ~
    2. **递归条件(Recursive Case)**8 t8 P' q. w$ w' o8 I9 h
       - 将原问题分解为更小的子问题6 U3 @1 r0 D+ k" q$ G
       - 例如:n! = n × (n-1)!
    % I( s. s. i, V* U0 ~8 d0 H& H/ N3 a+ C2 g: f" o0 R
    经典示例:计算阶乘9 g+ }1 L1 T, T, p% s! q6 [  ^* y
    python
    ! f" T& ]1 G1 xdef factorial(n):
    0 _% P, n' a7 P  c1 j% i, d    if n == 0:        # 基线条件
    2 Z+ u) S& x. o5 ^7 v  V        return 1) Q# ?- V" C  i& I6 k& Z- `! y4 D
        else:             # 递归条件
    : T+ G6 _  ~/ t        return n * factorial(n-1)3 L0 d: \: i, p. g* t7 i% I
    执行过程(以计算 3! 为例):
    $ ~! f9 r0 S8 k' w* Tfactorial(3)
    / s0 ?0 H2 `! q3 l  N: t3 * factorial(2)7 H# z+ s+ |: r* B% h4 J
    3 * (2 * factorial(1))
    ) X# l$ `- J0 D7 m; \5 K3 * (2 * (1 * factorial(0)))
    7 _& X* f/ U6 }  W6 D9 {* O3 * (2 * (1 * 1)) = 6+ o- Y7 H. {, D) x- q% k
    7 q$ w8 l* G. r. C1 c; y
    递归思维要点) |  o9 n6 v1 g, v) _7 w  N& X8 R# ^
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑( L- |" |( r, \- Q9 |; m
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    / |5 l& M# y) {) f. F* h- n7 n4 y3. **递推过程**:不断向下分解问题(递)' u5 ?# b% S/ r# K1 @5 n. [
    4. **回溯过程**:组合子问题结果返回(归)! U6 U/ s9 T$ N- C$ \9 W4 o; b0 e
    ) P# N8 F! p( \# \, p3 n
    注意事项
    5 i' d1 W% `7 j' q# Q5 ~  p必须要有终止条件
    ' l  L8 B& ~3 p2 q+ ^递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 x! c7 x( H/ Y% m: N! T
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
      W4 ], G8 e6 M+ \  i3 N/ m尾递归优化可以提升效率(但Python不支持)2 q8 D! J8 T: k
    ! P1 w( c; b( s7 g1 O1 T+ p* V
    递归 vs 迭代  `" d4 s! R9 y
    |          | 递归                          | 迭代               |6 |. M+ U9 `4 i
    |----------|-----------------------------|------------------|! y6 g3 ^% K1 r8 W  d/ Y. i- B/ I
    | 实现方式    | 函数自调用                        | 循环结构            |  b0 f5 H0 }4 N7 x5 q/ z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    4 O( ?7 }- e9 S- j8 R+ `) ?3 E| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    7 t* E3 _1 W3 V( e) O" @$ E| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    1 e4 L$ |# q% j) Y; p8 K9 E! h5 O& C
    / W. P$ B, E% l7 y' P/ x: T 经典递归应用场景
    ' v6 L# k7 A! F7 Q* K2 A% E# m1. 文件系统遍历(目录树结构)
    4 C# w6 f1 @/ r& [% k& E2. 快速排序/归并排序算法+ o( C: s. q- \  o& r
    3. 汉诺塔问题. o, B6 r$ D. X$ u) C
    4. 二叉树遍历(前序/中序/后序)
    * ]* d8 P( Y  z. s  O( X5. 生成所有可能的组合(回溯算法)8 D- n6 _+ b3 K+ Y0 d% W

    7 [! P. N% w) l6 s7 o$ E& z7 l试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 15:11
  • 签到天数: 3133 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  A5 _6 Y+ A! ~# B# N8 Z' I
    我推理机的核心算法应该是二叉树遍历的变种。0 ~5 r5 T3 M$ r  G
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . y6 f9 x- k! j7 g- @+ O! LKey Idea of Recursion
    0 _, e( c* v* R- p" `& G1 _, X( ^4 z
    A recursive function solves a problem by:- q& `6 c. p" i' g3 r, U

    # A8 D! s+ h+ Y  x, T# ?    Breaking the problem into smaller instances of the same problem.
    9 W. a) a* k( g' ]0 `0 d
    ) u: F' {" g- t& e    Solving the smallest instance directly (base case).
    # a1 R4 }) X/ I# [6 \9 X* d8 m+ o  U6 v( ^+ `
        Combining the results of smaller instances to solve the larger problem.
      I9 }$ i' h' P& R1 t3 v2 ~
    8 X2 v2 T, p7 ^Components of a Recursive Function
    ' d  Z' r% }. B
    2 c0 s, }! L$ A3 e! i% c5 C$ n    Base Case:
    " i4 W( ^. U. h
    0 n* Q5 r: J6 ~9 t# E. i1 |; Q        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.. r: R  e+ V0 ~* r
    : [6 r" B9 Q" k- U. K
            It acts as the stopping condition to prevent infinite recursion., Q) H) A* e6 `* }+ K* g, u
    % g* `, v& ~4 v% U2 s
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ) w/ c$ B) F$ `/ o: n, q  \5 `- N7 t
        Recursive Case:+ c0 D5 K: q( X' Q$ R- ?( u/ C2 `
    - o1 C, h! S3 i5 i
            This is where the function calls itself with a smaller or simpler version of the problem.
    , n$ `9 F# v) q; r4 Q1 t# R! v3 X* Q8 W0 t
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).2 C6 `  `: }6 N6 B2 m7 f7 H, M

    0 I8 c/ Q  {2 ]3 j: RExample: Factorial Calculation
    " A0 p' d5 G' o) k( n: g
    0 V/ e3 i# q, k4 P7 G2 ^( Z3 VThe 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:; \7 k4 s" [, i

    & Q- |' k4 C. \  ]    Base case: 0! = 1
    2 P: ^7 A1 O8 Z; f. Z; ]" p/ X; g( r5 |% Y: d8 u
        Recursive case: n! = n * (n-1)!
    ) M! C* x7 W7 _" p7 D2 k4 [/ f: m+ a  P" r" Q
    Here’s how it looks in code (Python):$ G5 ]/ o* W: ~9 H" |$ M4 Y
    python  ]' P( }& x5 D; D

    0 X) I% b9 Z5 y' m% Q7 A/ ?* T" I8 G6 d( q5 ^5 ]0 V0 n( d
    def factorial(n):0 `2 O  a$ E! S3 e  Y3 R
        # Base case2 n* w5 \6 W6 w% N% e4 z( M' f. [( _
        if n == 0:
      {2 o/ _, t1 ^7 ?3 \        return 16 T1 f5 q! c2 C( I$ \
        # Recursive case: H; M+ F* M7 l
        else:  ^( B& b  W6 O- m6 t0 X
            return n * factorial(n - 1)' m% ?* b; w, _. s
    0 z/ v, h" m3 \7 U  e4 p
    # Example usage
    8 ?( v1 V- g5 y) e, R" C+ ~print(factorial(5))  # Output: 1205 S2 {8 a! H8 r* ~1 c4 Q, [
      f1 V1 u% z& I" ]0 M. D- V3 U: Q
    How Recursion Works5 Z: g3 t" y% l, N! E
    9 g, t4 p0 C* H- C
        The function keeps calling itself with smaller inputs until it reaches the base case.* O0 T% F$ g2 `4 x6 W7 q* O

    2 i; t8 W1 t# r( S$ `4 ]4 q    Once the base case is reached, the function starts returning values back up the call stack.1 v! G5 Y5 {! j# Q2 P+ q

    - ?: }, s: n  g    These returned values are combined to produce the final result.
    8 U0 H/ f3 G5 S$ Q
    ( z9 @; [$ u  _. B( R7 MFor factorial(5):8 i4 V! M/ ^( e, n/ O

    ' V' |: J; x5 v1 S6 m5 l( w' O! s% U( k, z6 Z% A) c
    factorial(5) = 5 * factorial(4)' N) @" z4 N! v( U/ Q, `* j& K3 P
    factorial(4) = 4 * factorial(3)7 f: l  C0 e* D+ w' E; ]( X/ Y, |, A$ r
    factorial(3) = 3 * factorial(2)3 J  e5 `/ E6 c9 ]! e5 n
    factorial(2) = 2 * factorial(1)
    2 N2 b) `% i/ m8 S$ Z8 Ifactorial(1) = 1 * factorial(0)) v$ i3 X( m( d* g$ e7 f$ \9 y
    factorial(0) = 1  # Base case# ^% {1 f$ Q+ r; k. \' D) A# z
    , n% Q" Q- Z# o
    Then, the results are combined:
    - e+ H. r1 p! t5 |3 [* f( F/ @5 [8 n2 z. W( t! }" L1 O+ p  Y5 ]2 U3 ^

    - }/ ^6 X& |% j; h' xfactorial(1) = 1 * 1 = 1
    $ l" N! V7 y9 J! ^3 l$ S* P/ T4 p6 ?1 pfactorial(2) = 2 * 1 = 2
    + V# {8 b# h2 z' K/ ~7 R, Hfactorial(3) = 3 * 2 = 6  i+ f4 c' v$ N/ f: _
    factorial(4) = 4 * 6 = 24# J; J: t1 c* e3 d7 i
    factorial(5) = 5 * 24 = 120
    - w5 k& Y3 W. x
    3 v* A" f) e- GAdvantages of Recursion4 C" Q! U. s2 k

    1 [4 K0 U. 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).
    / v8 j' t+ x: S+ T
    7 M- c6 K' e1 @7 b0 w7 Q0 n- W    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    % h5 U3 X& v7 N, \; S% N
    5 f1 e# ^. S  I1 E  ]$ U- FDisadvantages of Recursion5 |1 ]- B  H1 S# d  D
    + r4 C9 v2 U/ [: @2 z
        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.
    / {4 J. U+ H4 c9 h
    + K& Z6 }# Z5 i' k$ q- z4 s3 b6 K    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).  X! D  Q; k2 m& t4 I( t2 {( a
    . ^% N- r4 y& p+ `' w5 q+ b
    When to Use Recursion% h8 m, R- m; K, N8 P. x
    8 h6 t$ |; i5 C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    8 i# _/ h. q9 H
    4 y1 T& E" d0 D1 h# J( r9 L+ E/ y    Problems with a clear base case and recursive case.# {3 s% |* D3 r, j/ D" a

    6 H: z; @9 _# vExample: Fibonacci Sequence2 {7 `( R( R/ H

    ! u) {! r, h6 I+ p, F! _The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    5 j8 }' M9 r: o* U3 A: u) {# s/ A/ L, H2 x( s  l( `" b/ }9 d
        Base case: fib(0) = 0, fib(1) = 1
      ]3 r, Q9 u6 M7 p- d: [7 G) ]/ c- `( ?# T
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    2 M* g0 a9 F% p- }$ R) Y4 g: q, a
    python1 b/ N8 |! t+ ]% ^/ p
    / m) }8 e/ o, B1 f9 }

    ) E7 e2 k" k4 |4 E0 l& ]& rdef fibonacci(n):( ~' }9 `& E1 b3 t8 W6 L
        # Base cases! z- J6 q& k) ?8 _( u2 F
        if n == 0:
    % D% f+ ?* K: K1 J        return 0. ?' Y0 {  y2 H' @
        elif n == 1:' F) E7 {+ a* x- K
            return 1+ |; j. v5 ~5 T8 K7 q
        # Recursive case* S3 i3 M/ ?5 ^' Y( I& t& j
        else:6 f$ C. y2 T6 [+ v$ j) f) }: ^4 p
            return fibonacci(n - 1) + fibonacci(n - 2)
    8 I7 T4 X; p9 B/ J2 W/ G, `( I3 Q- i2 C' K
    # Example usage
    # i5 r7 p2 Y" l( f' dprint(fibonacci(6))  # Output: 8
    0 s: L3 y. [# M, P) t( a9 V. k7 R! }+ @5 Y$ e) S3 g# g3 C( o" ]* A/ p
    Tail Recursion
    + l) u& G& m' s6 h9 M* m- P- C! |
    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).* |% ~8 @; T6 S0 B7 ~1 Y

    ; G, j, G. `. C  @# p1 G$ mIn 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-2 04:18 , Processed in 0.029655 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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