设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 3 \5 v3 x1 a0 F& R  b6 }

    ' h5 o8 a& h$ Q" i# k解释的不错: Q3 O* t- {5 S

    , k: ~; K4 O: X递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    / U' j9 w/ K2 U8 {0 ^% M5 e* b% Z
    # k" J/ {0 r" ~3 e: f6 K 关键要素0 q* X: Z6 t; @  L
    1. **基线条件(Base Case)**4 K0 |: A/ O- L
       - 递归终止的条件,防止无限循环, N: O5 a, S; {% C
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1. x1 @! k+ `' J) d% g$ V

    ) o+ ?% |6 }6 c- s* @1 L/ l9 o7 w2. **递归条件(Recursive Case)**
    1 e& q! f$ a/ V4 J4 J# b* ?  B   - 将原问题分解为更小的子问题; C0 ~* F; G6 K% _
       - 例如:n! = n × (n-1)!
    7 f, s2 J1 U& b" z9 ^% ?  v8 V% P  \5 B. L6 u- ]
    经典示例:计算阶乘* D  I+ J0 ]8 J; C) C
    python1 W: @' Y  D/ n, Z+ c
    def factorial(n):& k% [* k& P6 L1 j" h
        if n == 0:        # 基线条件3 M! E* }/ s3 Q/ u! u, c
            return 1# N3 x3 M1 _0 l4 A+ G5 y
        else:             # 递归条件
    / D- u( `5 y6 P        return n * factorial(n-1)
    + p( o  m: \+ L2 r, m执行过程(以计算 3! 为例):& M. W0 o% u! B7 ]( k
    factorial(3)+ b1 P! V* W% k
    3 * factorial(2)7 A  H& O1 n# {, F9 O4 Z
    3 * (2 * factorial(1))
    / G- W/ f+ G* j3 * (2 * (1 * factorial(0)))
    4 h9 Y% E) {3 a2 k3 * (2 * (1 * 1)) = 63 `9 T5 r9 \3 W6 c+ p
    , }' w6 @+ z3 M1 _
    递归思维要点
    ! J6 F7 p# u: Q. m$ V1 \1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ( b0 |% Y' d9 h3 ~3 A% m6 v2. **栈结构**:每次调用都会创建新的栈帧(内存空间)  H* r& G0 S5 ?! @# @& h! Z$ I
    3. **递推过程**:不断向下分解问题(递)
    ) W& A/ Y1 a1 L6 U* W( X4. **回溯过程**:组合子问题结果返回(归)% {! n/ l9 c8 |( C- s: `
    / b3 m6 J8 J  R5 Q- S6 c9 i
    注意事项9 a+ \0 r# f8 [
    必须要有终止条件
    4 N; J% D( c. C- l. U/ M递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    , ~. g5 D. b! g+ p3 a某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ' c4 L: z* e/ k) l* a; k尾递归优化可以提升效率(但Python不支持)% e3 a$ A/ I4 |3 C0 y
    + R( P8 }+ W  |& m. Y0 K% U
    递归 vs 迭代; x# I( D) c# A: o3 K- B. X
    |          | 递归                          | 迭代               |
    5 Z9 M  G5 g% A- F/ R/ i|----------|-----------------------------|------------------|
    6 H- q5 m7 a* s# i# b" ?| 实现方式    | 函数自调用                        | 循环结构            |
    4 n# g# E. d( G4 S+ N5 \) ?| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 r+ u4 u& b, |% R& G1 R& s* W; c6 ], Y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |, ^9 A1 x- Z5 l: j
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" c. T4 c; l  L5 b8 G/ Y0 ]
    7 |$ N! ?0 z/ g4 o/ }5 i
    经典递归应用场景0 ]+ g+ Y; `4 C2 P' y( @  Q5 w
    1. 文件系统遍历(目录树结构)/ B8 c  l2 u0 j
    2. 快速排序/归并排序算法1 J4 F4 G" H) B& k7 k+ f. z7 e4 z
    3. 汉诺塔问题
    & v6 `4 d& A# X; o$ o4. 二叉树遍历(前序/中序/后序); w' J2 a( F8 H! V* ~% H
    5. 生成所有可能的组合(回溯算法)
    5 ~/ Q) k% ^/ `  C( }
    : r, L- z) ]" P4 o4 e8 ?' L5 t试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    5 小时前
  • 签到天数: 3106 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,& h" z" s5 D8 ^0 a
    我推理机的核心算法应该是二叉树遍历的变种。8 a  e0 v8 J3 o+ R! p
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ ~" e! _* J$ F( |
    Key Idea of Recursion
    0 G& b5 Z3 O3 v8 \& L- A2 @. K! q) k+ i2 [
    A recursive function solves a problem by:% ~" E6 K3 F: D

    / X. s$ ]. s6 P7 p% i    Breaking the problem into smaller instances of the same problem.2 _- ~) m! K* n* T7 e
      y, U4 N, w5 t7 h% H! b4 R7 G
        Solving the smallest instance directly (base case).+ I' ~* ~6 x6 Q( T
    , @4 {  N: T# U
        Combining the results of smaller instances to solve the larger problem.
    0 W' @: G' s. o$ ~& p
    ; J/ i+ r; E5 a( QComponents of a Recursive Function7 o1 t6 u' g8 W; c& \* t
    , b4 o+ ~4 C* Z' W- h) U2 Z' T. X
        Base Case:
    : E' p7 W4 `$ {* f' @# a  ^
    % I9 t7 c2 K2 z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 L1 @# Q8 f2 d* v$ t! U, k2 ?+ _
            It acts as the stopping condition to prevent infinite recursion.
    1 {2 C8 o! f1 r! Q# i) T9 W$ M. Q3 Z+ h/ H
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' R' c5 X& M3 W! W3 p2 Z
    & O- J* C& K2 o( [, V4 O  p4 p    Recursive Case:
    7 }9 J2 U2 t1 Y5 x# K. [8 t; @; M! F2 X. L
            This is where the function calls itself with a smaller or simpler version of the problem.- Z# B  u" N# P% Q% s- P
    : y  k: ?7 h4 k, c  G( X
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    . Q7 K, [4 p. g; l. A' z! @8 J! s$ s7 j
    Example: Factorial Calculation% U' ]' i" e' G
    , B2 c' H. f1 [# D2 C2 k
    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:, j) Y( H9 ]( U8 Z

    ) K6 N+ U0 s  d/ [, M    Base case: 0! = 1
    ; w; @# m: t8 S5 W5 T1 P
    + ]* ?! I' d$ v: g* P    Recursive case: n! = n * (n-1)!
    / E4 P3 {/ V- ?7 t$ S: y0 r2 t& i# Q: C# N5 t# e" T6 L6 [3 M
    Here’s how it looks in code (Python):
    * z% m2 i$ i9 p9 R/ S' L- x! n# epython# ]  ^! P8 i! d# I- M& s/ g
    6 C8 F0 {% @8 T) v4 K* B

    5 e3 o& Q0 }/ M2 Q* ddef factorial(n):
    + d$ [1 i& t# S9 I    # Base case6 G4 P7 Q2 t, \3 s1 z, a
        if n == 0:/ u: [+ f! Y4 A# v1 L* L- {# D
            return 1
    7 x1 K! i5 y* V: L    # Recursive case
    5 m4 {( A: z: X  b& E3 B    else:$ B$ V; V( |# S: \( M* f
            return n * factorial(n - 1)2 e8 X3 h# k8 m$ Z0 o6 K
    # [4 ?, _. n) y' |& n; u; ]& O/ t4 C$ `
    # Example usage
    # b) X+ v" D# |% E7 E5 D( Vprint(factorial(5))  # Output: 120
    7 W* q5 @7 v$ a3 d: F
    2 `3 \8 S6 Q8 }1 ^2 qHow Recursion Works; |. S+ F1 ]; p3 ?; g

    7 f3 @0 ]7 I6 E& I# G3 ?. c" [    The function keeps calling itself with smaller inputs until it reaches the base case." }: e/ a) _; }2 b
    1 ^: p! O7 }$ w' s5 [
        Once the base case is reached, the function starts returning values back up the call stack.7 i$ M. i0 y( T8 w4 |" Q
    & O6 B) m. `: V6 }' W
        These returned values are combined to produce the final result.
    ! m4 H) e! y! Z7 a& y$ V0 o, f+ D& ]7 j- h; J9 P4 R
    For factorial(5):
    ) H& D1 r) }. j  c2 m3 U% J/ v
    & X: k8 o3 v. s! `% n. |/ n( ]+ Z& |: b/ \- v- b0 I- p2 x3 p* J& f& X8 Z
    factorial(5) = 5 * factorial(4)
    6 M+ m( H' b7 }2 u; \" e$ Tfactorial(4) = 4 * factorial(3)& U9 O+ A9 y! {# G, k# `# a
    factorial(3) = 3 * factorial(2)1 j* n4 d3 e1 V9 c( w% h
    factorial(2) = 2 * factorial(1)5 H& l! w8 F  l& ~. I8 p: O
    factorial(1) = 1 * factorial(0)4 S8 x2 X! O. ]% O6 z  b
    factorial(0) = 1  # Base case
    / `" A; Z' n4 x) m+ K# @# @% e, Z" L, u7 ^) p0 H8 ?
    Then, the results are combined:4 c! E* N% e) I% ?6 E9 n: T& W
    / D- D+ x; Z% {' a+ a6 F6 y

    1 {2 T2 Y. I  @' Z: z. Pfactorial(1) = 1 * 1 = 14 i8 W4 R" w: J
    factorial(2) = 2 * 1 = 2" q( }$ n7 f$ Y" f' l4 t
    factorial(3) = 3 * 2 = 6
    : {9 O$ o" C  A- {, _9 K+ O: |factorial(4) = 4 * 6 = 24% S! [' v9 E9 r% _0 h
    factorial(5) = 5 * 24 = 120
    " Z8 z4 E7 }1 q
    3 S( b9 d9 [# kAdvantages of Recursion
    , s4 X8 n/ v5 `- Q% t
    - A0 \& @- A# n    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).4 z7 ]9 V4 h9 _1 q3 v# S& _* W+ N
    9 E# P" Q/ W* R3 K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.  d3 K* r1 x; m' c/ U! R' G7 c" u
    / v. X4 _# {! u4 a/ w; ~* ^6 P
    Disadvantages of Recursion
    - n  H% B  |4 m4 ?+ L' b5 T  s& m* o5 m
        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.3 j; i9 Q2 R/ n; I3 `
    ( ]* d- b* c; i& R+ H
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    % _$ S9 t( n. W* i- O8 K# D" I& s0 [' C& r! C0 ?! z
    When to Use Recursion
    $ `+ c% k* q! M2 z2 d; t9 f6 _$ U* @0 t5 C  Z4 f
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) Z' g3 Z8 N; `/ ?
    + k, t& X/ T$ ~) I! E& v6 P4 k
        Problems with a clear base case and recursive case./ i7 A  p( L* q8 _$ V3 ?) i

    4 h! o8 ~. s2 X# Y0 u' f5 F& mExample: Fibonacci Sequence
    % a# G/ R; f3 W, V. G; m
    : U# Y3 Q' o6 {: g: X* r, \  hThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 x1 d0 m. L% p" d, D5 e
    : s2 R3 |+ x$ G0 c( J" T: Y2 ^0 _
        Base case: fib(0) = 0, fib(1) = 1
    2 M" K8 c% s0 [1 i- \5 }( Z6 o) [. `0 `5 p* `
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ; c1 T' R" i" N
    " s- V" }- p6 v9 O4 L6 x# C- W2 N! X2 S0 rpython  G; n$ h. I. R% o

    & i( Z$ D9 l2 b/ U0 v
    7 B& b! [; y/ E/ d7 s2 idef fibonacci(n):# \" r9 E: v! G+ h) Q
        # Base cases
    2 \6 i1 e- n5 k( v    if n == 0:* {+ [( Y6 p. U
            return 0
      J0 C5 R2 A# n& f. g2 b    elif n == 1:  U. H' ~  ?3 Z2 v) p, N" n* H6 z% G
            return 16 S" t$ L, s" k1 [2 z- e6 u
        # Recursive case
    ( Z. }; k3 R% ]$ L& ]7 u    else:% g8 `# e1 J& s
            return fibonacci(n - 1) + fibonacci(n - 2)3 z0 z$ E: e5 \3 h

    4 Z4 D2 n2 I. W- |# Example usage
    * X6 |# W5 I7 B' O2 Iprint(fibonacci(6))  # Output: 8
    + x- O! p& K2 n1 s8 }2 Z
    ' J9 S* K' i' K/ e. bTail Recursion5 e$ V  V# T* z
    & Q5 w, b5 @9 W+ ]& ?
    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).& q2 E6 |# n7 L" J) U7 O+ B' j* L5 e
    ) {* V  v2 [9 R
    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-12-2 13:15 , Processed in 0.033223 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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