设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 R+ z  W8 a3 k
    - M+ d/ I2 v$ w9 B1 F* H解释的不错2 Y# |/ N2 U: d+ m4 ]+ F
      _8 |  D) y$ D3 F, A
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    1 o/ e( V) B: R2 L8 [" f' [& ^4 W- f1 N/ Z& {% N5 K2 Z" D% M; @0 P* z3 W
    关键要素
      T$ Y2 c* t0 i! c1 B  [- v  T1. **基线条件(Base Case)**
    : r  i' d/ @/ k- E: O0 L   - 递归终止的条件,防止无限循环7 I" I' Q, f, H, a& O6 u( F
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    1 T4 b6 C7 `! {9 x: n6 K4 }0 q5 R; }- M9 O
    2. **递归条件(Recursive Case)**( Z, e# F6 ~, x  ?- C
       - 将原问题分解为更小的子问题
    ' |7 H5 U3 |2 s9 C& f   - 例如:n! = n × (n-1)!3 m- r! i! K1 D+ B

    6 k0 C. l% e: B0 } 经典示例:计算阶乘& o9 w$ K3 {; z# O8 q2 `! `
    python( B+ S8 t0 r! r7 d# o& s) e8 T5 s
    def factorial(n):
    $ P+ e# Y' ~2 J4 ?! i0 o' a+ f    if n == 0:        # 基线条件
      g+ ^( N6 R9 e        return 1
    $ C! Q8 G8 _' d2 }' d    else:             # 递归条件3 t9 O* E, s2 t5 q; s
            return n * factorial(n-1)
    9 w& ?) J: P% |) L9 o执行过程(以计算 3! 为例):
    * A" |' ?# ?5 e( l2 m4 U% B: Gfactorial(3)
    : Y" y& [" M/ k: ?/ \/ A3 * factorial(2)
    0 O  A6 O# C7 ?/ F( s( d3 * (2 * factorial(1))' Y* T9 e6 x" Q8 M  m) o
    3 * (2 * (1 * factorial(0)))- [$ l1 |" O% n
    3 * (2 * (1 * 1)) = 6
    + \# i" p  I" n; Z
    0 I, p2 d' A0 {3 ~: h 递归思维要点
    2 N6 l$ _0 [) x9 \: x: @8 c1 S/ B1. **信任递归**:假设子问题已经解决,专注当前层逻辑% W9 c* G* d# G2 s# ~
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    / l7 g) T# Q7 R" g+ R# r3. **递推过程**:不断向下分解问题(递)
    & m- i" Z7 ?1 p! [# }4. **回溯过程**:组合子问题结果返回(归)
    * A- k. D/ [8 p" V  F/ J/ G
    ; l' R# }0 K9 }; [0 E% Z" E9 l% ~! t 注意事项% T& J5 F+ K0 J
    必须要有终止条件4 P9 d( I4 c1 M5 L; M% W) g' U
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)' z% R, B. M$ F% ^
    某些问题用递归更直观(如树遍历),但效率可能不如迭代6 z  T  ^" h0 p
    尾递归优化可以提升效率(但Python不支持)
    ! {- P0 R9 ^) }, {" P8 F: a2 S7 N: f3 J8 C
    递归 vs 迭代
    . R* _9 w  J  @|          | 递归                          | 迭代               |- k: _- I; `1 E& O7 f% D
    |----------|-----------------------------|------------------|
    " b% [' X# Q  ^8 {' D5 X+ x0 e: O; G| 实现方式    | 函数自调用                        | 循环结构            |9 i% w' {  P) I9 N0 t+ q: U% Z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |0 R# y' y  C6 Q+ y9 X
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    / z7 w3 l0 h7 a+ H. a| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |& [+ U5 @; ]$ G& z! `! d8 `

    . l* n2 H2 c8 l/ K; A 经典递归应用场景
    : ~: N! y" D: I1. 文件系统遍历(目录树结构)
    + x; S' P* X9 M8 }3 l9 B5 G2. 快速排序/归并排序算法
    ; |' B) B: }) |2 C3. 汉诺塔问题0 ?- j7 f( F. g, B% T# U
    4. 二叉树遍历(前序/中序/后序)8 V- q( h$ I% F. D" q! h
    5. 生成所有可能的组合(回溯算法)3 r1 J" s/ l  M: ^5 s( o
    5 a% _( P1 u' {
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ Q: f, ^2 \8 _, u: e6 g1 ^: O2 w
    我推理机的核心算法应该是二叉树遍历的变种。: J7 {& e: V  N$ `# t# X$ 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:
    4 p" R$ ~( F; w' W) }) `Key Idea of Recursion
    " `$ G9 |4 F( T, W: J+ \- x" L$ n: j" J
    A recursive function solves a problem by:
    " b$ ?3 A, I1 @5 f0 {/ w; v3 Y; W6 Y  y* O) h& u
        Breaking the problem into smaller instances of the same problem.
    4 R" C6 q7 }+ Y- w; R: B2 k
    + C. j8 X+ H9 T" g5 N+ C* I    Solving the smallest instance directly (base case).# @. `4 ^9 E6 o. S
    % a( J+ q/ v/ a  `" k& e
        Combining the results of smaller instances to solve the larger problem.
    4 R, t5 ~% |) {! t; F% b; N8 w& r" X
    Components of a Recursive Function
    * N9 Z8 a7 R; U& M4 t* w9 O3 d; y3 Y6 R
        Base Case:
    9 Y4 H6 P1 K: M* F$ y7 K4 Y/ `7 W: O( d. U
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.8 B6 x' G1 Z0 ~& E
    2 g* p. [9 Q" n( `, S' l: L: }
            It acts as the stopping condition to prevent infinite recursion.& d, Z6 A* i. V, R

    7 [; A- v3 _9 |, I' x6 z+ o# Y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    + N7 W0 z; o" K8 r! |: o' _/ i# b0 m% j6 G. G
        Recursive Case:
    0 `/ W; o! p5 w, P# W( y7 L. T8 |) U' Z. c5 [8 c2 s
            This is where the function calls itself with a smaller or simpler version of the problem.' b( X# B! ~7 i8 n& F- H* T7 y

    , e" U3 W: c* k- K7 s) `( w# ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 z2 @' R' s7 o; W2 i* m0 [& w3 M

    8 M* F- G$ t& F  p7 q! J9 [Example: Factorial Calculation/ _5 b- f' w% m

    3 i9 H# e$ a& v9 U' v5 R& GThe 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:* D! w+ v+ m* x* O5 ?! {6 Z7 O

    * B) `5 i  x$ d6 a, r/ V& Z    Base case: 0! = 1
    - G3 r2 N. a# N& Z  U* v( n
    - D9 D" Y* u: U. e8 E    Recursive case: n! = n * (n-1)!5 C( f3 e3 T$ V/ r" H+ k
    # L" K2 ^( k" O- r8 {3 j# Z2 {0 \( \/ |
    Here’s how it looks in code (Python):6 C; v. k6 L2 Y
    python. e$ r5 J! j; D7 n

    . p! u; j8 o* g$ `5 T
    3 G/ N( G/ U' W- A& Mdef factorial(n):$ t2 v" O5 w5 M0 C8 Z5 c  \
        # Base case
    / }* f) F& x3 t. P+ g    if n == 0:  @# D1 v, o+ g
            return 10 b3 k& J8 u9 L4 J
        # Recursive case
    $ _7 i1 I5 M, A" Z    else:2 P) z. K# g7 X+ \. D2 d
            return n * factorial(n - 1)# B" }" h% }2 J& ~! I7 d# E

    1 h) l  r& T7 O" X. d" m! Z: e# Example usage
    % R% \! ~. G0 Q# vprint(factorial(5))  # Output: 120
    + L, y  B" f6 v- o; J/ s
    " ~+ t% M) v6 q8 W4 ]% X( FHow Recursion Works+ h4 f! h' L8 r5 y/ B5 C- Y! l, P; e
    . _% n$ [% X& i1 h
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ' t( e1 E7 r2 f5 M1 j& T, [
    3 z& Z+ F/ K1 y) R2 w# K    Once the base case is reached, the function starts returning values back up the call stack.
    / r7 ?* C7 S1 @6 a% c3 {# p2 W) N8 E
    ! C4 M9 U5 s$ X1 O3 Q. w    These returned values are combined to produce the final result.
    ) p; x9 r& i& l" M4 `
    ! |" K+ I5 Z1 p' ?8 [For factorial(5):
    ; r- X6 r3 x+ j( C  f+ o* g8 Q
    + r! W9 U6 \: q' P5 a8 k- e! x' j9 V0 Z2 f2 I( f! y, C
    factorial(5) = 5 * factorial(4). O6 U! A/ K+ `! L8 c' X* Z) f! i
    factorial(4) = 4 * factorial(3)  v5 ^5 d. J# }. s
    factorial(3) = 3 * factorial(2)
    5 M9 E) K$ U+ r3 K* o# x7 |factorial(2) = 2 * factorial(1). R$ c4 u' K5 ~. }
    factorial(1) = 1 * factorial(0)  C3 N# u+ C( N* L$ h7 }' y- k
    factorial(0) = 1  # Base case
    0 h/ D9 x( R* M7 v: i. p' o7 q
    0 |$ ^* z& H  EThen, the results are combined:
    ( _* z5 w; ^2 i* N- o& F. a/ T; V
    7 W3 Z; i7 J% h! ^* |5 X; U: S& _
    factorial(1) = 1 * 1 = 1
    ) k; P8 S2 I9 X  W: J: C, Dfactorial(2) = 2 * 1 = 2
    ' C3 q1 M8 f1 E" I5 Z9 Nfactorial(3) = 3 * 2 = 6
    + Q$ I6 I" y4 c# C% l4 C+ V0 rfactorial(4) = 4 * 6 = 247 k8 e+ p) n( F  c9 Q
    factorial(5) = 5 * 24 = 120
    # @5 b. o" q: A6 J
    6 s) a6 _; |! l, g6 n2 gAdvantages of Recursion
    0 ~9 a# X; X% r5 W# v0 `/ c. F, s" Z1 Y$ ~6 z' S% ]
        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).
    " |' a3 p  B" @. T5 @0 v0 d& ^/ j2 i2 D; n" z6 {
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " Z* I7 }. _+ T# y
    1 U# }) b' w6 M" VDisadvantages of Recursion
    0 h+ C+ H* v1 }8 z' m; v, v7 k( q) s
        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.
    8 Q4 ?: w! j7 A2 ?) X  y& Y
    , x3 [8 b2 x& t# x/ I    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    6 W; t# t5 ?; R( i7 O/ J3 |1 `( O  U0 F& S
    # E- O4 [9 F  r4 T! B9 E$ r  iWhen to Use Recursion
    $ |2 R# h$ _' c) J: j
    / y/ t9 H  ?2 M8 R, x2 G: ?: z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    1 T6 b3 c9 |, h7 ?; V
    % t  T. ]; h6 ]% S% |    Problems with a clear base case and recursive case.
    $ {' e9 d8 f6 m" e1 h: n% [$ g, l, I" h
    Example: Fibonacci Sequence& R" U* U' z; Q& I8 n
    8 [- n2 x% V' o; T5 Y
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ! C' A; `. R0 {$ Y( R1 t4 q, ?! @' D8 R+ Y4 h0 J, p' `
        Base case: fib(0) = 0, fib(1) = 1
    4 ?: j" x& T% f: l  _, O0 u1 @
    0 t0 Q0 J( G  M, I# l    Recursive case: fib(n) = fib(n-1) + fib(n-2)4 Y7 K2 f; G/ e, M9 {; S' r
    - J4 ~; d  I* ^: @
    python1 M9 @; d/ d' f. X$ f( ^

    & z6 {* I9 c# I2 ~: R. g  ?" Z3 h& r# w" z# `
    def fibonacci(n):
    7 B  \2 C" C6 l2 I    # Base cases$ N& F2 q# Y- v1 Y, n+ c" z
        if n == 0:
    " O+ g% L+ e" z  I        return 0
    " m9 V$ a: t7 R3 L    elif n == 1:
    0 G/ J8 {; F5 G        return 1" w; p( R8 d$ Y+ O
        # Recursive case
    . ]' n. ^5 }5 b    else:
      s* Z7 N% o/ B4 b: P        return fibonacci(n - 1) + fibonacci(n - 2)- @) f( g6 K* i: {8 J, L
    3 B! Z: p& d: k+ a* W' N1 A
    # Example usage9 @0 T0 ?) {  S. e6 J. |! S
    print(fibonacci(6))  # Output: 8
    ! u" n) q- E& I" l2 R+ [: h& D1 `
    Tail Recursion: g+ `3 M8 T# Z' s# K
    $ c5 U1 N1 A. R$ \
    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).
    2 ~+ v& e! N1 o& h1 J
    4 [: y  R. P, a0 ~2 ~  e: W4 tIn 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-11-28 23:03 , Processed in 0.033262 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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