设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ' o) o1 B3 g6 E# S& _
    * j8 i, [5 L7 w, L解释的不错: j/ B# R0 F  L" U- r+ M, H1 x
    # ^+ q* @% k" x- i
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% `# V5 i) E' l. d

    2 Z. h$ A/ X; n+ M! D3 q 关键要素5 t5 ~2 C+ R- N; {  O% C
    1. **基线条件(Base Case)**. C) e8 ^* D& e0 d/ i
       - 递归终止的条件,防止无限循环1 E4 ]! }( Z" y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    $ s- U" g9 [6 y, c: I" M' B( [8 A' k+ U2 k
    2. **递归条件(Recursive Case)**# P  F& r" }$ X# A: @& k- f
       - 将原问题分解为更小的子问题
    ' w, w" C2 F$ R   - 例如:n! = n × (n-1)!7 X4 X+ \7 ?& ~+ |( u5 y0 `3 H0 p

    . I, W" t' _9 k' r4 U# @7 | 经典示例:计算阶乘
    - _  O5 W- g# s/ p$ fpython
    % T. `% E& _, K: k8 n: wdef factorial(n):8 H2 E% w. W! C- f
        if n == 0:        # 基线条件: @# \$ O6 m9 H: e% `
            return 1
    " c- s/ z" W# F    else:             # 递归条件$ D2 r; Q$ o9 v7 `( Y
            return n * factorial(n-1)* C7 Y4 K1 {, ~( I
    执行过程(以计算 3! 为例):
    ! x8 U& z" z/ A) l" D& @' Hfactorial(3): ]5 K' Q9 O& I- X+ r9 v# W
    3 * factorial(2), U7 r/ A  z: ^( x) a; n
    3 * (2 * factorial(1))
    ; a1 A, @1 `. H7 K3 * (2 * (1 * factorial(0)))/ [! T3 P  u5 z4 N- {  F
    3 * (2 * (1 * 1)) = 6
    , D& F! d6 r! S! C7 @, K$ {
    4 l# o  h: B% r6 R" @ 递归思维要点- m8 ^+ t3 o) u3 k0 P
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ' b- Y. V& b. j  B2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    * `' d4 b5 ~* \( X2 N1 [3. **递推过程**:不断向下分解问题(递), |; E! b4 f5 E+ w( o
    4. **回溯过程**:组合子问题结果返回(归)* I& }* L/ O4 N; E/ Z' j
    2 i2 v. s  R+ |$ H8 Y
    注意事项  o3 a" L" S. X& k
    必须要有终止条件
    7 ]) Q1 ]$ Z  i5 z+ o递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    - v$ M" Q1 h6 A2 i某些问题用递归更直观(如树遍历),但效率可能不如迭代. v* d, N: \( F) C5 u
    尾递归优化可以提升效率(但Python不支持). S3 \, Y4 ]+ e5 F9 V8 D/ G! `
    4 Q- ^( K! N% p
    递归 vs 迭代
    / l, F$ w' x0 \: G|          | 递归                          | 迭代               |/ b7 I8 S) D$ O% I1 S9 y# e! I
    |----------|-----------------------------|------------------|
    6 L5 t1 T- ]3 c3 H4 N( ^| 实现方式    | 函数自调用                        | 循环结构            |
    , p9 c; ]6 {8 K| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 L$ Z+ S" y! P3 k8 k0 G| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 {: u, p7 G  g  m: ~) f| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 R* |" r. o' P' b$ K: Y/ H3 C. p. ~4 g! }% k. i
    经典递归应用场景& P% Y4 _8 R7 N2 ?1 N- S( y. |
    1. 文件系统遍历(目录树结构)
    ) K2 K) x; |$ X$ A! ?+ z2. 快速排序/归并排序算法
    9 S$ i4 I2 T, ^% @3. 汉诺塔问题
    9 o6 U' S, o4 ]1 @7 _+ h4. 二叉树遍历(前序/中序/后序): E* P1 M! X' B7 R9 g+ ]. x! A
    5. 生成所有可能的组合(回溯算法)) C% N; r$ y3 B; H' _( g8 Y  c& }

    * T7 f" S5 A( z: ?3 o试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 13:27
  • 签到天数: 3116 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. ?+ G; f( }" H- @4 c1 T. l
    我推理机的核心算法应该是二叉树遍历的变种。
    , n) A3 S9 ?- H/ T6 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:
    ' [: D3 _! w" L: ~% R5 qKey Idea of Recursion
    1 s2 K  [, g. E- p( c+ ~7 \2 L' f# l; H0 y4 Q6 N
    A recursive function solves a problem by:1 b: q) ]/ P# A$ T, `1 q" Y  T5 @' |

    # A) @) J8 ~: Y' H) C" v    Breaking the problem into smaller instances of the same problem.
    % I. ]+ z, j4 b$ B$ g, P8 o6 |5 r* b( c1 A- s1 v+ L( J  x
        Solving the smallest instance directly (base case).! {, S/ s: p  X) ^

    1 j3 J( _: `, M$ I5 K! B; z/ G' Q    Combining the results of smaller instances to solve the larger problem.* @1 }- O& s; ^9 r

    0 s9 |+ P& T* ~/ ~/ jComponents of a Recursive Function* @! i' l+ @5 j; W7 P

    0 t" v  c5 C  a, F0 D1 G% |) @, x    Base Case:6 p8 A' E# o- p) U
    & M' @) |; v4 u
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    * G0 ]$ L. w3 [; Z" S7 a: l  x3 p8 q4 k' P
            It acts as the stopping condition to prevent infinite recursion.
    ( m7 r$ \; e4 D7 M& v+ f+ }9 M, `8 b! |& {
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.6 S  Z, X5 o) c- U( C

    - b9 O0 a( o$ s& T5 D& q    Recursive Case:% @8 w$ e, D" f& ]
    * c4 Q& f& E1 ?$ M, t" S
            This is where the function calls itself with a smaller or simpler version of the problem.
    ! p: U/ Q+ Y: w& C6 M0 i  C
    : `- s" Q5 ~( [3 I* r& n, Y2 i        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    - M8 ]/ i4 g& r- K6 N! s
    . b, t+ ^& ]* _7 K3 nExample: Factorial Calculation
    1 n! o( N$ o7 x+ v/ k: p9 O; p1 N4 D
    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:3 g, e; w" G8 B6 E9 L% r

    * K1 J0 {7 N: {    Base case: 0! = 1. g# s2 v8 _$ o/ p  W
    - X/ _  |: p3 h+ Z. ?
        Recursive case: n! = n * (n-1)!3 Z2 o. ~' s7 D9 Y

    / x) g+ Z3 g- Q& [Here’s how it looks in code (Python):
    : r5 _, |& D$ d+ X. V  Y5 [& K* Ipython
    6 a$ z% c  D8 x+ Y
    9 Y  C9 f; M! A5 j/ Y4 d% a3 i2 X7 U# Y- S" M
    def factorial(n):8 T7 l4 s# u4 U  |
        # Base case
    9 _7 S+ L4 p* X    if n == 0:
    8 c. b  G# l# i1 i+ ^: z8 {& V# H        return 1
    + V  r# s5 X* H7 J$ d    # Recursive case! h5 t- c8 s" g9 D- I) l
        else:4 A0 }: D$ r. T* G; E
            return n * factorial(n - 1)
    - _! {5 \$ r' u! ]( V% E) Z3 G5 O2 G; d: v" q' g. m
    # Example usage  g2 C) F" v; l9 X" C
    print(factorial(5))  # Output: 1205 r! Z/ N  ]' ]& ~
    ! d: T$ o2 A0 Q0 p7 f! }
    How Recursion Works
    * l4 L" ~: ~" v( X. f( E2 u2 Z' ^6 I- F, Q, y9 A
        The function keeps calling itself with smaller inputs until it reaches the base case.
    % J; T" D/ E3 x# q2 Z/ @8 e. d# N/ c4 Q
        Once the base case is reached, the function starts returning values back up the call stack.2 O( e4 w5 b+ V% Y  L9 w1 {, z
    ) E' x; {4 N; h6 v- D
        These returned values are combined to produce the final result.5 _3 E$ K7 P! A  G. D

    ; a: j5 e; Z0 I- GFor factorial(5):
    ) m. `* o8 U+ `; t4 t$ Q. n0 T+ }  t) u

    7 l( Z: h! F, P" `4 y* W- Mfactorial(5) = 5 * factorial(4)
    ! ]) P$ |( z$ [$ kfactorial(4) = 4 * factorial(3)2 J' ?2 t2 b/ |  v$ r3 h2 h* x
    factorial(3) = 3 * factorial(2)* m/ a0 C8 U6 W
    factorial(2) = 2 * factorial(1)7 @& S8 Z$ O: G* A/ v$ ~
    factorial(1) = 1 * factorial(0)% ?( v$ y) s4 @* X4 g
    factorial(0) = 1  # Base case
    : e& e: X3 }" \3 |" B0 Q# o# [* N) n/ x. b2 F" E# s
    Then, the results are combined:
    " h: g. ]- ]2 P; T4 }% n/ s. Y" q5 l
    : u) K$ e8 K* h3 L
    factorial(1) = 1 * 1 = 17 z2 [9 Q: v9 _( G4 l# Z1 L
    factorial(2) = 2 * 1 = 2
      ~6 {: k1 t" w: D& D1 {factorial(3) = 3 * 2 = 6* }7 e0 n( C3 G3 |5 i6 b% p
    factorial(4) = 4 * 6 = 24
    5 g8 ]0 E. b  lfactorial(5) = 5 * 24 = 120
    & A4 n  K# y+ R( B5 `. H  E/ S/ w$ ?* G) V* \
    Advantages of Recursion: w. E  I. e# `( P

    ( C6 g" H% Y  m2 k: 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).
    ' d: B* W6 R0 i. O7 I3 x0 B  a4 ^$ S6 n7 ]& D3 {. e5 X+ m
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - K2 @8 Q$ m( g+ |- ?" o& ]+ u) l* V8 h
    Disadvantages of Recursion  T- L$ Z( }7 S/ A

    2 l2 o* m- y8 g( d7 W- a" U0 l+ I- U    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.
    ' B# R. C# A" ]8 U3 @$ A2 X6 O
    / A' y8 o7 Y; `+ U2 n1 Q    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).9 T7 a0 \$ G. q1 d+ w

    ) A- G( c& K) C* y3 gWhen to Use Recursion
    3 F/ y3 z( E6 V+ J" f1 t& I! h" @, R& f3 `+ |, G5 L3 r3 d  S
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    8 s' o" k) u) j9 r( X$ v: B! c$ L
    3 c1 [9 T0 s; a* N3 K( M    Problems with a clear base case and recursive case.
    ) n- d% F, u9 P/ i8 Y& j' A0 ?6 [8 Y) @0 V0 O
    Example: Fibonacci Sequence! b; R2 D- D3 k4 q% }9 H, H( d# B5 N. M  p

    0 @& d0 k+ h# y5 u, }, CThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - f9 U- K( t6 ^1 g  R5 [4 O7 X$ ?5 q
        Base case: fib(0) = 0, fib(1) = 1
    1 b2 B! v: Y; s) ^' Q; B/ `
    : {' L4 _& P; B, o* l! n    Recursive case: fib(n) = fib(n-1) + fib(n-2)( R2 b1 N* L9 M& l0 p( T. b$ i' u
    1 a7 y: T/ K, @$ E
    python
    $ V6 d3 @  c& K% I+ {
    : U/ k1 M8 s# `) S9 m8 r" X# E, ]1 G! y1 a2 W0 T
    def fibonacci(n):
    - i9 J4 a2 o9 K' c# G  c/ B    # Base cases
    / ]/ e7 ], {5 `' h! q. h+ R3 p    if n == 0:& L" Q+ n8 ~) O5 g/ e
            return 0, Z7 g( N1 ?$ [+ [) }6 a& X
        elif n == 1:- L8 n, p/ l' }$ U5 c; k
            return 1* ]$ f9 g3 f9 c; L6 Q0 I2 g( z
        # Recursive case; W5 [6 x2 K5 u& D' j+ p1 L
        else:6 ^& n" W: _0 `& B* R
            return fibonacci(n - 1) + fibonacci(n - 2): r. _  f0 }0 J+ U

    * T7 A0 M* H+ t7 t6 F) ]- r3 I# Example usage
    2 _* k9 D# {  E" N: `# ^( B4 D% Wprint(fibonacci(6))  # Output: 8
    * [3 v. X" F5 ~* j/ B3 Q
    " g( |2 K( j' V! a. @- ^Tail Recursion
    3 f2 v  U5 G4 }: h9 u9 H, u6 m2 z  S, p3 `, O
    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).
    * }, H# u6 {6 U! S* ]9 L8 y" w) ^& L# r& A8 {
    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-14 06:24 , Processed in 0.033127 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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