设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 K8 d+ Y" T" z5 R
    " N+ I0 |+ g/ c5 T& q+ c3 G6 c  q/ S解释的不错6 R6 j4 i, E, K8 y- ]# G

    5 O: d& y$ n% l; y( L! W递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。5 Y! ^; w4 o) l
    " G# G1 t- Q8 n7 a* D% o: X/ U
    关键要素/ D- |) H) R2 u: G0 Z4 Z1 @! n; ^
    1. **基线条件(Base Case)**
    - |  _% t. c4 b3 G5 ^; I   - 递归终止的条件,防止无限循环1 B" i& h2 x3 W+ m8 Y8 J# e. {
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ L2 c1 G) J- i% l1 s

    - X7 Z4 u/ w* e7 v, [$ z, I5 t! v2. **递归条件(Recursive Case)**$ b2 P3 _2 e' C
       - 将原问题分解为更小的子问题1 v7 v/ f+ ?) Y/ I. f* m
       - 例如:n! = n × (n-1)!: x" L5 L/ {% z" r5 }( o
    8 p  z/ M6 {" U; s
    经典示例:计算阶乘
    $ J7 N* v# }3 l/ o$ Epython
    ) b+ |! i0 J3 @2 n, T( q" S) ^7 P+ Gdef factorial(n):
    ! v) U( Q# B8 {+ i6 u" z4 c    if n == 0:        # 基线条件: S. T- b4 S5 g- B8 Z1 m: K
            return 18 G5 z& x% e) F8 ~+ i4 ~
        else:             # 递归条件
    ) p7 r! e% @* g+ _0 D9 s' }  I0 S' G        return n * factorial(n-1): [1 ~$ \& m, A% l" Y. J
    执行过程(以计算 3! 为例):/ Z. r! T! G( {* m/ |4 g
    factorial(3)
    1 s  N9 P& A& K4 Y/ V  t3 * factorial(2)
    8 D+ l3 O$ u3 M/ e- H+ z! @3 * (2 * factorial(1))7 p, U. W" a. R7 T: Q3 g
    3 * (2 * (1 * factorial(0)))- F! d6 J2 J9 ^3 M$ D! x
    3 * (2 * (1 * 1)) = 6
    " n- _- S: ~, x% d; J  a: H
    $ c3 z1 d# Q5 K) m4 D 递归思维要点. u% s) ?" S( z7 @
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : c$ T. I/ Z1 {1 I4 L& F. d% F- U2. **栈结构**:每次调用都会创建新的栈帧(内存空间)5 x" q3 e0 Q, K. Z
    3. **递推过程**:不断向下分解问题(递)7 x3 l* S; T+ g9 _
    4. **回溯过程**:组合子问题结果返回(归)
    - C& @" M; O: o) p5 x& f
    3 V, N& H+ v& V; N" e 注意事项
    : N4 E- I. m0 z, a( ^' u$ p) V$ [$ m必须要有终止条件1 s, h# y% F0 |% X3 V
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) C' I! t7 e* B9 T" i
    某些问题用递归更直观(如树遍历),但效率可能不如迭代3 m% Z* m6 Y, b+ f5 W( U
    尾递归优化可以提升效率(但Python不支持), a: `( X: G$ `, l, `

    & @7 }: t# h, L- E) R/ X 递归 vs 迭代/ j; e: {' {4 Y8 j$ c
    |          | 递归                          | 迭代               |, m1 U6 y% O. d: v+ k9 `& X
    |----------|-----------------------------|------------------|
    * F" [8 A) s5 m! y1 I| 实现方式    | 函数自调用                        | 循环结构            |
    & ~; V$ t- p$ s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! E) c/ e, b$ S* d- S| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |9 E5 f  s" A* @/ J. X
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 o" h* H$ l) l& B- C+ K

    7 u- K* P0 U8 I% R& m6 R 经典递归应用场景
    7 i: x6 W% M# y3 O1 ]1. 文件系统遍历(目录树结构)5 `# R0 m* q4 I: S
    2. 快速排序/归并排序算法% z( [, m, P" i
    3. 汉诺塔问题
    $ b7 J5 F5 R7 ]" D4. 二叉树遍历(前序/中序/后序)0 F, a$ y) g* F( V  ~3 j8 x
    5. 生成所有可能的组合(回溯算法)' t0 p& s, X6 _4 X( J- Q, S
    : x  @0 i* O' _" @9 s
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    前天 06:41
  • 签到天数: 3102 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 m+ u% Z" E( h; P1 t8 X
    我推理机的核心算法应该是二叉树遍历的变种。/ B3 Y- r9 |7 ~$ C9 Y% \
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    9 c1 ]1 K+ ^1 O# GKey Idea of Recursion
    ' n; Q9 J4 P/ k: g4 P& w
    0 e3 T- P( ^& ~7 Y& b# {0 r1 gA recursive function solves a problem by:
    9 e4 d  \$ |; x) E6 @7 _4 s5 ]. N8 D  v0 V; E7 E3 N) @+ h
        Breaking the problem into smaller instances of the same problem.
    , z& I, D" t: \  \
    3 ]$ ~6 T  z: g; e1 B7 A' T( _, W    Solving the smallest instance directly (base case).! _9 H% ]" Q9 R% W8 l- x, y! _

    9 }* y, d* Z9 W" P; O    Combining the results of smaller instances to solve the larger problem.1 H, q  g. A- {3 S8 M

    7 [5 Q1 a* V3 h: N% S4 u! x  \Components of a Recursive Function
    8 n% z7 T  g4 I, ~, M" m' ~3 E% _( g; d
        Base Case:
    + N" E; g- X0 _; R6 Y- q' v5 U3 U0 l' T7 F. D0 p; a, l. z# t
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 {; x0 r1 T& T; i1 F, ~+ G/ H4 T* V  p: |% o
            It acts as the stopping condition to prevent infinite recursion.
    0 D3 c' J( k  m$ q" D. `9 b
    0 m: F  \: c$ ?1 N' i4 i        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 ~7 w% k; _' T

    : v5 @9 C. b0 E  [    Recursive Case:
    * e% U2 U6 o$ S$ }' @" d( ~7 m1 n, F1 J0 y3 _
            This is where the function calls itself with a smaller or simpler version of the problem.
    / _* x; G" t( m1 t5 K6 L. r' _+ \' N# I3 ^
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." l/ O9 V: ]1 e, O- y, j2 m/ g
    ; M. c/ s* b% J7 G; M. L7 m$ q/ o. `- n
    Example: Factorial Calculation
    ) \( C8 i4 z# L. L% n( l% J" {) z+ X+ f7 a2 }
    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:# o# [' Q9 M) j7 z# T
    7 D; n5 Q8 I5 ]1 T
        Base case: 0! = 1
    9 \$ H3 K3 K" I  X5 x. E3 r8 z  Z9 @' P+ ]& R
        Recursive case: n! = n * (n-1)!# j  e/ n# y0 A" R# w/ V

    * ^) b7 K. R1 k( _  _% D- u' ZHere’s how it looks in code (Python):0 J  K. ~2 @/ |) ]; v# ]
    python
    ; G* a" I1 f; o) ?) l! T3 d7 s$ [6 Q5 }
    - r# ?; s, {# h) J
    def factorial(n):
    + O3 Y0 J: n6 {7 d, p4 j% F  T    # Base case  W% F/ S% s' @4 R) U% w: l
        if n == 0:
    : R& N! R9 r0 t3 x" g1 o        return 12 w8 m8 O& \# ?) ~. C# z1 V; W) x
        # Recursive case
    3 ]# X% y( ^+ m3 Q5 v    else:! L% n- y# @# Z% D
            return n * factorial(n - 1)2 Q: \6 T3 E1 h! S, y5 P

    3 Q# c' i$ T6 X3 R7 b# Example usage
    * U' S% E! \4 {  fprint(factorial(5))  # Output: 120+ `! T! f1 e) \( @0 {0 c
    6 v3 A% d; p  ]/ X; D- g6 ?
    How Recursion Works- \. a. y. T# L# x
    9 |% @0 G8 Z3 m8 E% i% ]3 Y
        The function keeps calling itself with smaller inputs until it reaches the base case.
    8 h7 S2 j6 _) T0 M! T8 J# |" O' l7 p. ]; Z/ k5 z
        Once the base case is reached, the function starts returning values back up the call stack.
    ) @( J- S" \7 J' W* g
    8 c% ^4 d: _+ i" T7 ]: A( K    These returned values are combined to produce the final result.
    4 F% w# ]# q" O& }) @& \5 Z7 h
    6 R# ^! ?6 E; p# Y5 DFor factorial(5):
    ! `6 ]  t8 {; A+ q! L( G( i) ^! p: y

      r- \) W+ X6 a! ?# w! ]& V& ?factorial(5) = 5 * factorial(4)
    $ O7 v! v) w1 ffactorial(4) = 4 * factorial(3)
    1 j; G- V# n/ {4 Rfactorial(3) = 3 * factorial(2)
    + N/ J: x8 d3 E: Vfactorial(2) = 2 * factorial(1)
    5 {' ]2 w7 P# s+ r7 y1 ofactorial(1) = 1 * factorial(0)& D  E, f$ {) f: ^# [
    factorial(0) = 1  # Base case
    , t$ h" T. F: \: }/ Y8 \6 \8 c3 j5 G9 |' z
    Then, the results are combined:+ Z1 q6 H* t. r: \$ H- W

    0 m# W0 U1 U$ S) A" w0 e2 Q: U1 g3 z, Z8 g
    factorial(1) = 1 * 1 = 1
    : I. s% U/ D5 m3 t$ Ffactorial(2) = 2 * 1 = 2" g5 w& [: Q/ v8 c6 E& k
    factorial(3) = 3 * 2 = 6, S7 e! F. i) ]; I, W: ~; S
    factorial(4) = 4 * 6 = 24
    5 q; z1 E; g8 {' P$ c, p2 H+ G$ B  zfactorial(5) = 5 * 24 = 120
    3 W9 [! ^" y0 M* _  p
    . Y- ~* j: h! ?Advantages of Recursion  `. i5 W- ~/ o# }; ?$ V

    ! P) d) y7 N$ h( 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).
    + R# b% t3 q; o
    0 o) j* x! m3 F: D' X, R    Readability: Recursive code can be more readable and concise compared to iterative solutions.2 o( I2 V3 g5 ^7 S( u
    8 s' ^* o' E2 X4 d; n$ n
    Disadvantages of Recursion- q5 S! ~- c1 h, t$ b% L
    - f, K$ i: X- d  a8 W
        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.
    # s+ E$ e/ n: S4 b. ?  ]' g& z
    + _+ q% t) E: Z: {0 s5 m    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ; T: J6 E- {0 u4 D; Y, n1 ^
    & c1 C& Z8 Y. N! W6 F9 e; HWhen to Use Recursion( r! `0 K5 d3 M# ^0 `7 N- Z+ A1 L, Y/ k
    ( S( s$ l( g" x* `: j
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % C( a9 D% n& M' K' j) ]  S. x. K, n9 K' [* ~
        Problems with a clear base case and recursive case.* O8 n7 W/ Q: t2 `' Z

    4 @, X1 @( ?9 r3 JExample: Fibonacci Sequence6 ~% f' T. [$ X7 G2 V
    ) D/ r8 ^/ X" l- |
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:; X2 K3 F: `+ a5 m% s# g
    9 Y  b  X1 F4 O. {
        Base case: fib(0) = 0, fib(1) = 1
    ! c$ K6 j2 N3 R1 f" y" d' M
    ( T$ Z8 {2 ?, @$ H( i/ ]    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    2 X3 J& I  c/ ~) Z+ N8 _& V- t# ?5 h! c3 U: _
    python
    ; Z3 R& C8 ^7 K& w- v: z
    , ^! O- w  W4 O) ~5 g# g. o$ l4 r( t5 |* b( Y0 w* T
    def fibonacci(n):7 j& h( \5 ^0 `6 Y) W
        # Base cases
    # q% B5 u9 O! u6 g5 |    if n == 0:* K5 h8 @8 ]/ F. o, l4 y5 N
            return 0- f4 O8 v0 I; F- e0 c' h* M7 P; Z
        elif n == 1:+ i' t0 q, |# w! a
            return 1, G% X; B! B. N) H$ O
        # Recursive case
    . g! s7 Q" z. v6 H) H    else:  j1 M" p+ N6 ]" j8 G
            return fibonacci(n - 1) + fibonacci(n - 2)
    " @0 a+ S; ?7 @6 z2 Q
    3 q  |, F4 o0 {9 w& h( f# Example usage
    9 e( V3 h8 `' f, zprint(fibonacci(6))  # Output: 8/ U/ n7 ?! p; O* A/ l( h

    7 `4 b7 r' C0 x9 b, e9 x# Y$ HTail Recursion
    8 p, ]$ |! J# d1 r3 S6 p; ~; ]) ?+ T
    ( G2 V; ^( l. I  t) L5 QTail 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).1 Z# e2 {0 K& F8 a; C7 t4 c( ~
    ) W& @% C! ?6 {1 m( s7 f
    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-28 02:36 , Processed in 0.031985 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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