设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , ~3 c! I0 e* }8 T4 X, |  H

    , [$ b/ W3 n/ Z$ [& E8 t, V解释的不错
    # [4 c  |7 K( M) v1 ?, Z+ Z1 b: q( l: z) S6 p
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ' ?" E, ?2 p2 Z% H- V2 E' E0 ^1 P. v
    关键要素
    2 y  H5 `4 K/ j; t1 b, p1. **基线条件(Base Case)**1 ]  Q# h$ V5 c' l( b, h! R% b& j
       - 递归终止的条件,防止无限循环4 T" O5 `9 l( T) @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% [; o, P# f- `- T

    ; \( N9 {2 [; S) \  M2. **递归条件(Recursive Case)**8 E" x: i' F$ E
       - 将原问题分解为更小的子问题
    , v6 \& Z8 W0 o. L, k7 a   - 例如:n! = n × (n-1)!! s* S  s6 p# z

    , L- `4 R! |# t( d 经典示例:计算阶乘  Z6 _9 g+ q# b: `6 m+ ^/ W- ]
    python
    6 H+ T, a* L9 @4 edef factorial(n):8 p# B8 }1 w  ?0 w9 f
        if n == 0:        # 基线条件4 D5 J. k+ V6 S4 q* p) Y( ]& \
            return 11 K* E' Z+ U& w8 d/ j- s
        else:             # 递归条件' F% c' B* j8 ^* F
            return n * factorial(n-1)( h& d' U% y5 t( E
    执行过程(以计算 3! 为例):% |5 P; E' [4 Y  C
    factorial(3): p  M: t' z0 L$ a$ B- u
    3 * factorial(2)
    ' L- X4 {( ]- j7 F* W8 a3 * (2 * factorial(1))$ L8 {4 X/ N4 Y# n' t: k
    3 * (2 * (1 * factorial(0)))
    6 |- Z6 W+ ?4 B: g( I3 Q3 * (2 * (1 * 1)) = 6
      P* b. i# v, B3 P' W$ Q- T6 x8 n1 p- J6 Y
    递归思维要点* H2 Y$ w- W# g# _& e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑' s! S2 g  f: y: [
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 Y8 z( Y4 j' [, K+ n9 E! x3. **递推过程**:不断向下分解问题(递)- C/ D8 _  F$ \7 d3 M. S
    4. **回溯过程**:组合子问题结果返回(归)! N2 L  k. p7 F/ e# S4 e

    " w' v, B/ A( |' U: G" F 注意事项5 H/ M" }8 y% z& r) m9 K0 Q
    必须要有终止条件+ p- A6 X- N- _) ?: X
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( L) S: q# W. V/ K5 R9 E* X0 c1 \某些问题用递归更直观(如树遍历),但效率可能不如迭代$ m: q7 d! G4 B. S1 c( P* x
    尾递归优化可以提升效率(但Python不支持)7 g4 Y" \, F0 o, C( s# i/ b. A" o
    1 H: {, D: H- `& ]6 r8 c4 Q# R; a& I4 v
    递归 vs 迭代, |7 ^" V9 z+ f8 I7 [* d2 F
    |          | 递归                          | 迭代               |
    / s8 u" n. C3 T5 q% Y|----------|-----------------------------|------------------|8 Q( H8 m! E$ W, u3 D0 H
    | 实现方式    | 函数自调用                        | 循环结构            |
    & j' C8 E* a  u9 B) E( }| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / v2 a5 O6 W9 e& u/ S8 G2 v- K$ E| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    . ?  u4 E9 ?2 V, n$ I1 w" n| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 P$ Y1 H- F# i0 B
    6 G) Y: m! Q: |! a9 R- a! w
    经典递归应用场景7 Y7 N3 X4 ~0 W8 W* \* O# \
    1. 文件系统遍历(目录树结构)3 @- z( z! T4 ]% t
    2. 快速排序/归并排序算法: K' O( e8 S- y5 D% m3 X* L2 J/ V
    3. 汉诺塔问题4 P3 I3 L  P4 m  h6 g/ _6 z. d3 X
    4. 二叉树遍历(前序/中序/后序)
    ! `) y3 }4 |" O; W5. 生成所有可能的组合(回溯算法)" ^. Z6 z! `1 R& P- n4 o
    ; v, Y  F# Y0 M, ?3 b# v. @" V% R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 07:37
  • 签到天数: 3172 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,8 Q4 }. Z2 n. p' V9 ?8 G( A
    我推理机的核心算法应该是二叉树遍历的变种。+ h- \% C9 m4 j; 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:6 F/ o% L2 R: v6 ]7 l; k2 a
    Key Idea of Recursion
    7 _/ C/ Y+ @, r  C- @' Y* [
    - }/ @0 R% ^+ B5 bA recursive function solves a problem by:
    " U0 d; g$ u7 m, y8 c# g$ D! X' I; v" {9 o# N) v1 V: K4 h( Q* `
        Breaking the problem into smaller instances of the same problem.
    # q" A0 G& a9 ~6 w! l
    ' W; t- W$ q- D5 |    Solving the smallest instance directly (base case).. f5 v# A/ }2 F0 e3 I1 a! j
    - O+ E; K3 k) R8 G, l. W
        Combining the results of smaller instances to solve the larger problem.# l* g5 a- G8 ]' S! K0 C

    4 y' E- S( n5 w, F: N7 KComponents of a Recursive Function9 u0 }* ]) e% G2 s
    5 C3 H. n1 s5 e, Y4 \
        Base Case:
    % `" r2 q! q" y1 P" v' X8 c0 H" W! U* y
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& L% Q8 D9 i/ Z- H' @

    ' t! v$ a% y5 I+ _. _        It acts as the stopping condition to prevent infinite recursion.: a+ ~& [$ u; w' e2 a; |

    8 v3 h+ J+ }7 ^% r! x        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# ^4 }5 k' S+ o5 O, G6 {
    ) u" |$ v4 x* W$ E; E
        Recursive Case:" L) n/ Z6 K* ~- E+ A

    " U1 ], A' P) s. _: \: _        This is where the function calls itself with a smaller or simpler version of the problem.7 d- W" Q2 L; E, b; ^
    0 B# R% N- g" j. ~2 {. E
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    0 D2 }" |/ s# l, J; r, d
    ( p. a1 I) k6 O3 B9 x* ?# g' OExample: Factorial Calculation
    3 L! Q" v' ?3 c6 s
    - J: ?4 u3 C# ], WThe 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:
    9 u4 I0 `/ Y3 ^* X9 `7 i5 T
    + x1 i1 W5 w$ m6 X/ ?- l7 Q* {# o6 q    Base case: 0! = 15 V' q- f# t: j$ ?: G& B
    * i( i' K# w& }. A! X* d5 Y3 f2 h
        Recursive case: n! = n * (n-1)!
    6 |% x2 j: s! R8 A7 P. `
    2 e8 P0 h# J* \. wHere’s how it looks in code (Python):) `) D7 q% P% s2 s; t; J
    python
    6 Y, f8 f2 Z7 p" y: P" k
    * I  ]& o. g. L1 a5 i
    2 ~* k2 B: n. H- P  ?4 j% Ndef factorial(n):
    2 P& L3 H# H2 Y- ]8 N" c    # Base case) ]6 |- M8 U3 z( j, q* G
        if n == 0:8 w$ E. h2 K1 h- R- l
            return 1
    - X# G! v) O: H7 l  Q8 c  p' y    # Recursive case
    ; M3 Q( o& _$ C    else:
    ' e$ u- S7 S2 K9 E! R4 a        return n * factorial(n - 1)
    3 ]7 D' i) g' z- w/ e
    & n/ T7 O" T' x" ]; S! f# Example usage5 G. N, I: j7 D# I. G; D+ U9 V
    print(factorial(5))  # Output: 120
    1 A3 u; D9 y/ @- o$ a  p2 i& V
    8 v8 |; G- A* ~( BHow Recursion Works
    1 N; Q. C" ^# D+ n8 f4 b! U; J; ?& o% J% |6 w
        The function keeps calling itself with smaller inputs until it reaches the base case.' e; j; R3 _, M% u7 @5 F( T) b, w
    , J# m3 b* F" ?" v% v( w
        Once the base case is reached, the function starts returning values back up the call stack.
    ( g$ L, A" N$ \1 y- p0 M
    $ y' K2 J+ {& `  a, R8 _    These returned values are combined to produce the final result.! ^* U, S2 p. m

    ! {9 H4 f+ m8 G3 L6 v+ YFor factorial(5):, j: M7 K/ [' @6 `2 m. j6 w$ a& ~

    " x/ `2 q. B/ e+ @2 B: H/ h9 d" f
    ) f+ k: R$ A! p! j/ c9 vfactorial(5) = 5 * factorial(4)7 b" t( y/ V: a- K" W. m. V
    factorial(4) = 4 * factorial(3)
    ' }) C1 F% u9 Q9 {& Nfactorial(3) = 3 * factorial(2)
    ! C" g7 N0 [/ z7 q% [5 P1 xfactorial(2) = 2 * factorial(1)
    + _3 B* ?2 a2 R' f$ ], ?. ifactorial(1) = 1 * factorial(0)
    ! z$ @# i9 A  z  A( y" R9 ^factorial(0) = 1  # Base case4 ~* w7 v3 M5 y% R0 y

    , s1 K: J8 S2 N3 P$ \7 BThen, the results are combined:
    : \* \& B1 \' J7 t  G4 m" \+ t! o) T7 O% C/ b! N" ^

    4 ?" r% x$ X; z- w7 r4 xfactorial(1) = 1 * 1 = 1& k4 |7 ~4 ], a4 g! Z  l( c
    factorial(2) = 2 * 1 = 2
    1 c: M9 }4 L8 e: B2 M/ X7 z8 R# @. Qfactorial(3) = 3 * 2 = 67 {8 N  ^+ X, k5 w, q! p
    factorial(4) = 4 * 6 = 24
    ! F; n  U" K5 i9 a6 g, nfactorial(5) = 5 * 24 = 120
    3 G$ _, A( D4 T( N; `$ k% J
    + M" [7 \. x- |7 |% EAdvantages of Recursion3 f/ _) A9 R- V+ Z* r7 W1 {
    * f- a2 E. z7 N$ Q: W9 B2 O& f
        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! Q, E1 _+ F! ]! w- s' e6 s
    , P0 c4 K0 A  l  `1 \& P
        Readability: Recursive code can be more readable and concise compared to iterative solutions.' \  R6 H- q" X% F* P6 {

    & l. h+ P  f& X; R* p4 g  v1 RDisadvantages of Recursion3 |$ O* E* Z0 [" |5 \
    1 z, b7 ^0 a8 n( y
        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.
    % a0 q4 q# i% p! c. g% b1 u
    6 \* S/ P' o( x    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + ]- l& Z0 A8 r. _$ K7 Z
    2 N# o$ C+ [/ l: i5 N0 V* iWhen to Use Recursion
    ( ^. l5 f" D) T0 s, d# K- {9 N4 r& a0 _5 p
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).5 s$ \2 u; y: [" R# s9 R, g1 D
    5 B! Y6 [1 o6 g; b7 u7 [2 i+ n+ a
        Problems with a clear base case and recursive case.
    6 F5 R. i2 `  Y3 ~  s' Q2 n+ N4 J7 J1 H  O( T6 }
    Example: Fibonacci Sequence, @7 K8 G- ~& i* T* X; ^# h8 Y
    # E" \, L4 \# N  l0 H/ q) t
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - w( [) G/ T0 O- j
    / F9 \- s+ ]5 o4 c( E    Base case: fib(0) = 0, fib(1) = 1+ A2 `4 ^0 ?) Z3 G
    # m3 ~5 J5 Y# y, ~6 Q7 W/ V
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 H% I9 o  e, s% ~! ]8 }+ M: i& R# @, y* U' w4 k* ]" T& b
    python
      v! t6 v2 j3 `- M( o. L- ^- q$ C4 t6 Y

    ! v. L7 Q6 D: S$ @! Bdef fibonacci(n):
    - ?+ n1 H5 @  W    # Base cases5 \& C6 _+ b, ~$ l: ~$ V, y+ G6 n
        if n == 0:
    . q; u6 {0 |! W+ r. Q        return 0" P# l- I( o) l* S3 J  y$ u4 `
        elif n == 1:
    . Q" z$ K  j2 v& j( ~" a. A        return 1
    5 S. E& q9 e% z( f2 j* ?    # Recursive case& F" {' Y$ Z" g( r' S! e4 m- M
        else:) ?0 ~& K& N' F
            return fibonacci(n - 1) + fibonacci(n - 2)
    8 |; [0 e; G( p0 D4 |" M; z  g, i3 R( C# y
    # Example usage7 K" Y' ~% \, j
    print(fibonacci(6))  # Output: 82 S' ?# r! J2 I( n2 v

    , R0 z% w& k' \: U# E9 ATail Recursion
    9 I" R+ C0 g) f5 i2 k  a' t
    7 b8 F7 t5 K# w' xTail 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 I' R; G; s7 _% N- D9 v
    - s% J: G. J$ J# \5 u3 sIn 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-14 02:02 , Processed in 0.054905 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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