设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 y% }: x# Y5 B9 v
    3 f, e/ z2 C$ ]. }; T% O0 }解释的不错
    . i: C* B. t9 i; E! T6 j* M+ D7 |. Y  m  b
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    2 c2 S3 |$ q( z  w( d
    3 D9 J* L8 Y' p; [6 \# u! i; S 关键要素! b5 |8 d1 f/ N6 ^# V
    1. **基线条件(Base Case)**; g6 q# V  S& r7 q3 c* X5 |
       - 递归终止的条件,防止无限循环) g( J: t$ g6 E
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1, b9 _* ^0 w! B3 ]9 p4 a

    4 Y! H* F" x3 ?* G) \2. **递归条件(Recursive Case)**( B+ c! F3 R5 }3 T6 a
       - 将原问题分解为更小的子问题
    ) f3 ]1 m) c# V9 ^4 t   - 例如:n! = n × (n-1)!4 {6 K3 B& _" Y7 h* }9 X
    : n5 C1 K# Z- Q/ @# u5 B. s2 y
    经典示例:计算阶乘3 B1 u( s; s, F+ o( {
    python" T+ |) T% i8 R% `; d, x! b
    def factorial(n):
    ! y; H% ]2 U- p. v. \    if n == 0:        # 基线条件
    ; }& W1 n8 C2 x5 J: B        return 18 f  s4 i4 V# Q8 ~7 y2 ~
        else:             # 递归条件
    " y7 W8 ~( J* h, p        return n * factorial(n-1)1 U. ~' V) F+ P1 F5 Q! ^
    执行过程(以计算 3! 为例):
    5 [; |% i, Z( y) _7 o% mfactorial(3). f6 L$ ~7 |* l
    3 * factorial(2)
    # o- v8 x7 v/ t0 g" j3 * (2 * factorial(1))$ \0 S+ h0 `% u, N- d
    3 * (2 * (1 * factorial(0)))
    ( s1 i% X3 M' \! j' _% E0 |8 o3 * (2 * (1 * 1)) = 6) ]: t4 t  v- p5 Z
    % g$ E5 @. {- S9 g' ^% C; y
    递归思维要点% F1 o3 k  @/ D
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* w/ v/ \$ x- X' o; b# |: s1 ]: Z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ' ?; n2 ^! v4 H1 x+ h3. **递推过程**:不断向下分解问题(递)
      {) m" S( D3 g) j4. **回溯过程**:组合子问题结果返回(归)3 ?0 {" q" E  `* q1 N. f

    ) p# n0 r9 z/ i0 l5 A% m! k 注意事项. g! f  g7 g$ E  Y$ k8 Z
    必须要有终止条件  @% E# h) Z8 |. z5 {: @
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 U/ _0 m9 I7 G6 ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " v2 a6 |/ G" c尾递归优化可以提升效率(但Python不支持)
    ; q7 O  o4 C5 B( u
    4 E8 d6 D6 }3 z' }; F- a& y0 m 递归 vs 迭代
    1 C& V/ _' I' a+ d" |# S3 P3 X|          | 递归                          | 迭代               |
    ; ~9 b7 \, l" o1 z|----------|-----------------------------|------------------|  [# J$ n5 o2 a9 C$ n! S
    | 实现方式    | 函数自调用                        | 循环结构            |
    - d) g3 t. d- M% z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    - \3 X$ ~. u! A| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    - k: D3 \1 e0 R" d+ S| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    : u; Y  y6 p2 g! k0 Q: w+ ?. X5 }) G  W
    经典递归应用场景# G" l$ `# g8 |3 A  r& O1 A8 N
    1. 文件系统遍历(目录树结构)" r6 r* I0 L: d' U# n5 O7 ~
    2. 快速排序/归并排序算法. Q) n& h8 q2 _9 v5 _- Y
    3. 汉诺塔问题
    3 Z9 u0 F+ K$ M! }+ z/ ~* n4. 二叉树遍历(前序/中序/后序)$ \6 `' S& l$ e! H7 D( X
    5. 生成所有可能的组合(回溯算法)
    ) r' H* B2 T6 C; q  w& K5 x: C2 H# j- {" H: t/ J: v+ H& ~9 z
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 06:52
  • 签到天数: 3202 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) Z/ d! b$ m" L/ A: a3 d/ G
    我推理机的核心算法应该是二叉树遍历的变种。0 v1 @* U' `7 [, F/ k/ f8 P* K
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    , @+ y! _" p# B7 B* zKey Idea of Recursion
    ; L* V# O8 C8 A6 n  H  U2 g- b: }/ E6 E( l# O
    A recursive function solves a problem by:
    ' }& T- L4 l; g4 B% F1 B
    " h* B3 J, _6 K' u; ~1 y    Breaking the problem into smaller instances of the same problem.
    8 g$ g5 D7 Q+ b" r& y" e( Z% Z9 s# ]$ v, U# J
        Solving the smallest instance directly (base case).
    8 a9 x4 H9 b2 O5 j: w: l( L: s: q9 u% G
        Combining the results of smaller instances to solve the larger problem.
    & i& n3 k) _9 x: ?
    . _: u! M, m: ^2 W# i# N0 CComponents of a Recursive Function
    , ~1 T! [/ K3 A" o' ^9 T" T
    # `0 y9 H& |8 N" y  L/ u    Base Case:. S) ]4 D8 ^+ X% t: W

    4 z* A+ p; N* T0 _" j% t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ) G1 k* F9 H) _9 h6 L. D: {  }7 M% R$ L2 Z8 n0 r4 H5 S, B
            It acts as the stopping condition to prevent infinite recursion.
    9 N$ i& [3 _3 r% q" O
    * {$ u/ s' \) j3 g. T- z! Q1 ^        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 {1 r/ x! D) H+ y; v3 k1 W+ {* Z3 f- l
        Recursive Case:
    . Y+ W* G( y$ O
    : Z( f/ B$ B" W! s! C- C- d        This is where the function calls itself with a smaller or simpler version of the problem.
    7 S, C! D' a2 X; k& _4 B0 t3 ?* d  C) D) |" N) F
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).3 f, ~1 L* m2 G& p" i4 r

    1 G1 D+ G/ E6 n7 c1 m; N3 k7 DExample: Factorial Calculation
    ' g, O: R" l  l, r& g% b7 D" n' e- f4 `$ h; g5 M9 L# A+ g9 W! P
    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:& K' B0 m9 O# H: c

    : P, T9 {1 e, [' Z    Base case: 0! = 1
    6 i! S/ t: A: R3 f1 }, i7 e$ l- j/ u+ i0 K- |' T
        Recursive case: n! = n * (n-1)!/ t9 e2 ~$ u1 T7 J7 O3 M3 s

    8 L! ?9 i1 N4 P( K/ k& Q. k9 s4 rHere’s how it looks in code (Python):9 h  V7 P& o' ?; |
    python0 I0 {# Z6 V% {2 n6 _- Z
    0 U$ J* v, J, A# U$ X1 E; _
    6 E/ x( C# {+ |6 i& M
    def factorial(n):
    # N- b2 [' Q7 _0 _    # Base case
    " ]% |6 \; [! O; n( D( e. `- b, ~    if n == 0:1 k: O1 p" B: b3 m. [' r
            return 1) Z( z6 [3 t3 f% }
        # Recursive case
    / }$ C9 h* u! T# K9 W. w    else:
    0 n6 a1 h* c2 {        return n * factorial(n - 1)4 g* y: \) h+ E/ R* ]' F" C6 p

    * `2 j( W5 W+ b1 [  c9 m5 v+ G: ]# Example usage
    / \! g: U& E0 K% p* kprint(factorial(5))  # Output: 1206 l0 O7 p* K- G7 L6 x+ n

    # q" |: g! h" V/ [3 `+ QHow Recursion Works! u$ P. i4 j+ h# s3 `

    " ]- n/ H# r' Q; b+ N- ]    The function keeps calling itself with smaller inputs until it reaches the base case.
    % X7 k% b9 N1 g3 b! h3 ~0 v# {$ m" W
        Once the base case is reached, the function starts returning values back up the call stack.
    ) \/ ~! @# h7 `+ d/ f
    . N  X9 J9 }% Z6 m0 o% K$ L9 b    These returned values are combined to produce the final result.
    * N& _; B' v) [: I2 P
    - [& j& ]6 q& TFor factorial(5):; t6 \% {% _& C+ @$ @8 ~3 r$ b. |

    0 I! Y" M! O& D' s5 W5 e1 e
    ; U4 k' C) e7 m! N$ {5 x) l, nfactorial(5) = 5 * factorial(4)
      q* P1 m1 A  E1 w6 L/ ^factorial(4) = 4 * factorial(3)
    , N; j5 D2 h# U8 P% G2 Efactorial(3) = 3 * factorial(2)
    6 X$ g2 g; R' [, Jfactorial(2) = 2 * factorial(1)  y- A  t% ?: c% \* I
    factorial(1) = 1 * factorial(0)7 R$ U5 V- z  t5 v$ ]2 q% ?/ j
    factorial(0) = 1  # Base case
    6 i( {5 T5 u, b1 m6 ?/ R; z- |
    / j$ K( A5 b- B9 F+ B4 c' UThen, the results are combined:' R7 J1 ~" m2 c7 B
    5 Y+ M$ B1 H1 a1 ?& C2 l3 X( Y: t/ N
      `! [- s- W' V( n/ A+ ?1 l
    factorial(1) = 1 * 1 = 1. i* W6 y) L5 z- q0 `
    factorial(2) = 2 * 1 = 2+ ~2 Z, h! l! s
    factorial(3) = 3 * 2 = 64 ^* f1 e' O9 V: X# f/ s
    factorial(4) = 4 * 6 = 24
    3 j. m* }5 a' j! `! P2 e/ ofactorial(5) = 5 * 24 = 1206 ]0 R. F9 N& i( b* G9 \$ m7 i' \

    $ C1 D# A6 F, `/ h! F2 B8 J  \. cAdvantages of Recursion; @/ I8 N3 I) S
    $ X* W9 E5 K: K5 Z
        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).' x! j& M/ r" U+ K
    1 G2 M# g6 X) }
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; Z9 t3 V' q1 {0 a  i
    5 n1 }0 h( [# B+ P9 ?Disadvantages of Recursion: [( w2 f" }5 C: C, `# Y
    ; f' e! ]/ z% e5 q8 @. T; B$ b
        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.
    # I3 Y& W/ O. Q5 w; e
    5 ?! K; a. {/ |! i. c& B8 \1 o    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 x" ?! R% ^! [3 t$ t

    1 u5 j) z: O1 u6 {When to Use Recursion
    & `9 ^. ~, B( H4 E$ _% w4 x: F5 O: k* K& {3 ?
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).* z" ?" h# ~6 t/ W: ^) u
    4 i9 w/ B" G% _1 J- g2 s) U
        Problems with a clear base case and recursive case.. e/ Q/ B8 s5 ]0 R, }$ J
    1 r" V) x% [3 \1 |- e) d
    Example: Fibonacci Sequence! v3 f$ J% {9 C3 t6 |1 z

    * C+ Q  T; u; {2 ?0 GThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    & C- S& L3 r, e/ q* Y0 s5 O0 U- u! Z3 M! J' n( r
        Base case: fib(0) = 0, fib(1) = 1
    % _: e) j+ k! E, o
    9 {1 S, _% }% g+ e) q( ^    Recursive case: fib(n) = fib(n-1) + fib(n-2)5 ^; E1 M. y0 P% w* k
    6 U" |  m5 K# u  i
    python
    * d& O$ S  E* g7 d
    * v) ]) G# z7 ?8 o3 N0 H
    5 f7 [9 `9 H3 tdef fibonacci(n):
    $ R* r! ^( `  l5 b    # Base cases
    . y) C, A  L9 X  A+ z8 a  Y    if n == 0:
    ! Y( ~2 H; g* P' {( f( `, d        return 0* w4 \1 }3 ^0 p2 N% h
        elif n == 1:
    / e6 G/ p  O2 _/ F& ^        return 14 o* {' v- q: C$ t: G. P
        # Recursive case" e( z& r! r9 [$ O+ C
        else:
    " u: D3 e7 i9 [8 d        return fibonacci(n - 1) + fibonacci(n - 2)4 D% W3 z7 a: M2 p' W! {- N
    % f: q5 o  Y) V4 J( f
    # Example usage
    3 @2 Z$ E2 p( B& c& x& kprint(fibonacci(6))  # Output: 8
    ; K  Q1 O- R9 G2 I$ Z+ v6 l5 B4 H3 k) ~: h6 D  C6 z
    Tail Recursion# q9 v9 U" w0 {
    . Q+ r1 k) B6 M, r  C) N( h, ^4 b+ B
    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).
    " S3 F* X1 R; i" o: K. Q
    : b+ B5 j: V8 v) uIn 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-16 06:10 , Processed in 0.067730 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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