设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    0 R) U" N3 {% t) Q2 d/ j
    ' _' O3 m, c; o4 b  }3 l$ q: y6 P解释的不错
    7 e  ]( i- R8 R1 W/ |1 \  z
    : V; F6 Y  k' b; U递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 f* _5 `# r) r8 _. F+ g7 d0 u" o
    关键要素
    # Q8 |4 U% `+ U" T" U% Q1. **基线条件(Base Case)**, D$ C( o) A; Q- B6 M! N, o  n
       - 递归终止的条件,防止无限循环, H4 H( L6 l7 O) E# Z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ) }0 l0 T" O* ?5 |5 k4 G# ^2 r4 y% d& r
    2. **递归条件(Recursive Case)**. r! |" B$ |2 k9 ^. S' C' ~
       - 将原问题分解为更小的子问题% [& m6 }% U+ d5 [' H) b, ]& d: L( v
       - 例如:n! = n × (n-1)!
    & u3 X3 v$ x; u5 n) b) f3 ~- {! r8 {3 B( Z/ C0 T, |
    经典示例:计算阶乘
    * u2 Y4 e( R1 r4 }; T# n, J! Jpython0 W& |8 g. Q8 p1 Z
    def factorial(n):3 C$ V- f/ k% i8 J$ o' \4 [6 q+ z
        if n == 0:        # 基线条件
    1 u' V) u1 [0 U- G# b* `        return 1! t9 ~! Q+ u* k" B% |" x
        else:             # 递归条件
    8 d; B6 Z, v. G        return n * factorial(n-1)
    3 l  [$ f3 m+ T* y8 P, g执行过程(以计算 3! 为例):
    5 }" w/ Q7 Z5 I. Q8 U7 B- ^factorial(3)
    0 X; ]' p( E7 ]3 * factorial(2)2 c7 H5 u+ B1 l
    3 * (2 * factorial(1))
    ! P! Z+ `  O" e1 D# W2 H, N, _3 * (2 * (1 * factorial(0)))" Z6 E' s$ l7 u7 N* b" y
    3 * (2 * (1 * 1)) = 6
    , [6 l% ^* ?, `  H' _3 S- E5 w  x5 F
    递归思维要点  Y4 |( t# O' ~- x% V# z
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑$ t# i  `) N% L- D
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)/ F  p4 b+ r- }
    3. **递推过程**:不断向下分解问题(递)6 N# Z$ H  x7 `1 u: a& l0 A7 l
    4. **回溯过程**:组合子问题结果返回(归)
    ' x" ?7 c. E' D" u/ n1 V  e2 S9 o6 J5 F6 b- L0 u4 {
    注意事项
    * P$ B& {5 P9 i- x必须要有终止条件3 E/ a- Z; t% a' q
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ r) h; A. j, |9 T# T; k; ]) F
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    4 U0 ?5 c7 t% X尾递归优化可以提升效率(但Python不支持)
    8 {  _! {5 T/ P3 |& f" a" k
    % V9 V# w7 c$ Z! R 递归 vs 迭代
    9 C& a: F+ M5 p|          | 递归                          | 迭代               |
    6 n5 U. l# l1 z3 y2 A: |# ?% T  ]|----------|-----------------------------|------------------|- T; l; N8 p* W% `5 [
    | 实现方式    | 函数自调用                        | 循环结构            |
    - u: B9 G, P, `3 {( n$ B! }+ ]| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    . J% ?3 |; k* R+ I! C| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% H" ^" z/ N( G* w: a, k+ h4 d6 ^
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |; J) K1 J. h' G4 j7 ^4 ?1 g$ a  U
    3 X, w; T" m9 a9 N- Z; |3 m8 R
    经典递归应用场景
    / L7 f! ]5 S) B1. 文件系统遍历(目录树结构)
    0 M- O, S8 J( W2. 快速排序/归并排序算法9 m: T$ I3 A2 Z, q( q7 B7 E4 Z- }" ?
    3. 汉诺塔问题
    / O3 W: x' E: R0 y2 k. \* M! }4. 二叉树遍历(前序/中序/后序)
    : v# y% m) L7 e5 }' d4 @5. 生成所有可能的组合(回溯算法)! `% X' ~9 `6 s6 k
    ! h& y" K& ?3 q
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 05:14
  • 签到天数: 3205 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ z& l9 F( V) H* a) `8 k4 `
    我推理机的核心算法应该是二叉树遍历的变种。; i+ a/ ?& p! z4 h% r, e
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:$ x8 g. h- a; B) e0 S) p, l3 X
    Key Idea of Recursion
    6 Z: C9 z( \% X4 L% c
      j' s0 h: |( X9 r# W; ?A recursive function solves a problem by:* z+ o% Y  z7 N2 M& H& R

    4 f2 ^' [' w, `& b    Breaking the problem into smaller instances of the same problem.! f" y1 D0 S/ u$ S9 N/ O% I
      \2 f8 H  y: T  Y1 [' I, b  B
        Solving the smallest instance directly (base case).2 J% J+ ?* Z8 ^, z
    * K* Z+ G- q5 X. S. v( S0 U3 u' ^0 r
        Combining the results of smaller instances to solve the larger problem.1 z' I( u$ k4 {- ?  t4 D
    , P6 F' H+ E2 T( {; z" m; ]2 d3 Z
    Components of a Recursive Function
    & `' L. ?  W# r2 ]5 x# c' y  u  o4 y: y6 C$ x& A
        Base Case:4 {* o, l* r! Q
    4 g0 B$ T( u- _0 v$ q/ C; I
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: Z: l4 u' ~* U% i5 Y6 U
    6 K* N' L- m- t+ o
            It acts as the stopping condition to prevent infinite recursion.
    - k5 Z5 v# v1 B4 o
    % {8 {7 q7 c' _5 K' q; F) [- C        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 e, I% X1 }% M+ O/ x( V
    + F6 c% [- g+ w3 F6 @    Recursive Case:
    6 M; l& P6 K) H( t, X
    7 G' X  |0 P6 M        This is where the function calls itself with a smaller or simpler version of the problem.' e  X  ]6 G! _

    0 z+ C8 M2 O5 }$ d$ _        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# ]; ^' l/ a+ N* I

    ) N0 P: E8 O/ h4 b  `+ b! {! P  fExample: Factorial Calculation5 |4 w) {8 ]3 ~

    / S( m2 _1 Y8 E3 p6 Y2 ?+ A8 V& hThe 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:
    # `% E6 A( b( p/ h+ z% |5 B! f; f- [3 t* p  K: t2 d
        Base case: 0! = 1( u+ H0 r3 c& ]7 B: n$ A
      Y  o" r( g( Q1 l7 p- Q
        Recursive case: n! = n * (n-1)!" y6 I4 J# E8 p9 ~

    7 Z8 `4 e; ~- c% jHere’s how it looks in code (Python):
    9 J; {6 U# \, ipython
    3 j# ]- G7 J6 {2 N- A' R( p$ k0 a8 C; P

    5 Y5 R2 g: V6 f) S8 R! _def factorial(n):% o9 |/ l6 x1 ^$ g' j8 [( K
        # Base case
    5 {$ C8 w( m7 k9 C    if n == 0:
    3 [4 }# ?4 z7 @. ~- g        return 14 a2 I! `3 g3 s% ]
        # Recursive case5 D/ }4 Z; A! ?. ?0 i# ?% E
        else:
    0 u# [) g! B1 l        return n * factorial(n - 1)5 x" j# s. v( l. v' M2 i0 a
    0 T4 Z( y% C  x
    # Example usage( {( p! L6 t  ]: X8 y5 H5 P
    print(factorial(5))  # Output: 120
      z! b- N/ W8 u! q* z7 z$ p$ m& l- P& g7 ?' u0 j1 s) i8 q
    How Recursion Works
    : t! H; p3 T% o4 [& ?
    6 a3 @: H# V* k6 u7 N4 d    The function keeps calling itself with smaller inputs until it reaches the base case.5 f4 s" H. T0 J" l
    . G  J5 ^- ^1 W& |, L4 g, D
        Once the base case is reached, the function starts returning values back up the call stack.
    % @0 q9 \7 ~0 w+ E" ]( D) x; x; L' t3 M. l
        These returned values are combined to produce the final result.2 n, \7 _1 V  m% q# T
    ; e+ d$ V3 d" `" _0 w8 k: U
    For factorial(5):% M' R' p( ^+ O- a3 I& r2 r, m

    5 a8 |1 [: F3 [! I( _/ k6 X2 V& R0 V. C
    factorial(5) = 5 * factorial(4)* S# N% y& q# }) q
    factorial(4) = 4 * factorial(3)
    3 {, W2 C* Q7 J8 Ufactorial(3) = 3 * factorial(2)
    4 `: @; n$ m/ M; x& Y& w  i) X; Cfactorial(2) = 2 * factorial(1)5 d1 y$ \, L- _4 C7 Q3 _0 x. a
    factorial(1) = 1 * factorial(0)" `1 H0 K0 Q( ^" g' i
    factorial(0) = 1  # Base case
    % S7 w- j! y- N9 \" x: ^/ c. c9 B9 u( w$ s9 e5 s
    Then, the results are combined:
    1 a8 D/ c9 @$ j" G5 R) d
    ! S  Y8 p$ M+ s( P0 {- }6 R
    6 F2 Z5 q& a( T( R  y- W# k$ mfactorial(1) = 1 * 1 = 1, [8 S. |: Z7 z) u% c; N. A0 j" p
    factorial(2) = 2 * 1 = 2
    / @3 @9 R( e1 V% @5 p( }5 L4 j, \0 _factorial(3) = 3 * 2 = 6
    9 g$ ]; y* U2 h4 z5 Cfactorial(4) = 4 * 6 = 242 d2 G, l* E( K* _
    factorial(5) = 5 * 24 = 120" P0 |  S" L. {: p
    3 j4 s% p" l! p+ N
    Advantages of Recursion
    ; ^0 t2 c+ P% M3 ?0 N8 \
    . h( U- w( \- Y' s9 [8 r& h9 Z5 L    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 H3 M1 q8 `& ~0 y6 x2 n
    $ h8 ]% a) m( K  Z7 R+ G' I' k6 P    Readability: Recursive code can be more readable and concise compared to iterative solutions.+ @' C1 H* q" K/ W% ?
    - ~0 a  j& p$ {& }) w3 W# w! d4 a# a
    Disadvantages of Recursion# ^- D: V0 C, V- ]5 q0 w

    - W" V* w1 |; \) H  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.
    - F% x/ o) N+ o, s3 R% |4 w" R. u1 r- d
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ) `  _3 Q5 i% t2 Z) V8 @4 j/ x& h1 e* \7 ^/ y
    When to Use Recursion
    . T# a  _6 u: E- H" h9 L+ Q' o/ R
    . M% K8 y. t. M    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 O  P& N. n! F9 c9 P

      j/ n( P; s1 E- d/ ]    Problems with a clear base case and recursive case.
    9 @+ Y2 a. y% |" c0 K9 i
    2 D. J/ P+ x$ z8 w0 ^2 }Example: Fibonacci Sequence
    / ]3 O; A* r: v, e$ D  z# L
    # q' B+ }* m# x: f9 [9 V9 f  `6 pThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    : t1 \& M# a; ]9 d# n+ H, c3 `: I& w# G. D$ f! Q
        Base case: fib(0) = 0, fib(1) = 1
    8 b8 E' B+ {, u. s& ?
    9 A" G9 o' c! G) q' m    Recursive case: fib(n) = fib(n-1) + fib(n-2)& N- l$ l0 K& e+ V, [0 B

    7 L. c4 W& q$ |1 X- D% L9 Mpython7 n9 u) }( [$ e4 g5 H5 G/ J

    / y: T) z. m% u+ ~5 `& M$ F: t+ {; o* c! z5 N1 J
    def fibonacci(n):, n, \+ G  a3 W- W- c# w
        # Base cases; Q$ ~# V# d+ W0 ]' {
        if n == 0:
    & k* y, m$ M" z5 R/ f7 O- d        return 0
    2 V6 u8 X) V+ r) D    elif n == 1:8 L- f; O3 L$ t3 N  N- x/ P
            return 1
      U; l) L$ X3 c6 I    # Recursive case
    ) `- P* q8 R8 k* D3 G    else:$ d7 y% o$ |) `0 |4 K
            return fibonacci(n - 1) + fibonacci(n - 2)
    8 q) x: @: ?1 d5 Y: F1 U9 y
    5 H! u. ?/ p7 ^9 A9 u, _# w7 G/ M# Example usage
    . O1 t1 `; C# B7 h( j, w' ?: Qprint(fibonacci(6))  # Output: 83 U2 B* y9 F' ?% @$ L: b

    : T) ~! c% x! Y" j# U% C- pTail Recursion& N# k. Y# [6 N& ^3 P
    9 u) J7 b3 ?/ d7 l% }
    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 s/ c8 R: C( N3 V4 S# a% s  i7 {+ ^- }& q+ T
    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-3-21 01:36 , Processed in 0.062986 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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