设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 ~3 x! D4 v# y+ [4 U
    , u4 O: a! C" a) z" \
    解释的不错
    / h5 U! S5 ], t8 O
    , b5 u9 p1 R" B" F. o" k) ], {递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 L0 d: i/ {, K7 u/ ?8 x
    ) A/ g, T. ?8 q- L2 t. a( E* g 关键要素
    * M; e9 G( h; m+ D- G) G3 O) n1. **基线条件(Base Case)**6 C; q: v( c9 ^; D2 H3 T
       - 递归终止的条件,防止无限循环- r. g5 z1 g  `' l  T9 _
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1" u% z7 Y+ f% W( r2 D

    1 ^& G/ L& b: ]; U) d, ]0 j) ]2. **递归条件(Recursive Case)**
    3 g. p( m! P. E0 f8 J   - 将原问题分解为更小的子问题  y' I' ?! W3 y: o7 f- U
       - 例如:n! = n × (n-1)!
    , ]9 g* v, t% T. @: X4 Q. a3 q$ P# u! q$ a* s/ `
    经典示例:计算阶乘4 @& l# z! `" P
    python
    8 P: B( X- a. o4 k6 ydef factorial(n):+ m  _- l/ J; v$ l% o% @/ b% |4 T# ^
        if n == 0:        # 基线条件3 w7 e& x1 v) R6 y9 o; S
            return 14 _" s! \+ R- L$ W, c6 A
        else:             # 递归条件4 y+ E! O4 _% K
            return n * factorial(n-1)) K/ C( V5 h2 Z7 f; O) {1 A' O- a
    执行过程(以计算 3! 为例):# n* j" Z6 }$ z3 I4 N; F
    factorial(3)
    ' l, @6 ^: J! o$ j7 u$ F" `3 * factorial(2)$ `& o( s' t' [" P
    3 * (2 * factorial(1))* \) Y0 D- [% J$ K: x# o5 J+ }1 ~
    3 * (2 * (1 * factorial(0)))
    0 i' ~( J* H9 f7 J3 p- A# I: R+ Y3 * (2 * (1 * 1)) = 6
    6 V4 ]' {3 G/ [4 N* w) t- r9 B$ `: n) @: I0 @9 O! f$ ^
    递归思维要点$ N3 e# s& h* {( `; n
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑) H5 I% l# w% h- j4 Z8 ^/ a
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    / ]  p0 b  q! ^4 Y3. **递推过程**:不断向下分解问题(递)) A3 q7 M! G' ]3 V' n) q
    4. **回溯过程**:组合子问题结果返回(归): s  J% K* w) @9 Y/ t% b
    * j5 R' u4 f  V# P: m5 r7 g
    注意事项! |0 F' ?# `; a. S! }6 y
    必须要有终止条件' {& \( h( l: H
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    9 J4 E* `/ G/ o6 J; Z2 W某些问题用递归更直观(如树遍历),但效率可能不如迭代5 N; R& a  R, l+ _! e1 Q' u
    尾递归优化可以提升效率(但Python不支持)
    5 Z( ]1 E# \; Z: x4 z  l7 L* p/ E
    % \3 y1 d" y5 k$ _! a0 _0 E 递归 vs 迭代
    5 [# `, v+ l: l- t) Y|          | 递归                          | 迭代               |) Y/ E9 M. D* r5 b- B
    |----------|-----------------------------|------------------|
    ; f7 r4 f& s* g| 实现方式    | 函数自调用                        | 循环结构            |! c2 K' g! Q% A/ E  s$ y: E& S
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ I( K- M3 d6 |6 Y- n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |" ~% n* M3 f( F
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    + h7 z+ T. K( J$ ]9 m1 u7 s5 C9 Q, w7 f" Y/ `8 m0 d. |' [
    经典递归应用场景& |3 L! t% o, z6 E* j* g! w
    1. 文件系统遍历(目录树结构)+ R. h& s, _% T  p5 H
    2. 快速排序/归并排序算法
    , L6 ]  l' X- i( O5 C' o8 Z! F3. 汉诺塔问题
    4 j! ?; U% c; H$ |% U) r" n4. 二叉树遍历(前序/中序/后序)' ?/ h6 X$ j. i3 \* Z) A
    5. 生成所有可能的组合(回溯算法)" I. _4 z) ]+ a$ K
    ! I- n4 \5 d0 s/ O9 Q# I
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 @  T( _3 u' c  x0 `4 E8 O9 N# S
    我推理机的核心算法应该是二叉树遍历的变种。6 v8 W! W7 f. R: P2 G: R3 {# |! V
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ' z% l3 B; s; s  B& RKey Idea of Recursion
    : i# u; A' k+ q2 _; k. M6 b
    " k/ o1 O  A/ c8 `  H* \+ dA recursive function solves a problem by:% Y/ ~% U2 k  j- Y

    5 z* C0 j) \' ]% K7 R+ c    Breaking the problem into smaller instances of the same problem.( i; W6 M$ b: k7 G. L- o3 g
    " V  z9 j/ A/ \* t/ z5 V; _9 _
        Solving the smallest instance directly (base case).: s3 i4 i7 t+ H3 s" W# j
    $ H9 \) J" D2 ?# H% ~! f
        Combining the results of smaller instances to solve the larger problem.
    + f# _& ^& }* n4 U4 ~! d. H; h# k: z& N/ [7 Q& R; z% Y2 `3 N4 _7 R
    Components of a Recursive Function
    - B2 V- C+ K# [& ~/ `, a1 H3 N- d) a+ c* Y* l
        Base Case:
    ; n' D; [) z4 y9 m* |3 h  D. d8 I2 U/ |: }1 g
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.% u' W& S' s5 B, |2 u6 v
    & [: r, m4 _  f
            It acts as the stopping condition to prevent infinite recursion.
    & B* w( F1 ?3 y6 p2 K4 q6 ^6 E+ s1 J7 U6 }
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ n  ~* [  E# K0 b% p" K" ?! {5 O0 {% |  S

    9 D3 @- z$ b, x; W" y, ^* G    Recursive Case:
    8 ]! e) v& E& ?: o& [9 A% r
    - S# s; F. P  E, }6 w' S$ k9 K* t        This is where the function calls itself with a smaller or simpler version of the problem.
      ]0 f+ Y$ g9 g# q/ k: w4 C2 _$ Z5 f
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).2 ?/ ~, L: b$ g, {

    6 L( s- ^$ l7 ^! D8 `9 Z0 F* f' E& }Example: Factorial Calculation
    , K0 _0 j% q, w+ {, c  H1 ^4 }1 y6 F5 [& H9 R  R  I
    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 g7 @+ D) R2 C) o, ~0 _; E* N
    ) S3 b2 Z3 }, D$ h' q/ v    Base case: 0! = 1$ h: E: A% v+ a+ u( w+ k( I

    ! }& g( [8 }0 U2 D+ h    Recursive case: n! = n * (n-1)!
    ' p- Z& \3 a' [5 t* ]4 o+ r, `. B/ ?$ [2 r  |9 ?
    Here’s how it looks in code (Python):
    # }( Z- p) k3 o9 V. ^  Tpython; ]2 l% w8 P' N# ~; [
    5 i4 l/ D2 [  w: _: V( M# v4 {  F
    & U/ w+ z( d7 Y# l6 J$ y
    def factorial(n):
    0 |: P0 g2 n, m    # Base case7 c3 _# @. Q" P
        if n == 0:
    # k3 z# y. `7 z! A/ b0 i0 k        return 1, e6 v0 s; A" q- {- w2 c
        # Recursive case. {" z5 U3 `3 F/ `5 N% v
        else:& E* L. L6 V. _$ N- a7 _& \7 ]: o! `
            return n * factorial(n - 1)) ]% ^' O; F) w. S6 i

    ! \7 S9 ~0 o+ C# Example usage
    8 f/ r- v! z  Jprint(factorial(5))  # Output: 1203 \& X! S& [& O! {$ W5 c- {
    ! P- W" S+ N9 U) o
    How Recursion Works: N/ P2 f1 y, S3 H$ h, v

    6 a& k0 ~# ^" x# W/ d: g    The function keeps calling itself with smaller inputs until it reaches the base case.; l8 y. {* z* }0 B7 F" o( D

    8 L) S" E3 J& C6 i, A' N  w$ x    Once the base case is reached, the function starts returning values back up the call stack.3 `; t; I, t- Z2 w& n
    # T$ g2 @: \* S. ?3 I* F
        These returned values are combined to produce the final result.
    $ N+ f+ w/ k$ W  e1 Z- w) [' K, ~* V# L& f6 {- f0 G- {
    For factorial(5):
    8 K3 Q) p7 k- O2 U0 E* r
    ; C( e6 W0 C0 \* M
    / }4 O- h+ i0 |4 i# |6 D- pfactorial(5) = 5 * factorial(4)4 K, D; j% s* u' H2 p
    factorial(4) = 4 * factorial(3)( @9 P7 y, ^! ?* h6 L3 W
    factorial(3) = 3 * factorial(2)
    ! U; I$ w, k" q' Ifactorial(2) = 2 * factorial(1)
    . R3 A; M3 E  v- p3 C" Ifactorial(1) = 1 * factorial(0)$ ~3 d- ?: Q2 M6 k# B% \  E0 e, O
    factorial(0) = 1  # Base case" y4 ^% |7 [7 p* W2 w8 W. T( q0 c, w
    $ T4 S* b3 i+ `- L/ a* q6 I0 n4 G0 s
    Then, the results are combined:
    6 M* Y! J. I0 S
    2 a& I& t& [1 x3 E
    ; X# y/ ]( d. T4 Ffactorial(1) = 1 * 1 = 1( y6 F; e( M$ [4 q0 J
    factorial(2) = 2 * 1 = 2, r) P- y% J2 S- Y
    factorial(3) = 3 * 2 = 6. ~' p  z4 j; ^# P& R. U
    factorial(4) = 4 * 6 = 24
    & G9 y: l" ?' j& m5 z  Bfactorial(5) = 5 * 24 = 120
    ) f0 B) L$ p4 l; `1 h( M; D5 I
    # D0 U$ i. ]* V! N- Y: ~Advantages of Recursion
    5 \# v$ l3 |, `3 ?/ U. |4 e# i! l3 y* d; E. K' 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).' c& G0 N) g- D6 C( R- P0 R; |8 T

    * ~& W/ J' V  c4 v: T  w/ _# g    Readability: Recursive code can be more readable and concise compared to iterative solutions.  S1 u, y+ [0 V( ]7 C) J5 i/ J
      |' x) z+ F" L) ^* |: I: p
    Disadvantages of Recursion/ R0 p- x7 V* @* O0 f8 X

    " a7 x' w" f$ n    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.
    . ], F; t) J# `) A9 K9 f" M. `6 H1 l/ z9 j, q% c9 B9 r
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    $ ~4 Y/ S: Z! _! @0 G  R  V1 t7 ^" U# ?8 w! \& S. V" z  k
    When to Use Recursion
    . v8 |  Y. _% a6 q6 |; G, s8 n
    " w) G4 W0 C$ K- v    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)./ `$ i6 F9 ~6 t! o( V% K* n# V: x
    1 d3 P9 q5 e6 D$ K. l
        Problems with a clear base case and recursive case." M1 e; j0 ?4 p% ^

    6 w5 b$ g- B* z* b, X9 s. WExample: Fibonacci Sequence
    0 E: i6 L3 h6 b2 j9 N% x" N+ D' C$ L5 V9 ^2 Z
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:! u5 e2 o: Z; n

    1 o; Y; k  M; U$ q, v6 S6 R. m- `    Base case: fib(0) = 0, fib(1) = 1
    " }  G2 @" B0 N0 C# |# d; t
    " T1 q3 l6 \# J: M# ?3 ]7 E# B    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . n, e* D# E  E) d$ ~! h& p
    , o! j8 {$ k# D" e1 p" ^python( R9 _4 g% G3 v7 g: ?
    # Y# x) P8 u2 h% D. L; G* `
    7 ^; Q1 y: d8 ^/ T! m( x" G- ?6 w
    def fibonacci(n):
    # p  I# a5 i, Y    # Base cases2 n' v- F' q: A  a. R4 ?4 T! A* L
        if n == 0:* K7 i& a2 s, p, e( `% z" r& t
            return 0
    ( k1 _- |+ x( G    elif n == 1:5 B" @/ V9 y7 d! z
            return 1
    2 j* T, n- v4 d9 N9 ^    # Recursive case
    4 Y+ t) ^. X' K# L9 d    else:
    # `1 |- i# x  \% {5 H8 W4 W        return fibonacci(n - 1) + fibonacci(n - 2)7 ~. o4 {6 R" [4 _5 K- E

    9 C0 I9 b6 h) |/ J8 s( [) K7 @1 ^# Example usage
    7 j) z9 m# c6 Y' Y  aprint(fibonacci(6))  # Output: 89 f; n4 Y0 x2 W1 l! a$ ]8 v
    , u4 W6 V  ]3 C) T: s8 F
    Tail Recursion: U; ~5 R0 h. Z4 i+ ^4 _
    & T4 n8 g0 |* u( Z
    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).7 h$ P+ R6 o7 P; m8 E
    ( @3 a% o2 h; `6 D
    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-11-24 14:20 , Processed in 0.034778 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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