设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 u9 u5 F0 ~6 p% k, E- i2 c
    1 S# `( E& r' H, H: f' ~! x! Z2 h解释的不错
    - s/ N+ m7 u6 E9 U
    # k3 l6 q8 t( G2 {递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    - ^; f/ I1 S5 }1 H7 Z1 ^
    ! ~! K+ w; A6 F6 ?9 k5 F, S3 _ 关键要素$ _9 ~& O6 X2 f; A' L
    1. **基线条件(Base Case)**
    * B, ~+ H5 o9 |9 {   - 递归终止的条件,防止无限循环8 m) q4 I& \6 ?) t) T
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 E5 m# E& Q6 O6 v! q
    . }$ v) B$ W0 E0 p
    2. **递归条件(Recursive Case)**: X1 K9 @& i# A9 i- z
       - 将原问题分解为更小的子问题
    + Z$ ]0 L( O4 b+ I6 K  B1 g; w- K   - 例如:n! = n × (n-1)!6 R0 s$ K. o$ U8 _$ n4 |
    ; n% d6 i4 M2 N( \2 z$ p( T
    经典示例:计算阶乘6 Z' h# ~4 h# ]# t0 v) S0 n
    python
    % u' _! T  B* {def factorial(n):% I& z+ n9 ?9 M7 Z* a9 L* e( Y
        if n == 0:        # 基线条件
    , v# r- j: c# V; K6 I5 x" ]: T        return 12 t7 }. X+ C$ {1 K
        else:             # 递归条件8 M0 O/ s2 _3 `5 P: {- E
            return n * factorial(n-1)
    , B7 e; U  @4 k执行过程(以计算 3! 为例):
    ( j& Y# G' M" K2 c! Qfactorial(3)
    % J5 y9 n9 X' Z5 _3 * factorial(2)
    % v' Q1 c% Y  g2 X( f3 * (2 * factorial(1))/ O0 |5 [' }- m1 G: P
    3 * (2 * (1 * factorial(0)))" k; P' S% }# I' [. b9 z9 _* W1 Y3 r
    3 * (2 * (1 * 1)) = 6
    3 D' c6 C+ b) }' U/ k6 p/ k
    / e* \  M' g+ z: Q) g- R 递归思维要点
    1 J* s7 o' w" X/ q3 `# J1. **信任递归**:假设子问题已经解决,专注当前层逻辑8 y; l; R( i# L' A! ]
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)' ?% [1 ~% h! Q
    3. **递推过程**:不断向下分解问题(递)
    ; t- f9 f; V/ L8 q& V% y( Z, r4. **回溯过程**:组合子问题结果返回(归)8 W' S% u0 i. P" L' F  P

    5 F. p( B' G4 ` 注意事项/ }; r/ v6 F7 b9 u5 I
    必须要有终止条件. N! T. }4 L3 N
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 A: e7 @. d- H, r, h8 E
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    % W; Y0 p+ l  n6 q3 c2 Y5 Z2 ~1 U; C尾递归优化可以提升效率(但Python不支持)3 j; V  E6 c% Y, _8 w7 |5 K, U  ^
    + D9 Q6 ~) f( e; K7 K/ |8 w
    递归 vs 迭代
    ' l$ z$ k, M! z% c' Y  f1 B1 x|          | 递归                          | 迭代               |
    ; Y* V  J7 X" n; E( `' [$ ]|----------|-----------------------------|------------------|3 b7 A2 u& f3 S% K+ C; _7 u
    | 实现方式    | 函数自调用                        | 循环结构            |1 u6 @$ n* l! ]6 O) n
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 R2 F8 e) o7 k/ n+ w| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ {5 {. ]) x8 J2 y1 X, r
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 o# x0 N& l+ I4 `2 ]8 z! }: x% D; O6 o1 P, Z! B. X" m* g3 w
    经典递归应用场景
    + y. I: K2 ?- K7 a5 c1. 文件系统遍历(目录树结构). P5 o7 f  `' o+ f( ]0 o$ R( w, e+ k% E& U
    2. 快速排序/归并排序算法  f5 z2 w& X1 P* x7 r- J
    3. 汉诺塔问题
    * P4 ~+ D4 x' \9 }! p6 |2 ]: A4. 二叉树遍历(前序/中序/后序)( d: Y9 p5 \1 ]
    5. 生成所有可能的组合(回溯算法)7 O4 F  e( d& D' r0 \

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

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    8 小时前
  • 签到天数: 3234 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,' z/ r/ o# Q5 l; A% Q0 j' z4 p) p
    我推理机的核心算法应该是二叉树遍历的变种。! U7 K, E5 B& 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:+ m, B3 s$ T# A$ `' e. G" H4 R
    Key Idea of Recursion
    5 [4 i/ {) e: N5 N( v$ F$ x" j7 P5 y( i% E2 T0 @1 I
    A recursive function solves a problem by:* a( q$ O; s, Z
    : O- b! j1 a- F+ T
        Breaking the problem into smaller instances of the same problem., a6 O7 H8 A" f3 i' C

    3 v% U0 n! R3 j8 q# ]    Solving the smallest instance directly (base case).
    # X7 f5 D$ ~8 r( R: r+ `& i
    ) e/ M$ J- n) H    Combining the results of smaller instances to solve the larger problem.6 h" w" y, |* \8 L
    + k+ d  {- u/ D' Z
    Components of a Recursive Function
    " B. o5 E( P& G( }6 ~" W  Z
    # U9 ?" Q( G8 b+ \    Base Case:1 r7 `6 w9 d! r) N
    7 w$ p0 f" p4 S+ }, C2 K( `
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 M8 e3 ?5 N" C
    3 f6 m8 z/ B8 _5 |' h        It acts as the stopping condition to prevent infinite recursion.- G  o) R9 P3 @$ ~8 L7 x( l/ I, d) L

    7 Z/ S% R/ O8 E- |        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.) ]9 I) E2 l$ O" v4 M& w, d6 M0 h0 p

    , v; {6 k% w$ z7 Z6 o2 C. t    Recursive Case:# [. t: _! H3 `
    9 h7 U0 o, V# R4 y
            This is where the function calls itself with a smaller or simpler version of the problem.' ]1 |) M# d2 S  ?& m8 F& p( ~

    5 ^" v5 e/ {3 I, y; x8 Z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ) `% j2 C' j1 z# K! u1 o- B+ @; m
    2 R; D9 W& m# {& zExample: Factorial Calculation
    % ~5 X3 u% e* Y) [9 l; @
    + M5 k2 H/ h# i& A( @3 p3 w0 aThe 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:
    5 V+ ]- k3 t2 G
    , X. P9 x8 _/ f: M    Base case: 0! = 1
    * J) M0 ~" [: z. m' ]8 [4 m" l2 _/ ~3 Q1 t! K! @& R, X% m
        Recursive case: n! = n * (n-1)!
    3 }. T1 Y  ]) G9 t  g
    + J# c4 O" V/ Z" X6 w0 y+ u# Y% [Here’s how it looks in code (Python):
    3 v' R4 H  @$ Z3 \- mpython; y5 U. ^: W6 [$ U; y

    ; _* ]6 U1 u8 u0 H
    ; m# B* t/ i5 n) E1 ~5 U6 Q* mdef factorial(n):
    ) ]1 x0 A( p, B    # Base case
    2 _: v: Y& X: R; j4 Z( F* F    if n == 0:
    % S% e- U  @5 ]# e( i8 r6 \& L        return 1
    $ s* ^* g5 L2 h5 g. G( {3 ^  S    # Recursive case
    3 ~! z2 m3 {) w8 o# b    else:! {4 C8 N$ R4 k$ X7 V9 L
            return n * factorial(n - 1)
    " s# d. O: a/ d* _/ @8 J
    + F8 U( D6 D# y4 `. P! F# f* z# Example usage' f9 A" Y0 x6 h( _* H  G. O7 Z. ]) D* M
    print(factorial(5))  # Output: 120
    ) J; e. J; n; O$ m# h9 a2 R" r7 Y. s/ k5 d; M" d, c. L
    How Recursion Works6 g% ?: X9 z- z3 {  C) X: ?! t1 A1 x

    : ~/ z9 |) @9 p! _2 q    The function keeps calling itself with smaller inputs until it reaches the base case.
    - V  ?5 F0 ?7 F3 N# t6 C( R0 n5 V8 H5 r+ [, }3 M
        Once the base case is reached, the function starts returning values back up the call stack.5 A5 P# Q; R0 k0 M0 k6 P2 G

    6 Z, t, u$ h3 f6 R; ]: r5 C    These returned values are combined to produce the final result.; a. v) x* G; O! T; v6 F$ d
    / }' a; t7 w" j" R+ r* G, q
    For factorial(5):9 K2 w+ u/ p* t0 }7 O9 @
    ( T$ L" J* A& e1 q$ W

    ; P% F" \: K6 F' E8 m9 Gfactorial(5) = 5 * factorial(4), R; S- d; V/ {+ T4 H
    factorial(4) = 4 * factorial(3)
    6 z, p4 ]+ ?0 v- ^factorial(3) = 3 * factorial(2)- X- b* T' W6 B
    factorial(2) = 2 * factorial(1)
    : z, F* w! @9 a; Tfactorial(1) = 1 * factorial(0)8 r: y% M$ x# B
    factorial(0) = 1  # Base case3 Q) j% W, k: Y6 f2 @- s
    + Q0 J" a) |( P, X
    Then, the results are combined:
    6 b0 S9 U/ m" H6 _# S3 G
    8 S( _: a! \4 Y  L3 h) N- c2 ~' y6 v6 r( ]: N) x4 f: Z5 K
    factorial(1) = 1 * 1 = 1' r3 r; n1 x! ^
    factorial(2) = 2 * 1 = 2
    4 Z' n7 c& _$ ?% qfactorial(3) = 3 * 2 = 6
    8 z9 l3 Y2 G* O, `+ yfactorial(4) = 4 * 6 = 247 @* v5 M$ g# k! M5 a
    factorial(5) = 5 * 24 = 120
    3 q3 `# G* s% s; V( Z7 p) c
    6 P  R9 z- F, R- K2 Z' p; K" g+ RAdvantages of Recursion
    + O8 J- j, L& K" N2 N  Z1 u8 y
    9 D5 F* b2 i: u7 x! X: w  A    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).  N) m3 `; R, Y0 C2 O' O

    - {" b# x0 n& {  [' i, @" m- {: c    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ' C, |$ `+ `8 B9 w7 K2 W& l$ b9 `# @' e$ W2 M4 w
    Disadvantages of Recursion
    ( d$ ?. k$ m( z. q, j- ?
    5 a9 k6 f+ [* Y( Y% M& Q1 v  a    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# n7 l+ s
    + `) T# ^& @* o8 g) R0 Y% }% r    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ! V6 }; {0 b+ P2 d0 w0 N  D& `! n8 `2 O+ V  H2 H) O6 }
    When to Use Recursion
    ' b0 y9 k8 K( Y3 k8 ]9 H$ i& g
    8 ?& K- D1 ?& S/ d$ C, ]    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)., y+ ?; u9 d( I9 n* W

    ( V5 _- d& D6 j5 H% E( i$ w    Problems with a clear base case and recursive case.! |9 H' |' s8 n' \
    2 i( _& s8 Y. x, H7 a( C
    Example: Fibonacci Sequence+ Y% R' p, s1 F, P' s2 E: `8 h: k

    8 m( a2 y( O' F; }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 O, z" G( G- p2 S$ M3 @" g& @% Z2 g' q; n, a1 @
        Base case: fib(0) = 0, fib(1) = 1
    2 \3 u% D( x! s2 h) a
    9 \. \2 r& v4 F    Recursive case: fib(n) = fib(n-1) + fib(n-2); u6 X% i* D& ~" b6 ?8 i

    - X/ W( A/ y' S( j7 @$ Z: jpython: E6 u/ E# E& F! N: l' W0 F/ q
    , w1 C1 H( E5 t

    0 _+ s8 N2 P6 S1 q$ bdef fibonacci(n):/ `3 G* U9 F- m2 u; z, p$ e
        # Base cases: ^2 r) V1 E; _. Y. T; K3 }
        if n == 0:9 b: |# o( G5 m7 K6 f
            return 0' P) O9 S8 H4 P3 ^% h1 u% H( j
        elif n == 1:
    , q, B2 ?- l* x: G. ?        return 1( Y- J% I% x4 }; O$ i
        # Recursive case5 R1 z2 T5 T  O1 g: S
        else:
    6 ^- [; N1 {! ~6 b; O' B6 Y        return fibonacci(n - 1) + fibonacci(n - 2)6 ?: J& L$ I% c

    . x+ j. D+ }; H4 |; C# H# Example usage4 G5 k, ^0 L( o# ?8 c* m
    print(fibonacci(6))  # Output: 8
    # E/ Q$ V% Q: b6 y# W( t7 p- }
    ; |2 v& N$ @4 v/ r7 I1 Z  }9 l8 HTail Recursion
    8 c' W+ P7 _  p, w
    / g! _0 L0 G! O6 E, Z  [5 jTail 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).
    $ t! H0 t. F9 N, ^' \
    % a& `: W4 V" @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-5-8 17:20 , Processed in 0.057963 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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