设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    : j. _; o, u& \& o2 ]' Y( x9 x+ X4 P: a$ G, Q
    解释的不错
    , o1 h9 L$ O" _2 O
    * ]4 e2 x1 O: O/ Q) `( |2 A; F递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    2 e/ J! q7 P4 |& Y. w
    / E( n+ e+ `* X" U3 F  O 关键要素
    5 J& `, V( ]7 }5 ~0 @5 s3 T$ ^1. **基线条件(Base Case)**
    . X: q& M! R) O. o6 L2 [& m6 r   - 递归终止的条件,防止无限循环
    3 _) n& f# Q9 F. o   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ; e& I1 \: w4 Q0 v: |; G$ R; ]0 C/ C7 ]% v, r: z
    2. **递归条件(Recursive Case)*** }; s! g! M' ^1 y( j
       - 将原问题分解为更小的子问题% j* R3 q" o2 Z
       - 例如:n! = n × (n-1)!
    - }3 |3 P( k, M) {3 ~: Y: l6 O' {0 C- |- A1 [" N! t; ]/ ?. S0 q* [. o
    经典示例:计算阶乘
    5 `' `5 F9 s+ r6 e6 V, }python( H/ i4 m1 ^, L: M/ o" F. _; T
    def factorial(n):
    9 o1 n4 B$ V6 D! ]' W# B    if n == 0:        # 基线条件
    $ V* p! U! Y6 F        return 1
    0 _% E  X! o1 O: \( ?' _    else:             # 递归条件' K& F9 e5 H1 n: P6 ]
            return n * factorial(n-1)
    ) B# b" I! d1 t" h! q- X执行过程(以计算 3! 为例):  }9 H! m5 w  f
    factorial(3)8 f. u2 d4 g! `) A
    3 * factorial(2)
    , j8 V' y: g" R; x3 * (2 * factorial(1))
    $ z6 E7 V3 G; N+ ^7 }1 ^3 * (2 * (1 * factorial(0))): ?' B  e3 ~8 D8 Y) j# c
    3 * (2 * (1 * 1)) = 6
    3 U1 |* |7 x/ Z; l  x7 R- T- |+ R) r, A2 f7 l/ v/ R
    递归思维要点5 y  X0 C/ d& y0 G
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑/ \$ z/ Y; J0 {; V3 n$ e
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - a# D9 o, U4 g& W3. **递推过程**:不断向下分解问题(递)
    4 N' a" E# Q" K& k* ?7 t1 H. t* V4. **回溯过程**:组合子问题结果返回(归)
      n% y0 r2 e; ^  Z( }* ?" p# T5 i5 R+ O# t. p! B
    注意事项
    , W. }0 q5 g5 b" z1 _# S必须要有终止条件
    $ ]: Z% D' L, o, X7 Z! x. ~/ h' ]& I递归深度过大可能导致栈溢出(Python默认递归深度约1000层); o5 l- s  o3 T; N9 g
    某些问题用递归更直观(如树遍历),但效率可能不如迭代4 A6 n/ ]  S+ E  |# {* U
    尾递归优化可以提升效率(但Python不支持)
    # w9 F, x# D! u3 X" G' y
    , m1 B0 v, K( a+ a& G2 U9 u4 B- o 递归 vs 迭代
    " N6 b. {( k  s9 S/ d0 n9 L& G( V' a|          | 递归                          | 迭代               |* E! ?# s8 c7 n  s3 y4 m: h
    |----------|-----------------------------|------------------|
    ' [4 N4 q# m$ X5 r9 p, r| 实现方式    | 函数自调用                        | 循环结构            |
    / I- n5 p9 W; A3 \5 ]| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |4 D' u$ d* q& ], Z( k3 E
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ) y3 I3 x* g6 k| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ! d0 ]7 m, c6 \! f0 \- f6 @% n8 S; d8 b4 h: y, L
    经典递归应用场景
    " J' W( w/ G! C  X- x0 w2 \1. 文件系统遍历(目录树结构)
    4 y/ f. u( o% ^0 }* R- c2. 快速排序/归并排序算法
    ' j7 M- `/ v! C' F* l2 i3. 汉诺塔问题
    9 C  z+ v' o/ q6 y$ F) k! u4. 二叉树遍历(前序/中序/后序)
    ; T; z' P- h* h/ [5. 生成所有可能的组合(回溯算法)) s  O9 `# }* D% {. i% k! Z
    . K/ \4 G1 h, r
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    11 小时前
  • 签到天数: 2912 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 k6 `2 A' z" j" \1 W( y8 a- C
    我推理机的核心算法应该是二叉树遍历的变种。  v/ P0 v7 a0 [3 |0 i+ L/ a1 q* {
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:/ B6 b3 U  G  h0 _, O2 t$ K5 ?
    Key Idea of Recursion1 F+ W8 A1 _* i4 s! p2 D
    / p1 l+ {- o+ F* l, |! H7 K
    A recursive function solves a problem by:
    9 P$ z' ~+ f( Z8 A4 C
    - S2 A, Z5 V' p4 ^4 K    Breaking the problem into smaller instances of the same problem.+ W. _' T6 z0 ^$ o

    $ F2 D. P8 T  |9 }" I( ~2 }    Solving the smallest instance directly (base case)./ Q2 D5 C$ K2 i7 J  `
    % M% \. w4 q6 X; X9 v; O  o
        Combining the results of smaller instances to solve the larger problem.) l& i- M, U% b% V. n" e5 z- k* K/ b

    3 g' ?# [) |! E6 y5 G1 Y3 p& ZComponents of a Recursive Function3 e9 S0 F' b" Y9 P

    , z9 z# A: N. p/ {9 A& |6 N& e8 O    Base Case:/ [8 x, ^& k; f' U0 e
    2 n( B4 p$ d, y1 \
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.0 X5 W/ `$ f- y9 G) H
    ) S  r& j. f. j: y  {; U* ?
            It acts as the stopping condition to prevent infinite recursion.; g$ e4 d2 [, S( V# L3 j; P

    . F. o9 F4 ?7 D, r$ u7 U8 _& P        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.% z) I6 ~0 y$ n! C9 d- a
    6 j4 U9 ~) B. |/ ^2 J; }- N' f# N
        Recursive Case:$ Y5 S; x' L+ v
    2 n5 v! }  W8 s# Y  E9 K
            This is where the function calls itself with a smaller or simpler version of the problem.  N0 W! e) Z7 n! X' u

    1 x) u  y1 E) _2 J- e        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    , v/ V8 {; B% J9 k1 s! Z# g3 Y2 p8 y3 b$ l) }$ _% u( |
    Example: Factorial Calculation
    % q# x' g. U+ @2 [$ m
    + Z, b- X: ?6 }! Q+ B+ o- R3 l1 dThe 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:/ s- f6 |5 C7 W1 R' q1 z; G
    - s5 ]/ ?, y+ M! m# x
        Base case: 0! = 1
    6 C0 v6 d  z6 {$ U
    7 a" P/ l& o" Y% U) r, m    Recursive case: n! = n * (n-1)!
    . \: J6 i: E# N( w/ f" ?; `6 z2 s0 U$ N; a
    Here’s how it looks in code (Python):
    6 i- [" U% U& v  g  D3 qpython
    0 R0 {+ l) p6 g; ?5 L0 Z1 y1 ~8 ^
    ' B% P9 \( b7 _: s3 W! u7 Q) c' Z# e* ^6 S
    def factorial(n):6 r: D+ K$ ^# v6 k' J6 e
        # Base case
    2 G3 A# ^8 G2 K4 h0 k    if n == 0:0 r: Z" z3 O' m2 a! p: ~6 ~+ v
            return 10 |6 E5 o( m: o
        # Recursive case
    # ?+ c( A  Y2 A- I    else:
    : d: M1 c2 }! B- s8 u4 l        return n * factorial(n - 1)! t2 x. z; M, M1 t% y' l$ [
    2 \, Z$ Q% ^7 y  ]( N2 a
    # Example usage
    4 R3 J7 d2 m3 Fprint(factorial(5))  # Output: 120( m  w4 G) R6 o$ y- @  k

    0 [2 N0 l0 N2 p) MHow Recursion Works/ {6 L* z- Q2 \4 W4 x8 k
    8 L: Y. u( Y# W& p
        The function keeps calling itself with smaller inputs until it reaches the base case.
    , }1 F7 V9 q8 n( J0 [$ X5 ~3 S
    * ^% w( J6 \5 d! {2 G    Once the base case is reached, the function starts returning values back up the call stack.6 m1 B( x( s" Q6 M  z  @; u, Y" |

    9 ?7 {: I0 U/ _  b& V    These returned values are combined to produce the final result.( Y6 P+ s$ _7 Y4 \4 k, S
    ) c& C! A6 |7 c: X7 l7 ?
    For factorial(5):
    1 M/ ]9 M8 k/ g, e$ v, }  L  o0 L/ r+ K. ?, J8 Z) |2 Z( C, p

    2 k4 t$ {* \. F: p5 Tfactorial(5) = 5 * factorial(4)* z$ u% Q- U2 X5 |7 M
    factorial(4) = 4 * factorial(3)
    ( [1 R9 A1 R+ ^9 G' Z" C# `factorial(3) = 3 * factorial(2)
    3 x* \( T; R" G& [) Lfactorial(2) = 2 * factorial(1). ?" e- g) V. Z" n6 A& c
    factorial(1) = 1 * factorial(0)* [- f3 {0 v6 Z
    factorial(0) = 1  # Base case3 D  c: d) ]7 |+ O0 g

    % ?- Z/ s0 `/ H: {Then, the results are combined:
    - ~; r) J$ h2 @$ L5 F( t
    " }) g( I& d0 c3 d# k, ^. X9 C) K5 _+ a8 f
    factorial(1) = 1 * 1 = 1+ c' b) ^% V3 U) t, p- N; x; f1 F, A
    factorial(2) = 2 * 1 = 2
    0 C3 E) x- v1 w( G' S* _# xfactorial(3) = 3 * 2 = 63 }0 a0 T! D% T3 C$ T9 x
    factorial(4) = 4 * 6 = 24$ g7 V# H* ^" l1 b
    factorial(5) = 5 * 24 = 120
    3 a+ J' E& _& I1 q( r; h9 O
    ' I- U) x5 i5 \+ K  pAdvantages of Recursion+ w* u; A2 O4 Z, t

    ! Z3 y  S. i! c4 X    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).
    2 B) ^4 m5 `2 @8 p8 q5 [0 T
    , M+ h. T! D9 o" c4 Q. y    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    . h! A1 C2 n1 s5 {+ a" |: E  z5 O
    , A: c5 h5 C" A+ ]& rDisadvantages of Recursion
    . U. h  V' T% d+ g* n  z; l6 n( M, _8 Z3 }. O- m6 e
        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.  E# L. O3 v( r

    " ~1 ^7 k0 _5 t% q* ?: N" k* ~    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . Z$ u+ Y6 A! N+ c5 l- i1 s- ~4 c( I% Y/ d, A9 t- T6 C4 }
    When to Use Recursion$ s' |: x8 J9 V& j; J' F
    ! c$ s- a3 x7 C' |/ L; A" p
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
      T4 y( O( i) g6 Y) \2 S
    ! G6 O1 S; ]; Q% M- Z% \: ~" W    Problems with a clear base case and recursive case.
    8 }2 l4 d$ r; I+ n8 {1 `1 w
    ; k* q  E# o% p+ Q% m3 eExample: Fibonacci Sequence
    : f3 O$ A4 ?! J# f/ |8 `- r* _+ ]
    ' j7 o9 _  a5 W! {% U/ H+ tThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    * {: g0 ^2 j/ i) n0 N) [" `6 E& m: ^
        Base case: fib(0) = 0, fib(1) = 1$ L8 i: m0 w# f% k2 D- f( l* d

    ; _" y  J- `8 L5 [$ N' T5 t7 Y    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    8 }5 i: I7 n8 _0 |- S( F$ \
    9 M1 _* Z, y& C% ~; u7 m/ {' Qpython& a7 w( d2 r9 Q9 E4 a8 {  q5 g
    & i( A& H4 n2 t) l; d; m
    # o! H- s# u7 n8 @5 }/ P
    def fibonacci(n):4 f8 d& C/ B5 E# G, I
        # Base cases: d. z0 F  w  O
        if n == 0:4 y' o- A1 @3 H6 Q6 G/ b3 o5 Z
            return 0( }% a/ M- h6 _  \& A7 }/ n
        elif n == 1:+ N8 m. {: ^, S# D0 }& Y
            return 1
    ! N) D3 c% {- i+ I6 {7 |    # Recursive case. S$ D8 d, b& E5 {$ _
        else:& S( g  {/ B; h- R  i4 M
            return fibonacci(n - 1) + fibonacci(n - 2)
    # ?$ M8 m9 R8 T" o) ]& c( t# ~# b) c' K& C, i
    # Example usage
    " e9 `) C; {% }; R$ M! \2 Bprint(fibonacci(6))  # Output: 8
    - x$ b0 A( x2 ^; d' ^  Y8 D2 b0 R9 ]8 A4 W) r$ W* \; Q/ N; T5 q
    Tail Recursion- w! S+ r3 {1 {

    8 Y* q7 A/ v5 c& k2 i  `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).
    5 i5 j; J0 V0 r7 A( z+ l$ \5 Z. J7 D! S3 M
    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-5-6 11:57 , Processed in 0.036840 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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