设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 Z1 y# ]/ e! i
    2 w  ?" z7 m2 c解释的不错
    6 R. Q8 f* m/ A/ V0 y/ |  X
    5 R! l4 ^; [) G# L$ v4 j& ?, `递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ( e) M0 E) c" p7 B; g+ e
    3 o, X" d( `' A1 d' E  J/ ] 关键要素/ t0 Y% L8 c  X7 R* ?- _/ `
    1. **基线条件(Base Case)**0 m6 i: ?8 Y! i6 r
       - 递归终止的条件,防止无限循环
    " @" K2 i) z+ |6 N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ! Z& l$ ?" o  j  V# D' m0 O) g6 S0 W/ n( f: Y: `+ G+ l
    2. **递归条件(Recursive Case)**
    8 }0 d. I: _' j9 q& J3 H   - 将原问题分解为更小的子问题
    & M- s% H& P4 }$ ]& d' x   - 例如:n! = n × (n-1)!7 k3 L5 g, A1 ~7 W. p' _9 Q

    1 \0 p8 t2 _- I 经典示例:计算阶乘8 u3 z" `. Z7 D: a9 @! }; z% |6 w
    python* g; h  K! b4 ?
    def factorial(n):5 d6 Y' f! v6 c  [
        if n == 0:        # 基线条件1 m* f, X6 M; G2 B3 N) N
            return 1- J- i1 O/ A$ o3 s/ |3 {
        else:             # 递归条件
    ( m2 X! o4 K5 h- \* t; I( k        return n * factorial(n-1)
    , j7 d- B' M5 c' _7 E3 E执行过程(以计算 3! 为例):! c! S5 x7 e# f7 }3 V) z" A
    factorial(3)
    ! Y; k* p+ w2 R3 * factorial(2)& Z" [( l' ?6 s; W* }# f
    3 * (2 * factorial(1))
    * k# |# |9 O/ T/ ?7 @& N; _- `3 * (2 * (1 * factorial(0)))
    : t6 @* r( m% e+ ^3 * (2 * (1 * 1)) = 6
    3 ^. c5 D& J; B0 y9 [# m) y; |$ ^0 t3 H# m$ E
    递归思维要点
    2 Q" J$ y9 a9 k/ \1. **信任递归**:假设子问题已经解决,专注当前层逻辑
      \7 U: Y- Z3 P$ m2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " }3 h) a* I2 H- q9 e3 M% t8 E3. **递推过程**:不断向下分解问题(递)1 j! O; s0 r9 r
    4. **回溯过程**:组合子问题结果返回(归)- @$ y+ k9 w, D: }% ]6 b( U
    8 Y. [/ t5 k. q  A% [9 j
    注意事项9 |% m% i6 k; v# `$ T" L
    必须要有终止条件& |# L5 J' s( v. a* f# D: y
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)/ B* N7 ^; p, q+ i+ ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    6 M/ m" C" k3 A, r* [6 ]尾递归优化可以提升效率(但Python不支持)
    7 [8 A: `) P/ H: `. I* c8 p4 g. E
    递归 vs 迭代
    + `( l( s& A, D7 n. Y$ ~7 l) ?1 T|          | 递归                          | 迭代               |
    5 w! p. f5 W  E3 [3 }|----------|-----------------------------|------------------|
    * t+ K: q+ F1 x  K" {' L- ?; O4 G* m| 实现方式    | 函数自调用                        | 循环结构            |  j3 M0 w. o! J$ u( c7 C
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) X, P% n) J0 b" g' Z4 e+ P0 n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    " m7 {# R0 C( W  D& D| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ t  a! E2 w0 D  e! k9 B

    4 F7 X5 N% ^- Q& V1 J% q% Q 经典递归应用场景+ @7 u* d8 m+ u! Q1 h
    1. 文件系统遍历(目录树结构)
    8 G6 W/ L& d0 d0 E* E4 e' ^# V! e2. 快速排序/归并排序算法" x$ y& U  B. g. t9 _
    3. 汉诺塔问题! S3 ~+ H1 T" V0 w& b: [# W4 w+ {
    4. 二叉树遍历(前序/中序/后序)
    0 Z& [6 `" S; f6 S' r  J' I5 O5. 生成所有可能的组合(回溯算法)5 i6 }; c2 h+ ^4 X' Y0 Y8 |; a

    % e7 z4 T! g) ^: ^- I' s试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    半小时前
  • 签到天数: 3112 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    + b$ ?6 q# V% N3 a! \# q, S$ A$ L0 J我推理机的核心算法应该是二叉树遍历的变种。" o$ C9 B& y" T# M5 s) 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:
    ' J: D: x; E& k/ YKey Idea of Recursion
    + {; u( @% y1 n" p: J6 j& i8 Y
    ; r5 i2 U6 s2 K3 L" r9 zA recursive function solves a problem by:4 m  c3 R  a9 P3 a3 W4 E, V# g
    3 [4 @) S/ A1 V1 ]4 Q( O9 Q( \
        Breaking the problem into smaller instances of the same problem.
    1 |6 f# l6 ?6 n: [/ _: d# n) _& `( f' b) \" |
        Solving the smallest instance directly (base case).
    + D& B7 ?/ Z# h$ `0 k7 f# F! O! s' ]- r# F. I
        Combining the results of smaller instances to solve the larger problem.
    3 h, t% I  P9 Z4 q  j. E. q# o7 |" c' y) h) [  w4 E
    Components of a Recursive Function& \; {6 \2 ^* N& q& F$ ?( K
    * [  n7 [! q/ w  X5 \4 p
        Base Case:8 o9 }8 j4 ]# B7 J

    . z  d% A- X+ X) Y5 d        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 q: D/ Q2 U. {+ c' b( q5 |6 X9 B  q
    * P7 f7 h- C6 @) h        It acts as the stopping condition to prevent infinite recursion.
    9 \5 N* U4 g6 [, J; K. v. R/ Z& L0 Q) w: w% F8 Z2 U/ B
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1., C! u, {1 w) X1 z5 Q0 J

    5 l1 f* z; f3 T. t    Recursive Case:
    7 M5 O) m* V1 B
    4 n( L' r# R$ n* v6 }/ X) W        This is where the function calls itself with a smaller or simpler version of the problem.1 n" g) d- x6 U) h" o0 o% e

    9 a5 X4 X$ g. K  f        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; l0 e! D9 Y" t2 ]$ E7 e* F  Z$ ]! t7 [
    Example: Factorial Calculation
    7 M; _5 s. b/ o, ~( \& s8 `: j. E
    $ a! X! O0 B! B  ^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:; y8 L1 N8 U) T. C$ o$ Q4 X

    - ^; I5 y, z3 y6 {4 D+ o: g    Base case: 0! = 1/ j' U! O& |; V

    9 J$ W$ Z, \' {: W    Recursive case: n! = n * (n-1)!: Z5 I3 o3 ^' }0 h5 q3 f' Q7 s

    % {: w9 ~: i) P( fHere’s how it looks in code (Python):1 v6 I6 {4 t3 w$ Q& A0 y7 O
    python
    2 z; @4 ^' T1 r9 W' Y  I1 A+ K5 c5 c; ^5 X2 b3 y

    ' z! Q% `( E8 _: @  Fdef factorial(n):
    0 j% f& H2 G- ]9 m    # Base case
    " H) e2 x. e! e% e0 o" g0 e& A6 j    if n == 0:8 h( B5 V0 a( |* r, {9 J
            return 15 Q" V) F4 j; s+ a* V( l8 g
        # Recursive case
    6 I/ h0 z- P; j1 j& ~4 W4 v# j4 M- c    else:  H4 R9 b" ^. ]/ e1 m
            return n * factorial(n - 1)
    0 h8 z1 R: A9 t7 O7 f4 ?. o; _, L" D% h, H
    # Example usage; c9 S0 c3 n* G: q' p0 ]' o' t  ~
    print(factorial(5))  # Output: 120
    - N4 ~; R* x) V3 [& R3 N7 [0 H8 Q" n- K% S8 P$ V! P% M
    How Recursion Works+ R) t' P  e. V( O) J% x

    5 U; f. h& b5 w) c    The function keeps calling itself with smaller inputs until it reaches the base case.
    8 `7 z9 S2 p! @# T
    $ S9 ]8 j- y7 N  S+ `& J8 E8 ^    Once the base case is reached, the function starts returning values back up the call stack.
    # e9 g, E8 e) K! @8 ]
    " k* h; d6 L2 ~7 n) R9 |6 E1 B3 p+ y    These returned values are combined to produce the final result.2 U$ Y: S5 T9 x5 w3 S

    3 P, E0 @. N5 G+ w+ e* FFor factorial(5):6 X0 O1 @/ p0 K* c9 F: [

    ; e: V6 S% X! ]  t+ |
    ) ^0 u* a) W7 h1 S0 ]factorial(5) = 5 * factorial(4)
    8 z  q  R7 v' u1 kfactorial(4) = 4 * factorial(3)4 o  X$ N/ q1 D; A
    factorial(3) = 3 * factorial(2)
    ( W' W6 t6 S" [. m" dfactorial(2) = 2 * factorial(1)4 R9 j. Y2 {! `. Y. e
    factorial(1) = 1 * factorial(0)
    / o. }: z1 a5 J$ z' ?, G1 O- S' Q8 }factorial(0) = 1  # Base case7 Y! t" I" o: a

    0 _, A# U' o) P1 ^Then, the results are combined:) {1 c; a7 j6 e% }6 [9 e
    4 l- X% B, w) b. Y  M: m
    0 F) P" e. n2 M* J+ l2 W
    factorial(1) = 1 * 1 = 1
    8 t! l  B& t" V# p! a. Dfactorial(2) = 2 * 1 = 20 h1 ]3 A& a1 s0 D2 h6 y
    factorial(3) = 3 * 2 = 6  _) M  {9 j' R: q( k2 s2 Y
    factorial(4) = 4 * 6 = 24
    , g) \7 |; X- Nfactorial(5) = 5 * 24 = 120
    2 K) x: D; X* m. k
    8 K2 m7 }% Q/ T# g) i( vAdvantages of Recursion- G, J& p% [: `- r0 a+ p) Y

    8 b% T6 b2 z8 M0 ]& D* k$ Q6 o+ B    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).- M" d. R. X2 Q. R

    5 q* @8 {" B  E    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 t5 W8 w* S/ {
      G9 R$ k$ \# eDisadvantages of Recursion
    3 W' i+ A# ^  b8 K1 p
    " [6 {- f" {/ \5 h. E' U    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.
    , ~+ R9 R; x' I, k$ u
    ; ?+ o) a% i+ @" B% E4 J* b- p    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# _8 K. A% B- M, ]2 W. E

    0 V! e6 `* C& a% k+ uWhen to Use Recursion
    0 P& e& P+ p6 J7 l" w
    + H: X  V0 O" N/ f    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).4 R  b# s# y% q( G' v

    4 B, I( v; N& _& ~1 d/ O5 O    Problems with a clear base case and recursive case.
    + }* k; c# q) y! @  }2 h; y& L# A9 X
    Example: Fibonacci Sequence
    ( `. ?3 `- t$ n
    1 h0 z: X1 ]6 E/ y, c( \: l# ?3 GThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    " [' `; ]5 b7 J1 w3 ]4 E! p& _+ w  c  }8 r
        Base case: fib(0) = 0, fib(1) = 1
    - D6 ~4 }$ p& d
    ; ]# m2 `/ d" |8 c& J1 b    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . B/ ]4 O. q6 g) B. x2 v; [  q3 t5 C: T8 ?
    python
    8 p$ r! ]' e+ U- D5 {2 t7 r/ J
    0 ^: N! y( ^. Z  O( p3 {5 c, s0 j+ v; E0 Z5 x( ]$ q: o! Y! y
    def fibonacci(n):
    # y1 |( i$ @+ \) ]/ O& N    # Base cases: m+ \+ C& R5 I  ~1 \
        if n == 0:: k# C; F& Y+ W& N+ P* @
            return 0) s% J: v, z% Q9 E! z) d0 M) D! z
        elif n == 1:* }5 r) `0 `+ \* d# S2 Z
            return 10 e. M  f0 i$ a9 G2 t
        # Recursive case$ b/ {4 T2 S- H7 G
        else:
    ; h4 J" T& v, ~9 J) D        return fibonacci(n - 1) + fibonacci(n - 2)
    0 O( q+ x$ Y  ~; c0 |' r
    ( V! N4 E- ~( V- W# Example usage
    1 z$ E& u2 r6 lprint(fibonacci(6))  # Output: 80 w4 ]* m& X# g6 _
    0 ]; F  f8 ^7 H2 U/ ^
    Tail Recursion
    . f0 i& T+ Z) U3 a. e. ^! k- q' E2 A5 N
    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 m4 H( D2 x3 S7 V

    ! q/ V$ z7 a0 O+ p7 FIn 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-8 07:52 , Processed in 0.030810 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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