设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / W1 C) Q5 d4 o1 ]0 U& G# l, \& z+ h. V2 `4 d3 b3 j
    解释的不错; t. q2 S; U7 W* r: H; {

    0 K; O/ M( F7 b递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。; w) {/ j, P6 q  b  L

    ; _0 n" q7 D4 A) ]* W) W, @ 关键要素1 q  \7 N, [5 I9 [  \& E
    1. **基线条件(Base Case)**! Q5 N. V6 z" e% r0 j
       - 递归终止的条件,防止无限循环4 W9 c6 P. X( k
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# z9 A; y( |4 Q$ f) ?
    1 ], W( [/ b6 X+ @6 x6 r
    2. **递归条件(Recursive Case)**
    ' t; }: A' }4 e0 j/ x: r   - 将原问题分解为更小的子问题
    ( W3 @3 V: @- J3 u$ }) I, g   - 例如:n! = n × (n-1)!
    * Y+ g  ~, k6 j* a8 ]1 |5 @
    ; Y$ b5 b2 u+ T* B$ N4 J+ @ 经典示例:计算阶乘/ Y/ }2 A6 K8 \$ F4 e$ m2 N! d+ H
    python
    . ~" q4 B. y+ M/ ydef factorial(n):* X) L  _; g0 f- B3 m
        if n == 0:        # 基线条件% n, F+ d' l4 L/ G  o& x1 `0 s
            return 1/ {  d. d. E3 o3 A3 Q
        else:             # 递归条件
    ' r; }. d2 _6 a* f        return n * factorial(n-1)! C- Q# p% p* }8 N5 B
    执行过程(以计算 3! 为例):4 M4 \- @! Q" f+ J9 _6 I- i6 R
    factorial(3)
    5 t/ `0 K; G! R% g. D3 * factorial(2)9 V  |, W8 X; v
    3 * (2 * factorial(1))
    0 K3 \; ?- `' W$ g' N( ]3 * (2 * (1 * factorial(0))). n+ T8 e+ y$ t6 @1 v
    3 * (2 * (1 * 1)) = 6
    , h7 H# q. J5 h- [4 {, o; o6 n# t- a, E/ |
    递归思维要点0 H. _6 K, ?5 d
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑& F9 V) `8 D1 a7 q$ f
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)2 Q- G' K, J7 l9 ]0 h+ d
    3. **递推过程**:不断向下分解问题(递)1 ?* v9 X, z/ X
    4. **回溯过程**:组合子问题结果返回(归)
    2 c; Y# _* j& @0 G! P
    + s3 B/ d) z9 h7 c 注意事项5 l4 j6 A+ E& ~, N
    必须要有终止条件3 S! i- q8 V  |7 K0 Q- ~9 o
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) N, i7 R  H4 A: R6 z- L
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    6 D) Z% d8 j4 a2 ]4 S- D8 {2 R尾递归优化可以提升效率(但Python不支持), g$ _5 p" e0 m4 o- Y2 b4 O

      Z4 B. _, O5 e" B" j4 P! D+ Y 递归 vs 迭代0 `& L8 R- x  ^! K1 r  g  ]2 V
    |          | 递归                          | 迭代               |
    : ~" \1 N9 O) q( v! V2 E|----------|-----------------------------|------------------|: y" l" l) |0 _% x+ c, I; A6 v
    | 实现方式    | 函数自调用                        | 循环结构            |
    8 f, G4 W9 w7 e| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |0 p& t" d& e5 G) s7 v5 s% P- Q0 ^9 w
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 M  `8 @( |3 W0 [2 f
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ' s  W( A% Q  J1 O! o
    ; h9 ]" g& c8 X6 ~+ g5 ^ 经典递归应用场景" u  _! P0 m. R, M& a. L
    1. 文件系统遍历(目录树结构)& [  g! l1 D* w9 l1 R+ Z
    2. 快速排序/归并排序算法2 Z/ n. H3 h1 G8 u2 S, z5 M
    3. 汉诺塔问题* q0 c# C* \! m' |+ u+ y
    4. 二叉树遍历(前序/中序/后序)0 w5 p* f7 z+ h. S; ~
    5. 生成所有可能的组合(回溯算法)7 ~1 _. R* I, b- J8 C
    $ \9 n4 Y- F7 B9 E; x7 _& |* _8 G$ Q
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    16 小时前
  • 签到天数: 3186 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  z; V" k- Q* q, |. h0 U+ P$ h
    我推理机的核心算法应该是二叉树遍历的变种。6 B9 }) y1 j: A+ c0 c6 @! F' `
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . t- _( l7 E, s! H$ ?0 a* K# yKey Idea of Recursion
    3 V! V8 E# c. J% L1 N5 c; Z; |/ j) ^- J; m4 Z
    A recursive function solves a problem by:3 ?8 t* F* |% Q2 u7 b
    - y7 F+ z% X7 v0 C8 t6 A
        Breaking the problem into smaller instances of the same problem.! R" Q5 c# [5 f# F

    - Z7 [! ]! Z4 P    Solving the smallest instance directly (base case).
    & |4 X0 P) ^; f; C2 `* b* R
    . g7 a. g$ n8 `    Combining the results of smaller instances to solve the larger problem.2 i5 g' F  Z  {& ^0 Y5 x; J, y

    ' {/ ~. q, ^+ ^/ ~2 F4 o8 EComponents of a Recursive Function
    . b, X6 g: R" l( c* K2 b+ i& n* S" `/ Z  L
        Base Case:) V, `4 ^. K! }- g; G

    ( G6 I2 Y, q! K9 F4 q7 U        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 b8 ^! B; D- t4 [
    $ k  n' E* r' d0 Q        It acts as the stopping condition to prevent infinite recursion.6 I8 I0 ], F% ]2 s2 R
    1 t0 O# W9 X# ~5 `% C; a, W" O* Y. \9 V
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ) _& y1 {( T, _+ h9 S4 R( M1 C# O! Q8 ?: D( ^0 G6 j* |% f& X
        Recursive Case:
    4 s( V8 n1 n9 x. x- x6 b/ |3 ^$ y# o% K6 {6 f  e
            This is where the function calls itself with a smaller or simpler version of the problem.
    & F& X2 v$ ^) U, }" P! c- ?1 E4 y
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- R1 i7 E/ y  Z  g" ?# O

    , g/ ~* P7 M* A% U  G) {/ GExample: Factorial Calculation: B1 q1 R8 R3 c

    ! s+ ?) s  W# Y1 pThe 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:
      F5 v: }5 N- s- K7 n# k- l
    / i5 {# r" V- |2 O! A, g    Base case: 0! = 15 f/ _% a  m  p- t
    , v! t  }$ L4 {* M1 p
        Recursive case: n! = n * (n-1)!$ a0 d  g0 y, O  I: K6 U6 H; z

    + t, W. X, u( P9 U0 ?Here’s how it looks in code (Python):4 n: C8 @: `5 L3 }3 ]
    python
    0 ~0 X& m) U# ]0 D' g4 s
    ; l, ?" B0 M$ e: }5 \
    & Y, M) c  p0 [' `: a. ?& ]; S7 wdef factorial(n):
    ' L1 z- |7 |2 |) x3 R7 v2 U: i    # Base case
    7 ]9 R4 K! ?" x4 w, K. E7 n    if n == 0:
    ( |- ^  ?# {: y6 I* q4 @+ l+ A        return 12 V5 U( ?. Y4 s3 U+ [+ }2 [
        # Recursive case9 {4 j% |2 v$ O  T0 @1 y4 X
        else:2 T% m3 v2 @+ v  Q* C# m
            return n * factorial(n - 1)
    6 P( A* ^8 g8 N& a3 x+ e* d# S
    , f' s: O; p6 O/ o1 {2 O- ?# Example usage$ l5 Z2 p2 b! Y% B+ v
    print(factorial(5))  # Output: 1205 c1 ?- t* m4 ]* a6 y

    & Y" i, `4 S& GHow Recursion Works' c: n) }. V9 M' H. X8 z  k$ F% P
    * R1 G2 m7 @4 w! s, C
        The function keeps calling itself with smaller inputs until it reaches the base case., }6 J) M/ {+ v" E

    $ r% }6 ]0 v5 V) ?8 M9 I9 |    Once the base case is reached, the function starts returning values back up the call stack.1 J- ]8 w+ {& L& E: v+ @2 l* S0 K
    0 |, h! x& \3 T) u) M0 |
        These returned values are combined to produce the final result.
    $ m6 @! |) l7 L+ H- C* N! ~$ A3 m8 i/ q& K0 U" \5 e, Z
    For factorial(5):7 ~! [! f4 F+ W: F" w( f3 l/ ]

    & b6 P% \  L9 g4 f6 z* a' M
    # i2 A1 i- x  q7 jfactorial(5) = 5 * factorial(4)
    4 |/ m* P2 H7 o/ |factorial(4) = 4 * factorial(3)
    ' t, T  Z7 B+ w" n+ ffactorial(3) = 3 * factorial(2)0 m! @6 U8 p* g- o) v) Z  B
    factorial(2) = 2 * factorial(1)
    4 P! O+ t3 O8 F; _& j  j. ]$ C! ufactorial(1) = 1 * factorial(0), M- j4 |/ V% J1 e: A8 P, X5 O
    factorial(0) = 1  # Base case
      z4 ~- V  E! U" `) f. G
    : ?; g$ }) U2 \+ SThen, the results are combined:" x# E7 k$ n# t7 {/ T# o
    $ e3 ^& \0 \- [) X" `9 A

    5 U! F- J+ G% u5 K% rfactorial(1) = 1 * 1 = 1
    / q' S) R4 {9 f6 q5 W9 {factorial(2) = 2 * 1 = 2
    5 R6 M: r5 o3 ~% h* ^4 h" ]; ^factorial(3) = 3 * 2 = 6
    + K/ W  r  }% _6 {factorial(4) = 4 * 6 = 24
    8 L+ t1 r, C3 F7 s3 xfactorial(5) = 5 * 24 = 120  ]/ x) F0 h: d1 }, ^

    , I- k( Q9 G! }" @# b( C5 kAdvantages of Recursion
    " a2 [+ R) V: }
    ' r  u! j) z/ _/ Y: e- U  k9 I  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).
    $ u5 V# @& G/ U
    + O8 x: R: E; \/ @    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    . b. D! d& t& v0 T3 H( ?( i6 N2 a, ~
    Disadvantages of Recursion- ]# u. X3 t3 N' r/ p7 O" k4 r
    , Y" z; Z( V/ `( @
        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 E% [) K; H- c% M0 C0 D4 |
    ! s6 [. N+ O" |. p
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    6 O- f8 q+ Z% d2 w& h
    & y4 S! w) n  O( E) u& _When to Use Recursion
    " `  J# f: ^, o& C
    7 U% @& C5 S, x! M8 [/ ^) a& b3 |    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # P" [# l9 p) B# O) Y5 B4 l
    4 L' V4 K% E5 \- P* H" L    Problems with a clear base case and recursive case.
    $ z: y7 J: A, ]% [5 F/ @
    # _- k" }) I, z' R' bExample: Fibonacci Sequence" ?2 i# S  ~5 p$ G

      i9 p5 X' G2 JThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    9 g9 f* m2 X  ^3 W4 ?% l/ x' z0 Y& R* ?0 b
        Base case: fib(0) = 0, fib(1) = 1. ?0 k+ Z, M, W4 Q+ i

      I, Y, C; f; I- g. {) B    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . a8 q( B( @( Z1 @9 e6 p' e
    % o$ }5 ]+ H4 m( b( Kpython
    , g, l- R/ }0 W( t8 {2 {' @0 Q1 D  l# w# Z
    6 c# u* p+ g/ j. a! |+ q
    def fibonacci(n):! s( C8 k/ |" M" z% w/ W
        # Base cases
    8 i! J1 Q( Y2 @3 x- h& V    if n == 0:" h" X" r( k! _( F! E. R7 ]  i
            return 0
    & k& c+ O8 `, n: I( c* p    elif n == 1:* e% ]2 S/ I& ~2 ]; C
            return 19 `; t# _0 t2 R7 Q9 H9 u
        # Recursive case7 P- n5 \1 \& i5 c. E
        else:
    + d5 I- Q- b% J, d% D        return fibonacci(n - 1) + fibonacci(n - 2)
    3 @6 v' ?$ O& `4 r
    & a, m% S# b( L# Example usage
    : ?+ X) w% Q6 ^print(fibonacci(6))  # Output: 8
    7 {! ^' C7 e: H, S3 U3 d1 R; V) `5 |' ]7 T9 i
    Tail Recursion
      s: D7 F! S2 q: ?: ~1 b' S0 j( F" a/ ^, t0 S
    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).
      S8 W! h" m7 _- d" L4 G. d1 O( X7 e3 S3 G5 r5 k6 p! 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, 2026-2-27 23:32 , Processed in 0.062916 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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