设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ) ~$ g( x* j) n* ^2 K5 e/ h8 m( H/ T9 n3 b9 ^/ c* Y, n
    解释的不错
    - D& ^' [5 D0 p) P3 |2 f5 d2 X- D/ _& |/ `8 j+ f1 x" v
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, W! L$ c5 O# v

    0 p1 ~* Q1 e' W0 Z6 r+ l6 H3 u 关键要素
    $ q6 T/ }2 s7 ^6 T/ W% m$ |0 Q; E; Q: L1. **基线条件(Base Case)**
    * `9 r+ d( B* X# W/ X! n- w   - 递归终止的条件,防止无限循环
    : {" R7 ^& r7 \% j) |0 s   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
      ^" B/ b: D4 w' Y: O. J0 V8 C* P+ P9 U
    2. **递归条件(Recursive Case)**$ e: ^* d8 f5 Q& G
       - 将原问题分解为更小的子问题
    - D- W8 k# M+ b" w, T+ p0 [4 ]   - 例如:n! = n × (n-1)!* H2 B3 p' e' Y8 j. I/ T, w: k
    3 _( J- i/ T8 V6 u# a8 ?
    经典示例:计算阶乘: h# N, b, D! E  `$ @( K
    python
    ( a- T9 a; Z( m7 M7 U5 K3 p" F' edef factorial(n):
    ! a' P, r; k' f! ?% ?    if n == 0:        # 基线条件" m  _0 z* w( w4 e, t) c
            return 1+ t/ U4 `" v5 _0 T* z, ~/ \
        else:             # 递归条件5 _  h$ U$ }# R+ R% D% z# X1 B
            return n * factorial(n-1)
    & K3 J9 U% m- @( O7 @8 m% F9 ?7 T执行过程(以计算 3! 为例):
    6 j" c: O7 ]( M$ O8 j9 nfactorial(3)6 S9 o4 O  ?+ C. t$ ~
    3 * factorial(2). U& |2 W, _+ V  ?6 g& V
    3 * (2 * factorial(1)). Y3 x5 _* Q6 c6 x: L
    3 * (2 * (1 * factorial(0)))
    ) H2 T0 I( u7 y3 Q+ Z3 * (2 * (1 * 1)) = 60 ?* ?, Q( W! A+ f
    . z* W$ C$ S8 l( V, A' o/ a( Q) ]. s
    递归思维要点
    ! Y5 Y6 y, |& T3 X% O& }, v1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    0 R9 ^* p/ [1 H: d2. **栈结构**:每次调用都会创建新的栈帧(内存空间)8 @6 l! v3 M) C0 {
    3. **递推过程**:不断向下分解问题(递), K  q( h* `/ [' x( T
    4. **回溯过程**:组合子问题结果返回(归)
    2 b, M6 p6 m* W7 ~( k5 `/ s# T/ |& M, s- ~5 ]* B* V
    注意事项# r. o$ d& E: g* Y# x; G
    必须要有终止条件
      ^+ Z0 p/ ~) x/ J9 _" E递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    , y2 [! T- t( ^- m% L某些问题用递归更直观(如树遍历),但效率可能不如迭代0 E/ _. {# r7 a' l$ m
    尾递归优化可以提升效率(但Python不支持)
    & V# f/ c, O7 q) `  E; S5 m4 q, i$ e
    递归 vs 迭代
    ! R/ g9 a9 |' `4 {* P|          | 递归                          | 迭代               |
    " v" u/ Y# U7 l/ V9 r|----------|-----------------------------|------------------|- j4 }$ G# W9 M0 X5 `+ R5 D
    | 实现方式    | 函数自调用                        | 循环结构            |$ L& Y! P4 k& e, e' {! `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- Y- h$ w  W5 r6 x
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- B( L4 [) r7 |7 f6 `
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ o" [; L- X( @. n

    $ u6 J' h& M7 x) a 经典递归应用场景
    $ c  C  w9 p" w- }! w0 P* L' {, d1. 文件系统遍历(目录树结构)
    + e) ~% r9 {" ]- Q* Y1 L0 C2. 快速排序/归并排序算法) v; A' [# b# Q
    3. 汉诺塔问题
    4 q# V* \$ g, o% e' m# |) ^* D& s, U4. 二叉树遍历(前序/中序/后序)
    5 j* E4 J' R" @: t0 `5. 生成所有可能的组合(回溯算法)
    * ^% @/ B2 }) f. [- p0 |& S9 F0 P. Q% d' C
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 3201 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    + o& s1 S" o4 C0 L( \5 M" z8 Y我推理机的核心算法应该是二叉树遍历的变种。+ B7 T  F. _' j' \
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:% f- h9 m0 l; P
    Key Idea of Recursion
    ! Y4 L2 l1 n% d" A& N
    * a. D, l% s4 N7 M. u7 yA recursive function solves a problem by:
    8 x/ |) s: f; q: T/ d, f) |% ]" V4 E6 W' o: [3 w
        Breaking the problem into smaller instances of the same problem.
    5 R, e) S) ?' R! k6 i. a
    7 [5 @8 S; n% `4 f. X3 l0 e+ B    Solving the smallest instance directly (base case).
    . G: I  U# x5 f5 G/ k4 I
    2 c* M+ X2 G7 G2 v4 C0 N* F    Combining the results of smaller instances to solve the larger problem.* F9 l! F: P; E  s; U
    ( |. V  M0 i1 n- l* W' t
    Components of a Recursive Function
    ! S4 h9 E# s4 \4 R6 R% G& Z- d  l5 ]
        Base Case:
    . T- X2 }& y8 w( G& Z; G% [; s: H5 k4 s; r2 d$ \; N
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 B* {" K3 a& d. o

    $ s+ O' Q& z  ~2 [: l) D7 i        It acts as the stopping condition to prevent infinite recursion.
    8 x. i) w; ], q6 }+ @
    . s! s5 ~5 G2 O" Z1 e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ) S' |; a( N- R" v+ e
    0 E/ I# z) U  j. G6 s    Recursive Case:5 W8 U& j- k. X( P: p# ]

    4 q2 ^! Y( y# B, G+ j- t        This is where the function calls itself with a smaller or simpler version of the problem.
    ! i8 [' B2 h; A' c5 j/ K; w+ ], ~& u2 l& x4 A: O0 G
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).) S, ?8 u/ p' k$ T* ?. [) Z

    , T4 O3 F1 d( u' G7 t" |Example: Factorial Calculation( e' P0 [  U3 k4 K/ U5 W
    $ {; {: i5 h% d+ B4 N7 j, o1 \
    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:
    ' b4 \6 J! y! Z
    3 M& h$ g7 \, ^5 M, O* b* K    Base case: 0! = 1
    ! r9 `# W3 b6 C9 z
    8 H8 Y! M& G+ L) {: I2 I    Recursive case: n! = n * (n-1)!
    $ C0 s7 s" E: j4 F$ m" N
    ; Q5 l$ v  Q  j: S  O4 @Here’s how it looks in code (Python):
    + K( N5 _( G# N) u3 tpython3 j: Z( y/ ]* j( j" Y  c: B
    ' l1 p3 I1 l/ v; u/ |, Y

    8 q6 N* k. ^3 a- n# G, W( U9 m5 ?def factorial(n):
    $ s7 F# `! x8 @& G" m/ I3 r- B    # Base case
    / h$ I% [8 b% r8 p+ N# {- s    if n == 0:& B3 Q3 d6 x' j9 Z
            return 1! W/ r" d0 ^9 P
        # Recursive case- n* V& {  n& ^: O
        else:
    2 E5 t- H7 L+ p, h' I        return n * factorial(n - 1)+ A4 B' d7 V- |6 n! m3 d  t' [

    5 q# z, s3 N) o4 s& A! v) h: L& [9 K# Example usage
    7 w- m; J% U7 T4 |0 b# O$ cprint(factorial(5))  # Output: 120
    $ }: N. K) ^5 I: U3 ?& U! U8 B: |  v7 W+ v0 H- m1 l
    How Recursion Works8 t% k& M7 ~, K8 I( g  |
    2 C' b5 m/ I1 E( ~- n
        The function keeps calling itself with smaller inputs until it reaches the base case.6 i2 E/ d/ O0 J1 l! _
    1 M$ ~1 `( g  l1 r  b
        Once the base case is reached, the function starts returning values back up the call stack.! d( `. z! ]6 i& ]4 m% J
    " ?! ]! u; G* R! o# G& f
        These returned values are combined to produce the final result.- {, `* T% S' S, ?
    ; `/ [1 b" S5 g
    For factorial(5):0 U- v2 K+ Q3 U# Q3 j$ v7 P
    : @4 v" B9 M7 x; E4 w  q3 f

    * p$ {0 D+ A) D- ^factorial(5) = 5 * factorial(4)% j1 T  |  {7 a. l% ^' Q3 l/ n, O
    factorial(4) = 4 * factorial(3)
    # p; Q) L2 c; t0 ]2 m8 m: \% |2 Qfactorial(3) = 3 * factorial(2)
    - S1 \: z9 w& P. J) C2 Ufactorial(2) = 2 * factorial(1)
    - H7 t( O: x$ P; kfactorial(1) = 1 * factorial(0)4 N6 ?% R5 t8 i) [. f
    factorial(0) = 1  # Base case/ Z0 {& K6 M$ D. u: @/ y

    - E6 d0 T7 i: x% e1 X  w: fThen, the results are combined:
    0 z0 X) U! W, S, k9 q4 S- {
    9 X$ m8 y% `' M. H! u% l0 P
      M6 _7 Q, D# u) p, a- D" s+ N9 Pfactorial(1) = 1 * 1 = 1
    , N2 W/ c0 a) }. n2 Lfactorial(2) = 2 * 1 = 2; f% u# h( _" a7 Q6 ^
    factorial(3) = 3 * 2 = 6
    1 }7 }* y( M! \$ F* V  |: Bfactorial(4) = 4 * 6 = 24( q+ l5 _+ G- o' W" s0 R$ c- F
    factorial(5) = 5 * 24 = 120+ q1 q/ q% S& T" W+ G
    2 m* R( ~$ q" A/ b. t
    Advantages of Recursion
    7 y9 N/ j- v9 U
    9 C/ w2 S8 E0 f: Z. H    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)./ M/ L( V* q! c2 ~2 c0 M( @
    : X- _- R, I* l7 L5 G% Q
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - m% Q  r( M, i* x7 c5 @0 R7 f$ Z" ]; V
    Disadvantages of Recursion
    % W& ?" |( f' j
    . ], m$ A5 x9 j    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.$ Y) n  C$ R* @; M
    ' M9 B% z; J  n' q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).3 z/ H  z- E" ~8 w  e8 A
    # E8 q* l, d' h5 c/ ]; ~  G
    When to Use Recursion6 a/ [; ~5 s* E7 }/ \# ^

    - E+ o* {, z( H+ j    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) y. ?* W9 D3 ~$ l+ c, i

    & _* k4 Y; M* h' I- U& i    Problems with a clear base case and recursive case.
    * Y! |0 N0 J$ S4 h+ u. E
    ; J& C& s3 Y, YExample: Fibonacci Sequence
    ) i2 r' Y' W& [, N% {, n. `5 t
    " h; O. e( b3 H) zThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 P) a$ D# A& o0 v! [7 k3 f! j- _; X; L5 _. A3 B- u+ V: I* n9 n  N
        Base case: fib(0) = 0, fib(1) = 1( ]: ~. ~! \% O& G/ k

    , X3 n) Q2 \% O# A% E5 T    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ) [+ W1 M" I" F! _3 a, O8 ~
    - u6 E# M4 f# R$ u! t1 s9 A: S3 P) vpython) G- i" Q6 |7 ~2 |# t& V
    9 F) l& m0 ^" ^% X; @0 i
    # f3 y% U. h& b6 `1 ^
    def fibonacci(n):
    / ~  e- ], w* u, a" f9 ?! ?    # Base cases
    " ^2 O# C  e. E) x. g4 K    if n == 0:1 o# p  l5 I# O* N1 d/ Z9 Q
            return 07 q7 d1 m9 }  X2 O! `
        elif n == 1:
    % b- m2 i: u+ _$ {6 j9 Z        return 1
    1 s6 M, {7 z8 z$ b; ?2 c3 t7 a0 z    # Recursive case
    5 k+ Z8 a# x/ e! n6 ]& Q5 u    else:" I3 A9 I! q7 t
            return fibonacci(n - 1) + fibonacci(n - 2)( @: u  j- s2 Q4 i0 }& a/ J
    * y/ W& a) \1 K9 D; n# @
    # Example usage% M) q6 ^/ L: K$ O; t4 u
    print(fibonacci(6))  # Output: 8
    ; W' p- S2 a4 u+ X' I
    3 m. @$ a  H, l& KTail Recursion
    - c/ M9 U& ~4 W2 N* [) [  I. ]5 e8 E/ c6 n- L( c% {" \
    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)./ z' _. b% d8 }. R* t
    7 U  n1 }3 c$ Z$ a  G
    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, 2026-3-14 22:15 , Processed in 0.056072 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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