设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    $ C% ?7 V/ M3 T% r$ O) Q2 a, P
    ' r" J2 n- r- N  W0 V+ s解释的不错
    6 l6 d9 v# M3 F5 Q' Z' @# _. b- ?6 H: V+ y& m* Q
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 q% b% w: Y' s4 r) [
    1 d1 F; j3 g, Y2 ?0 i% o- L2 M 关键要素
    ; D5 H( l) `) L7 A$ E. O1. **基线条件(Base Case)*** y) U3 C. I4 m, [' T0 |! r
       - 递归终止的条件,防止无限循环( s5 w$ l) ?( @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# C1 x/ X' L/ d( N+ j9 }$ U
    * H, I2 b, b4 g& N6 b
    2. **递归条件(Recursive Case)**
    $ ?% ?6 |! n0 T0 P( @+ O1 w! [   - 将原问题分解为更小的子问题
    $ N/ l2 T' R* H5 M   - 例如:n! = n × (n-1)!
    % e/ X( l  t, t$ x3 N7 K& o, D" u0 r1 m) x1 P( d* h! x) r! A* m
    经典示例:计算阶乘
      x+ p& D( h1 c0 opython
    1 |  o0 E: r5 M) A- N. u2 ~def factorial(n):, V8 B  `! h& I8 L& P+ e$ C
        if n == 0:        # 基线条件
    . p  z+ v/ }" b  _; |* c! A        return 19 N# |* `# W9 ^" a# |% b# U2 l5 w
        else:             # 递归条件
    3 S8 H( o; A: x) p        return n * factorial(n-1)
    8 s' z" h- n5 D/ i2 K0 w执行过程(以计算 3! 为例):' ~9 e* s+ ~" h/ _" k8 Q
    factorial(3)6 Z; ?/ L. O) i2 K' p3 G' }' U! c+ V
    3 * factorial(2)0 z, K; H1 ]/ r
    3 * (2 * factorial(1))1 w% T; D$ W7 s0 P# V6 I
    3 * (2 * (1 * factorial(0)))
    * N/ e- C5 N/ {3 * (2 * (1 * 1)) = 6, j( F! R1 M3 S: [, ~

    * |3 }/ x  \6 W4 B! X1 i 递归思维要点
    ! _$ l' @5 x- V* g* K$ r* c1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    4 O5 k9 }2 o4 v8 ]2. **栈结构**:每次调用都会创建新的栈帧(内存空间)- G% ?3 v' I+ T
    3. **递推过程**:不断向下分解问题(递)
    ( R0 v: c$ W; k, v" o+ D4. **回溯过程**:组合子问题结果返回(归)
    0 p! u% s" z0 e: ^, N/ }# A1 F/ G( |7 T( _' t3 G9 W
    注意事项* s' C6 E& t# N
    必须要有终止条件
    + N* L0 u5 S+ L7 ~+ c: F& `递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    $ {" L" A, w; f某些问题用递归更直观(如树遍历),但效率可能不如迭代; g2 b& _& q& N" J. W8 y4 N( B
    尾递归优化可以提升效率(但Python不支持)# n5 e( Z# x2 x+ E% g5 k* m3 C; V9 Z

    , o# H+ l5 H' r% ]  ]( m 递归 vs 迭代
    . }/ v$ x; y3 C+ }( C5 n: s|          | 递归                          | 迭代               |
    " j. O  o% i$ J|----------|-----------------------------|------------------|
    - G% f* F- w& ^" ]- W: G8 L- \| 实现方式    | 函数自调用                        | 循环结构            |. G* h& m5 a: [2 t6 U& Y) A  v
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
      |/ E6 F# ]3 o& _/ x7 @| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    $ l# W' {& c/ n" ^| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 [; i" ~. r/ v0 V: B4 O8 A

    7 H9 [6 a- x2 c5 z 经典递归应用场景
    , ~  [6 E- t/ ^. D1. 文件系统遍历(目录树结构)
      f/ o4 N0 E: S( T* |" G7 ^+ ^2 D" t2. 快速排序/归并排序算法
    ) y5 e- Q. i  i: C& a* l" a3. 汉诺塔问题
    * h% y. c9 d* ~! [4. 二叉树遍历(前序/中序/后序)
    / W: |: A9 ~1 \% N, M* _( y5. 生成所有可能的组合(回溯算法)2 X) e6 {/ @8 _& i

    : b! r; x7 s/ _试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 22:40
  • 签到天数: 3176 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,2 F2 S/ _( s) B+ J# l" D" t2 ]
    我推理机的核心算法应该是二叉树遍历的变种。2 U6 t0 s/ C5 B5 _! L( D9 m* z: X4 n
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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/ m, o7 o; A* p  R, p4 G
    Key Idea of Recursion
    3 a* g3 |4 p2 E* Z0 n, I7 H: x( s$ v+ z8 k8 P1 F% |( {4 T4 p, M
    A recursive function solves a problem by:# N; t0 o8 S* C9 N$ i

    9 ~* E1 x6 n2 o. {0 I    Breaking the problem into smaller instances of the same problem.' L1 C# u9 j* U1 o6 M2 }8 g+ [

    4 q+ U3 K4 n( M" s6 P    Solving the smallest instance directly (base case).  K$ N7 ?$ [0 c6 p

    - ~( i% `4 E- q) R+ N! [    Combining the results of smaller instances to solve the larger problem.3 w- L$ H1 _" R/ t2 H' B  M& s
    - @4 X  n0 i& r9 B8 Y2 y
    Components of a Recursive Function
    . m( [5 S1 T# w/ f1 I6 D3 ~9 K" k# k9 g( s4 x) d
        Base Case:
    : F- x. u# k* n" W% \, X$ M3 F6 `# S! G7 O7 w
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.! I+ k/ d  X  U1 U0 ?3 f
    7 C0 j9 K: L: K$ H
            It acts as the stopping condition to prevent infinite recursion.
    5 C5 w$ q& N, k
    5 ~. @/ {2 h( Q1 m6 t5 ]  a7 T        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.* _* l) P5 ^7 k& u# r7 G/ r
    " ^' D$ R$ Z  g2 O( s. p& I
        Recursive Case:" ?, e7 p# N2 x

    9 ~& F7 O3 n- Z' B! {4 ?4 N        This is where the function calls itself with a smaller or simpler version of the problem.  O8 P7 Y/ b9 Y, j% X
    2 \, w" a( F" g8 p& {! H
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 k* R, [7 T9 B. c# f" v! S- J( t

    + T9 }, v6 ?" m2 }- x9 Y; |Example: Factorial Calculation
    ' T# h& ]2 Q5 a  j/ L* u
    ! q, t9 _2 j# V* p* B' n+ B% [) fThe 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:
    6 h) D3 b# x2 w6 x8 U- x! J9 W% p9 ^( u( V. [
        Base case: 0! = 1
    : @2 I$ A$ `6 a- s, s: T
    - J8 M6 Q; e* E' C5 ^' G    Recursive case: n! = n * (n-1)!
    " x9 \( S8 A! ?  F
    . @! u  I/ a$ ^) j. W. |Here’s how it looks in code (Python):* ]$ d! W4 J/ D  |8 M
    python
    % N0 v. {+ \7 C3 T& y8 L! ~' `  c" m
    2 `( ?, Q8 c$ }& j2 z  X! T5 e' F& ~, {& j/ m- {; v) B
    def factorial(n):" X9 W/ |6 d  v
        # Base case' J9 h1 d. {6 @; Q% u  h
        if n == 0:* }% w; h. ]# [  W
            return 1
    4 z' Y7 [/ Y+ |  a, J& T! u5 d    # Recursive case. \- x; ^" x) Y
        else:. W. |7 ~2 y0 f# a8 Z
            return n * factorial(n - 1)) M$ R6 g; ]; }) I* W0 ^4 y/ d6 J

    4 d# V* _" p! [$ z, Z1 q# Example usage3 A) w% n6 I% {
    print(factorial(5))  # Output: 120
    3 n4 T+ t, T% F" t
    9 S# h" w& L. B' @+ zHow Recursion Works* Y6 ~4 C/ t3 K3 I+ p
    " _: L$ x; }. S
        The function keeps calling itself with smaller inputs until it reaches the base case.2 a" W0 i+ `) l7 A
    . Z/ y+ v; _4 L
        Once the base case is reached, the function starts returning values back up the call stack./ k# p3 u8 I" l0 Q1 `8 @
    9 O% F6 g: o; R4 ?) Y6 u0 p/ F
        These returned values are combined to produce the final result.& d2 E- _$ Y! x" s3 c9 {5 ?8 c1 ~( @
    8 H7 w9 t1 I  o+ W" `" l
    For factorial(5):. G' N  k7 F' K' r0 h( N1 z  n

    4 B' V! F9 b& s8 y- V0 S6 G5 v& p! ~
    factorial(5) = 5 * factorial(4)
    3 O" ~0 X2 t! K9 n  Xfactorial(4) = 4 * factorial(3)
      \8 ?  h- V6 a9 Z/ m( z, lfactorial(3) = 3 * factorial(2)
    9 F$ H4 Z- t1 e' Gfactorial(2) = 2 * factorial(1)3 h  Q. F% b; x5 \3 y  }! m
    factorial(1) = 1 * factorial(0)0 ?0 s2 G4 J# [# k! s. E7 z
    factorial(0) = 1  # Base case# ]8 R& ~8 K& }9 h2 S/ a- ~$ H; Q9 _

    & T9 e8 m& t$ LThen, the results are combined:% e  e4 s$ z  u4 {
    0 ?3 e& r2 }& R" ~1 H& |

    - }# c( K' C5 D1 B0 ^6 Jfactorial(1) = 1 * 1 = 1
    ' e4 |# x' O4 n; G- yfactorial(2) = 2 * 1 = 2
    . f0 |  `7 k: N* s9 kfactorial(3) = 3 * 2 = 69 q; w/ O; d( T
    factorial(4) = 4 * 6 = 24
    ! g& B. c" F2 x( R: Zfactorial(5) = 5 * 24 = 120
    . z$ S, @8 y: d0 w# J! P  g
    6 l3 p5 ?  I. m. K# tAdvantages of Recursion
    : ]. k4 k$ X7 D1 _' J8 K" N; ^' W, V' L, B$ W
        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).
    ' T7 n4 x$ R0 D# L
    4 g0 E8 v3 I' A! W    Readability: Recursive code can be more readable and concise compared to iterative solutions./ v$ l* l6 y) J: j

    % B+ N: ]* i& D/ A* ?" u0 _Disadvantages of Recursion
    4 m/ o' S" d! w: ~3 l8 C/ g$ l4 \
        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.- j2 t) ?" h' o$ d

    ) {& z+ q8 y7 g( {    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    8 A0 N! j9 S+ V3 p' k6 u1 w* `2 \
    ; f% U, E( ^. yWhen to Use Recursion3 e! q" g" ?+ n% a
    / A3 i/ ?* \3 P
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ! A/ w  A5 `% X6 Y6 e
    9 {3 D2 [6 a9 r    Problems with a clear base case and recursive case.
    " L4 r4 E% m+ y6 m" k" I2 A# r8 m- C' d1 ^. C
    Example: Fibonacci Sequence9 b% Z3 n/ l) J3 \0 ^% Z( ~

    ' N( P7 `# d/ N- w) }' m( `' {& c6 P: HThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:* C: J' d+ q4 O" g' p
    ; u! k: A' W0 g! f6 |# j
        Base case: fib(0) = 0, fib(1) = 1
    % u! @8 r' ~+ u* A9 k* d
    1 U1 V5 `/ U( h2 {$ d% f- c, X: x! j" X    Recursive case: fib(n) = fib(n-1) + fib(n-2)6 p7 e' Y/ }# h3 ^7 F$ n
    - _# Z- A5 `' g! i8 C$ K  T
    python6 Z3 Z& x3 Z% n7 d2 f6 U: c5 w- O9 G7 B

    5 ~3 w$ \8 U+ K8 F# s+ g7 h5 Z: I1 c0 b$ ]% K  R3 [% {& b9 C
    def fibonacci(n):
    , j% O1 d/ Q9 w    # Base cases
    & C( a, f) K+ G8 o8 @5 j7 d    if n == 0:7 b. r* V  L, K: t$ Q/ r+ d+ c
            return 03 U7 D, K' r7 }1 C- ?
        elif n == 1:! O! g* w) v7 g& R# o9 Z" W
            return 1; y' D5 p0 C% |1 i
        # Recursive case
    + G1 Q, A, ]' I    else:. N) v7 @$ j: l$ J( Q1 X8 V; p! L: k
            return fibonacci(n - 1) + fibonacci(n - 2). B& H$ Y8 T2 U' H
    / l5 j5 _7 Z9 s" p% T) S
    # Example usage
    9 H: A6 h1 @  C5 ^" l  U/ Y8 @print(fibonacci(6))  # Output: 85 {$ |8 Y2 r" x8 U  ~# G
    6 @0 P( e8 l  R+ b* b, R8 y
    Tail Recursion
    " s* S9 D5 S) s5 j$ M0 C
    , T8 k8 q' D3 x/ }- r7 ]8 [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).1 t) b4 h1 T' u7 P1 j" F4 h

    2 ]+ u8 M# @! ZIn 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-18 13:05 , Processed in 0.060276 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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