设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & R# s! x8 Q: f  s0 Q' y8 c8 A& b
    - t# x9 e4 {; v# ~$ G+ R2 c解释的不错
    ; @* `! G% R: d1 M  v) E; B' m& K5 I, m. n2 `0 j% c6 @6 F2 c9 ?! ^4 Y
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    % S6 I3 R& ?& A8 i3 c: k* a2 v  K8 o- a- ~% M
    关键要素
    $ s* d) w7 H" A, _7 O, e: t1. **基线条件(Base Case)**
    * \% [2 Y" J4 p* m) Q   - 递归终止的条件,防止无限循环5 B2 c6 d' W( F
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 s6 Z0 z) `3 Y7 i& ^8 ^6 r

    2 ^) I+ S- j; ?  \! A$ G2. **递归条件(Recursive Case)**" G' U9 I! S% ~. ]- H' c* o
       - 将原问题分解为更小的子问题
      D3 o3 c. Z, p2 `" j0 S: j   - 例如:n! = n × (n-1)!1 X$ u" v8 Q* |* q7 g: X1 Q
    , V5 l, ?5 \. k; J! K0 t) e
    经典示例:计算阶乘
    0 F; z  v  j0 u7 apython
    % c3 B- y+ a! f7 @' ~def factorial(n):& ~0 x2 Z- z2 \+ ]0 d/ i
        if n == 0:        # 基线条件) D* S0 a+ F' @$ R
            return 15 U1 e1 M! u$ O
        else:             # 递归条件
    9 i5 I. D; o/ S* J, G: j7 J/ W/ F        return n * factorial(n-1)
    / J9 Z; ^( C- f8 b) @7 y执行过程(以计算 3! 为例):; w6 D) ]$ x! L6 @( u. v
    factorial(3)/ [( A9 Z5 ~# z8 R4 `- S+ t! Y
    3 * factorial(2)
    / M$ a+ x& p1 V& K' P+ f. ^3 * (2 * factorial(1))/ I: V6 t' V7 U6 e& n
    3 * (2 * (1 * factorial(0)))
    ) f. b9 ?- F+ N, g5 N3 * (2 * (1 * 1)) = 6
    # q, W" C  @% m) z6 v: x3 U1 N* |$ h( M. Z& w) P
    递归思维要点8 D( B+ P9 m8 q1 I, }
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑- J% }4 p! u0 C: E  ~4 O2 Z0 B5 [# _
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)( w% w8 T; D6 R  i% C% }0 {( i/ z6 o
    3. **递推过程**:不断向下分解问题(递)8 x# Z' k: @7 A
    4. **回溯过程**:组合子问题结果返回(归)3 q3 F7 L0 r' D" a. ^

    + x- _$ f0 x9 [# ^" x8 @; H 注意事项$ |) d- A2 j4 ~7 D" O2 k. n( o
    必须要有终止条件
      G( f8 U2 r$ ^! n; u* W" k递归深度过大可能导致栈溢出(Python默认递归深度约1000层)$ J% ~" e# W4 p* ?) M" r( p
    某些问题用递归更直观(如树遍历),但效率可能不如迭代% W* g7 x( E6 s' S
    尾递归优化可以提升效率(但Python不支持)
    + j! h4 S1 K  @1 S7 B+ j8 G2 e0 J- r3 R; S5 k$ O
    递归 vs 迭代7 A4 g: i3 j2 N% Z) A9 f
    |          | 递归                          | 迭代               |
    9 Y* V5 P/ y# u5 K: n$ K- d# p- ~|----------|-----------------------------|------------------|! _: @3 F7 l+ B# A  [# G) U
    | 实现方式    | 函数自调用                        | 循环结构            |2 Z; \' q  x: b% b
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! L7 _# x; ^8 G* R$ ]* v2 X| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- w1 [  v2 k. |
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |9 ]" a: i. q2 H) A5 h
    ' Z* n2 o; D7 H6 i$ G; P1 W6 M
    经典递归应用场景
    # q# w! D( p1 R5 i, [1. 文件系统遍历(目录树结构)
    6 U& ^: y/ j( _, N2. 快速排序/归并排序算法
    # Z4 C* p" T% |& R- f3 M# h% c3. 汉诺塔问题
    1 P4 v% o5 _5 k- @2 I( |" b% G4. 二叉树遍历(前序/中序/后序)
    % T# J& a, O6 _, x$ L5 ?5. 生成所有可能的组合(回溯算法), i/ P( ?/ j* y+ u
    : A& u& V& D8 V, Y  z9 v" z; B
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    6 小时前
  • 签到天数: 3117 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( T) |" u7 ^- k8 {! I6 l8 l! c
    我推理机的核心算法应该是二叉树遍历的变种。2 f# z; v0 c; @9 x4 a
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    9 Q7 r- Q+ L: p6 k3 y: l6 DKey Idea of Recursion
    5 a; v" R& F+ j. b, }; K3 n" e% T
    ) g' L6 h& O+ t: M- aA recursive function solves a problem by:0 c' G7 {5 m/ w% {

    2 x. @2 r3 I  H1 F7 k/ ~/ n    Breaking the problem into smaller instances of the same problem.+ Z: x6 v6 q! [) _) P$ ?4 k
    4 v0 Q7 Q* C( u. s, S/ E! {
        Solving the smallest instance directly (base case).
    9 u& s/ n( U9 ~* Z1 b1 t' V* |+ c# j# a- k# p# z
        Combining the results of smaller instances to solve the larger problem.9 v% t* f+ ?+ P( ?7 h& x+ B

    ( d! ?+ ^8 _/ _Components of a Recursive Function
      g+ _8 H# R5 J: a$ u$ P; t& I% r, I; c8 ]9 M8 V
        Base Case:
    ' H/ g9 q' R. x) C, `( |) j* W- ]" O- A; I8 U
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# S8 V7 W) q* c6 t7 p' O$ ?

    6 ?# N$ r2 h+ B" [) p        It acts as the stopping condition to prevent infinite recursion.; z8 k. v. ^$ D5 J* B. @) w
    6 O  X" G# W& N' R  L2 b% B" Y
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    , H' O7 W3 F* Q
    & |  S( Z6 G, _2 y    Recursive Case:4 v: }! i/ G+ b. j

    * `$ [" s( r- c* A2 D+ ?        This is where the function calls itself with a smaller or simpler version of the problem.
    $ p" {0 `, y9 J1 o, S. j+ R7 P5 B- g! T1 D/ F0 f4 B/ V' n
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 v/ U# S% m6 G8 l  C# e, D1 l
    , p  [* q( n7 J, Z- D. k- sExample: Factorial Calculation! O/ |5 ]/ i! v& |8 j0 ?, ~  o
    % N8 f% \7 }+ }  }- Z. 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:& V3 n' k% y' \4 H% z3 j
    $ j7 O  I- B' q5 f% r" e
        Base case: 0! = 1
    ) I$ \# O4 d- V9 T/ s- o: N* @% A1 e# d" K" Y- w: G$ m
        Recursive case: n! = n * (n-1)!
    5 l5 \" L4 F! a+ ?& l! E+ k; H6 j, W( \4 c) X+ s
    Here’s how it looks in code (Python):5 M1 J+ J- C( s, ?9 O
    python
    ! {0 |( ]+ L& L% k
    & n3 A' l- H+ F7 X$ V
    5 u% ^0 J; b5 {1 p" a+ Zdef factorial(n):/ N% I) P" ^, E  S' E" K
        # Base case
    1 @+ b; x9 j, \' _0 c    if n == 0:
    ( Z0 x! J, _, P        return 1
    + g7 @9 c3 ^, N7 s    # Recursive case, D# V' P- X5 J) T4 A, q, |# O4 I$ N8 ~
        else:5 A9 ]+ I, R- I* P- h5 `4 h5 W
            return n * factorial(n - 1)
    : k8 O4 Z9 d9 W
    - e9 t; X0 E6 @6 b% J7 ~# Example usage
    9 g$ F1 E5 i2 Iprint(factorial(5))  # Output: 1207 t8 ^3 A$ ]  \  a4 K+ M9 ?

    & l6 E( ^4 W; j3 IHow Recursion Works
      P3 A+ [4 f' O8 x
    ' g" @* a* `+ t4 M5 x$ C, U    The function keeps calling itself with smaller inputs until it reaches the base case.
    3 e6 V5 ~& Q! D9 G
    9 ~; F7 b4 v4 R2 u) z# t- R2 f    Once the base case is reached, the function starts returning values back up the call stack.
    4 x. @3 S* S- s0 r+ z$ V1 b
    5 I' c0 i5 C) |7 V( W- h( _" E    These returned values are combined to produce the final result.
    ) m  ~4 O3 |2 l; P3 A+ ]/ ~
    $ [! W: h+ B- d' g5 s# UFor factorial(5):
    & w$ \$ g) I+ g" q! W/ A( b# b4 k" y
    : j3 e" Q/ t. ]. _
    factorial(5) = 5 * factorial(4). [$ W- [4 ]" l  N
    factorial(4) = 4 * factorial(3)
    5 ~& `+ J8 Q) X8 e( p: Afactorial(3) = 3 * factorial(2)( Q6 U: ]8 h7 r# X
    factorial(2) = 2 * factorial(1)( J( _2 \1 x5 L5 n0 d6 N0 f
    factorial(1) = 1 * factorial(0)
    ; P; X' M  U, Rfactorial(0) = 1  # Base case
    ; q9 z3 x( u7 J3 ^, m
    7 Q- y+ @* D! y. _/ g% hThen, the results are combined:
    5 u% L% L: N6 S. p- u- L1 n& e$ L0 O! j( P0 u
    # H' t. x! \  J4 e$ A5 S6 P
    factorial(1) = 1 * 1 = 1
    / D3 J% ~8 V9 m, u! c6 {6 efactorial(2) = 2 * 1 = 2
    9 b* i. i6 h" R2 D0 [2 j6 K9 I5 Sfactorial(3) = 3 * 2 = 67 `' t2 }3 a; Q; r! @1 p5 @
    factorial(4) = 4 * 6 = 24
    * i! i, O  h  x# Q# Hfactorial(5) = 5 * 24 = 120
    ; D, n: @: M- m8 ^7 H3 T' t* o" h
    Advantages of Recursion
    ) ?( D* C4 ?0 |( p, D7 [% C! k1 H. W4 S, N$ m
        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).
    3 s  n) _) N& h4 x9 @1 L5 B* K0 O5 R  k9 f/ B0 z% \9 _0 E$ e/ K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.! K' r: I  m* @
    % S0 Z2 E: h; ~0 I9 C" e
    Disadvantages of Recursion" X3 z/ h' H. i5 f

    " y3 k( `( a- P$ G    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.
    " s- R% d8 F; u; {* ~
    % d! z) G  `6 q" z+ o    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 X( Z  e: Q  Y2 |( Y2 n
    , k3 e  e$ w: ^+ j# G- gWhen to Use Recursion
    9 N8 Y6 a" p; Z
    & @4 R" ^; s$ I9 W    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)." P5 m5 d* a0 d3 g7 u  c2 O; Y

    ' G( u$ g0 A' d7 e9 v  f    Problems with a clear base case and recursive case.
    + ~- t* V5 D4 N: ^  l' N& V3 t& l" b5 \) s3 B
    Example: Fibonacci Sequence
      R0 |0 E" b1 W1 X+ t4 G. n+ J6 j+ A$ n( G
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 F$ J+ g6 T) W0 k/ f# k( T5 e

    ' L% Y! o$ [2 [8 e    Base case: fib(0) = 0, fib(1) = 1
    8 r- C. q! B: L3 O9 n# M. \% ?
    5 k. m. A* e" ~# P0 T  Q3 k- @    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    3 E) T# `/ n$ z9 T  L; r
    + y% K6 B' p7 M, Z9 ~5 J1 D* o8 {python
    , Y7 R# `+ y# ?% x* V3 o
    3 I' X, S4 o- l5 R# @
    5 u+ R7 U' D) c$ a% ^0 R3 `def fibonacci(n):, A5 b8 W% b" G9 k" J. {
        # Base cases! W' X; f* G% v0 f+ \4 M
        if n == 0:. s4 }- z. ^  U9 W* ]: F
            return 0# J4 S4 R1 k. _0 v- L# V: L
        elif n == 1:. w4 A( A$ {& d
            return 1
    1 E7 L( v/ m9 S3 @! b    # Recursive case1 ^! M8 Y! s" a
        else:
    1 h* D+ G7 ^4 m        return fibonacci(n - 1) + fibonacci(n - 2)
    4 v2 y. x0 V8 E  w2 q
    2 i( L% W6 e4 l5 T6 Q# l# Example usage7 d; n: n- x5 k) K3 i9 ~" t
    print(fibonacci(6))  # Output: 8' y0 i- }' Y' M
    4 i% q( H- X7 ?. Q! b1 @
    Tail Recursion6 G: I$ c& G  O% i' ?
    ) V8 s' F+ D- \& }7 R4 M
    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).# h: ~6 w! D8 r; n  r
      {3 a! \5 z3 X, X! c. H6 X/ O
    In 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-14 14:12 , Processed in 0.030465 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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