设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    * R$ ~" A; ]' H
    ( b& n/ T2 o) S# |! n3 ~解释的不错, ?4 c9 ^8 n0 l( n/ k
      N) G2 m- Z4 e" t
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    / [6 A6 ?" T, f% D/ s; d  R% _$ w
    * L  Z7 x, V8 g8 B0 X/ E 关键要素
    & s! N1 b! l. \1 f( o$ o1. **基线条件(Base Case)**' j. y  e- D' B4 i0 C  V
       - 递归终止的条件,防止无限循环- h6 c: e" `* v2 E# k
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
      ], Z& Z3 r! N9 _- t! J: T: R
    ; T) t% C- n$ A; C/ v0 X2. **递归条件(Recursive Case)**- c; b5 _* p- U1 P
       - 将原问题分解为更小的子问题
    $ G# d# D, u( D/ K) B" Y  h- c   - 例如:n! = n × (n-1)!3 ?/ a2 x# S$ L, j
    & o% i, y: E# m) h8 _" i! v
    经典示例:计算阶乘
    4 [! g! L4 R0 T9 m2 Tpython3 S% h% q" t; c) ]( s: D
    def factorial(n):6 ?) z5 s0 o$ p9 C- E
        if n == 0:        # 基线条件& g4 a- J# d9 K; A/ y2 w: u- g
            return 1
    , X) t3 n6 c) x! d6 S    else:             # 递归条件# ?9 y4 h% r. [
            return n * factorial(n-1)) K5 g) G' C) G0 M
    执行过程(以计算 3! 为例):; e% d4 i% A9 r- i# i8 w1 ~
    factorial(3)/ W3 V+ k6 n; ?7 [
    3 * factorial(2)
    6 H$ j: ]/ @! _) @4 T9 n3 * (2 * factorial(1))0 G9 g) r. A% B8 v6 c% @
    3 * (2 * (1 * factorial(0)))
    ( \* w! j" v3 p& k" Z' v3 * (2 * (1 * 1)) = 6, Z# t& D, v  {* `* ~6 s% Q" n
    , A7 Y" `& D7 d
    递归思维要点& L" F7 \0 {* H4 c$ g! {
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    " T* a# P5 J& e$ W! G7 t2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 c7 Y$ }* X* R' R; J: Q7 D, X3. **递推过程**:不断向下分解问题(递)# a: U$ G: u. {4 a. q  s7 k, r
    4. **回溯过程**:组合子问题结果返回(归)
    1 q. ~) M4 M# Y& v$ l* x: \. ?! `2 t4 T% F, h! r8 ^
    注意事项
      u& e$ ]' Q! O5 q- m& u必须要有终止条件
    # J: S! G8 i6 V2 H递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & g  K; m0 i+ y某些问题用递归更直观(如树遍历),但效率可能不如迭代8 U. h+ T4 M' m
    尾递归优化可以提升效率(但Python不支持)/ [& l: E6 d/ I
    8 g  d: w% F, k4 d0 U7 h" l; K
    递归 vs 迭代' d$ b2 y" V, R5 k* W
    |          | 递归                          | 迭代               |
    + g% e" }- F. `8 v|----------|-----------------------------|------------------|# A9 Y0 n$ v( X5 m( q$ i1 e
    | 实现方式    | 函数自调用                        | 循环结构            |
    ; ]. Y# _5 q# {7 C- e| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |* m1 G2 ]2 I  \8 B- r9 E" ^& B
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    7 ^: N6 `4 f: P| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 M  k1 [" ~: Q4 s4 t1 s
    % v/ J" u7 a) ]( ^0 w 经典递归应用场景2 h( {# [+ @# g" \7 `
    1. 文件系统遍历(目录树结构)
    + @8 c2 |3 M  ]1 }' k2. 快速排序/归并排序算法
    5 G" N: ~; ^0 x3. 汉诺塔问题8 i1 B8 a4 `, H' w2 {
    4. 二叉树遍历(前序/中序/后序)0 f% P4 o" J5 z/ C) P: g
    5. 生成所有可能的组合(回溯算法)
    / g! u  ~2 q* @* i) W4 r1 e. _0 R1 c/ U
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    3 天前
  • 签到天数: 3219 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# f; K" Z' M, X% h; e9 q
    我推理机的核心算法应该是二叉树遍历的变种。
    & }( F. P' `# y8 |另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    2 G- `' ^! S) h% r5 t% W. T+ @" gKey Idea of Recursion3 @( v/ @. f! }7 u6 o
    2 |$ ~5 Y  r* {( F. E
    A recursive function solves a problem by:  c* z" A& D2 O
    8 K* {* b; J! Y# M
        Breaking the problem into smaller instances of the same problem.
    7 z8 o( P0 s* R1 }, ^4 g+ q, x7 w6 E+ C3 b
        Solving the smallest instance directly (base case).' Q! }/ O2 _) I6 K! ]- c' I" h
    . F3 y2 `# p( w/ n0 A
        Combining the results of smaller instances to solve the larger problem.3 T9 t  _% }' d* x+ e. B0 u5 @
    4 k' V& V6 u0 f1 r3 R% P% P# U+ l
    Components of a Recursive Function
    ( O6 b0 P; Q; o# @
    ! B5 b% A9 q) s    Base Case:; [/ v+ b  J7 s- U9 e: A

    8 T1 H) s1 E4 `' P+ @) O, M  Z  e        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    * u# D) l& ~8 Z# m; B! M
    ) Q: y" F2 h2 c        It acts as the stopping condition to prevent infinite recursion.+ T; b4 o& b, I4 Y9 w/ p" D
      |+ X0 k! O) @- J- C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.8 D+ @7 C* r, R5 P6 r# [( z/ N1 Q

    # e% j7 T3 H( k+ v' v. w' I    Recursive Case:  z; y. h" c9 l2 t
    + L! x- M4 `4 s3 u. d
            This is where the function calls itself with a smaller or simpler version of the problem.# B5 j8 x5 ?+ |0 t) _

    , q. U( U1 x9 L) Z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." X' `7 u; i" p/ D8 @
    - b: i3 R7 d; w+ y) |
    Example: Factorial Calculation, I- _) v9 j1 i
    % D  \5 Z; q/ O" k0 g
    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:
    + V  [6 w* D3 L$ g5 H2 s0 i5 r0 X0 o7 M: f1 b. \
        Base case: 0! = 1
    / x+ b: a) C2 L; r3 R
    3 a8 W9 T; W/ O8 I3 e# r! {    Recursive case: n! = n * (n-1)!/ Z  _1 P' J  P, p

    * `- J, ]& C/ E/ C1 y* a, jHere’s how it looks in code (Python):
    : n! |0 i- y' o, h: Gpython
    7 ]: G6 J! B1 l" `. L4 F8 R
      q* T1 T: a5 i4 l7 G0 z6 S" M$ ^$ n% V( u: m
    def factorial(n):3 G/ i1 w8 }3 a5 }8 f7 Y
        # Base case
    7 T9 s3 F+ K1 W6 U- L- T  s- Y. z0 b8 L    if n == 0:3 Z8 j9 g' ?  V: d1 B7 R5 u% D% }
            return 1
    % f, L$ V- ?2 E7 b! _1 j  b    # Recursive case
    & t8 q2 \5 g' I  _# i4 f    else:
    4 l3 d- i8 t/ F# K, ?. D+ D        return n * factorial(n - 1)
    , d2 O) O/ L, j0 p+ Y1 }0 P0 n
    1 x, A0 G: K5 l# Example usage4 i; q% G, Z9 F6 L& o
    print(factorial(5))  # Output: 1209 x: O: f' K# |! W3 P0 l
      D" K& ?& I; C! x
    How Recursion Works
    & t0 ]( p  B' t' @2 r2 A9 @
    0 d- z6 e! a6 r) L7 Z; G    The function keeps calling itself with smaller inputs until it reaches the base case.: U8 Z& F  W" F5 |% z4 A
    , T; I& v  r5 D8 Z: L- N
        Once the base case is reached, the function starts returning values back up the call stack.% _0 H; \6 ~+ N' K
    9 f6 ?# ?7 s) R: ^# n: N
        These returned values are combined to produce the final result.
      `6 g' J1 K2 }; Y4 Z8 e3 W1 r
    # {6 Z( {; y4 O* }7 y/ eFor factorial(5):4 H3 M, _& @5 U$ V

      g& k8 g  \% {# }. V  Y( c
    6 j2 N# k( Z; [0 Yfactorial(5) = 5 * factorial(4); r' U" G) J( J: N0 R0 Z
    factorial(4) = 4 * factorial(3)  R# F9 m- _2 m7 s2 p8 J
    factorial(3) = 3 * factorial(2)% x) Y3 C; G+ ?7 T
    factorial(2) = 2 * factorial(1)2 }  ^- i: V3 U" s
    factorial(1) = 1 * factorial(0)# ]5 q! c! Y0 M/ t4 b9 z
    factorial(0) = 1  # Base case9 d% E, [- c% ]; {3 ]

    % B# {* S6 u2 @" tThen, the results are combined:
    7 y: r- V9 b* W: y2 G; n  _1 U8 X: }3 f5 l

    6 j8 f) k, W3 m1 `factorial(1) = 1 * 1 = 1
    * g2 u& t% ?4 R" z- y' xfactorial(2) = 2 * 1 = 2" O: k* K! A* u  [  v, b$ ]  y2 ?
    factorial(3) = 3 * 2 = 6
    ! i* z) {, g0 z9 a3 B# ifactorial(4) = 4 * 6 = 24
    9 Z5 O9 ?* J" R. v  N* wfactorial(5) = 5 * 24 = 120
    6 K4 [8 q6 v/ j. r" x3 ~2 ^7 Q1 Y( ]
    Advantages of Recursion
    3 C% u1 P& l" M! |0 G1 ~1 G: B
    & N0 i7 i6 z' X% a/ u: U    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).
    9 b3 I# {+ L8 P# @. \" m: K( t. G5 T2 P- O8 P5 T" O
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    3 K$ r, ^9 l3 e+ ^# B
    / ]* W- b1 ^. fDisadvantages of Recursion
    7 Q( s* f; S( L2 ]8 J) l  |5 c, I. H! A" Y
        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.
    2 Z% I" X( l+ J7 B3 t' a
    5 M" N2 G2 R4 H( n, T" C( e( Y8 j4 X    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ O+ j! k; M: d8 i: ]& B) }% F
    3 C" L5 e0 }: u  k. k( ]8 U2 z! A: v
    When to Use Recursion
    ! F. f$ V& S: K: Q$ ?  L
    + y# F- D! z% h; J    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    7 Y5 d0 d( x5 k2 R; Y4 S3 G
    & I) l6 t! ^& K4 m  e- w! C& g4 h    Problems with a clear base case and recursive case.; ]* f! v9 [7 U8 I0 `1 E( o) S
    4 E3 @. M5 Q+ F2 R, E
    Example: Fibonacci Sequence3 t8 @( Y% z0 J8 G/ M6 x
    ) b2 \3 G- c7 n. R# ^0 F
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    " K1 H  i* d+ Q, H  F$ Q1 N3 d! T3 u4 a" r4 z3 x6 w+ f
        Base case: fib(0) = 0, fib(1) = 1
    2 J6 k7 O! H4 q9 E2 m1 k+ Y" M
    ) u+ z, \* d/ ]# A! T) [    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    & `/ J; x+ Y. N& @, x* j( Y: o. y& ]1 [" z: e9 k% |$ T
    python/ a$ o0 D$ _$ x- s3 U

      s3 V) ^* V  _: `3 w9 F4 e: ~( c: j, N/ I; m( J* \) ?
    def fibonacci(n):
    2 |1 k0 Z1 \8 Q+ ^4 K    # Base cases
    & O7 a) ?2 Q, U7 \    if n == 0:
    ) h. x% Q0 C2 w9 x+ W2 t% q. c2 j        return 0
    - X. Z2 D  v9 u: Q9 W5 O  }8 B    elif n == 1:9 ?/ @4 N! x) g7 M( X- ]5 H
            return 12 }# k. \0 @( H, R
        # Recursive case8 r4 M, z4 w  }7 U
        else:1 v/ U% B, G" r4 d3 m% O
            return fibonacci(n - 1) + fibonacci(n - 2)9 d: B1 E; r6 b1 T5 c) P0 f
    ) [- X% }5 m+ |: r
    # Example usage: [8 {: A: v+ l2 i/ G& [
    print(fibonacci(6))  # Output: 8! b9 T& Z( ]" W, r  F/ u% H% x' c
    , z! j) R4 B* w$ Q
    Tail Recursion/ c$ L6 g  R8 b8 y" ^7 \
    ) a& C1 d9 t" W! C9 @7 L; O
    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).4 @* x* o# b. {8 I5 Z
    4 `8 ^# @: R  o. i
    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, 2026-4-21 15:50 , Processed in 0.064444 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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