设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 }! g% M# v+ D4 x4 \7 p& ]1 D* E& r* z2 V
    解释的不错; B+ I2 ]9 Y* l4 `
    % V9 S1 Q! O" R9 S2 [
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% c# p) W# a6 y1 L" `
    # f6 l5 u; K8 X& g* t6 [! [) {4 V
    关键要素' I+ F% f1 R3 q: h5 g: H
    1. **基线条件(Base Case)**
    ; J5 P6 E* L' U- i# S   - 递归终止的条件,防止无限循环
    + l, ^- @5 j9 v   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1: ]6 ~& {9 l) C0 ?

      I0 G$ G+ N7 H/ v5 Z2. **递归条件(Recursive Case)**1 f; A7 w# S4 U* v
       - 将原问题分解为更小的子问题& G& N( c& |! _
       - 例如:n! = n × (n-1)!
    % a  i" @6 J* \2 K+ z* G- v
    " o% t- M3 w! c4 p& U: x 经典示例:计算阶乘# x& x, s$ T0 ^7 m2 O
    python2 Y7 A0 _! _; t( {
    def factorial(n):" y, P8 r0 u1 ]$ a8 V6 N' Q/ ?
        if n == 0:        # 基线条件/ [8 J& j) [" V. }/ T' p: Y
            return 17 G2 S0 ^6 D1 N4 O1 R% l, u3 o
        else:             # 递归条件' H* W- y! ^# t+ A/ s* X$ J6 g; Q  J
            return n * factorial(n-1)# g. G  B0 _4 Y& U8 E% i
    执行过程(以计算 3! 为例):
    6 w% C! M8 F4 ~$ b; j/ O3 jfactorial(3)
    5 e4 F9 Z3 {7 ~. T3 * factorial(2)- f# C! Z! k) U8 @5 E0 A
    3 * (2 * factorial(1))  X/ W+ U+ ^# F/ U$ a, H
    3 * (2 * (1 * factorial(0)))
    : }% @: w- ]& w0 H/ T. ~3 * (2 * (1 * 1)) = 69 ]- A5 q2 @  z' |% W6 V

    2 Q+ W" {$ q, A2 | 递归思维要点
    7 x+ g( {4 W4 q" u9 O" z1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    6 w  F9 f! z& [- x- m9 B2. **栈结构**:每次调用都会创建新的栈帧(内存空间)9 w5 Z2 B% p' w* W2 r9 j
    3. **递推过程**:不断向下分解问题(递)$ S7 U5 J7 A( S  b2 T# W% S# n; l
    4. **回溯过程**:组合子问题结果返回(归)
    , u) |0 l0 u, w0 _) u/ x
    ( {3 R) ^. o6 @1 r6 Q" B8 H 注意事项
    , o) H+ c& K4 |1 e* c, c* X必须要有终止条件
    1 N% N% O# {4 n+ B递归深度过大可能导致栈溢出(Python默认递归深度约1000层)2 E% C4 ]2 o* Y! ]/ \$ s+ ?  k, x& N& B
    某些问题用递归更直观(如树遍历),但效率可能不如迭代  S9 p* n6 z: D5 r
    尾递归优化可以提升效率(但Python不支持)4 C# M7 z- w8 X% A+ M
    - A& x+ I  B7 Y; i. M
    递归 vs 迭代  w" y+ A, Z! O& f
    |          | 递归                          | 迭代               |& F0 Q" S# |/ h6 r' p% u% _# k
    |----------|-----------------------------|------------------|
    # B$ J" g! Q6 |( S| 实现方式    | 函数自调用                        | 循环结构            |
      E6 t7 g. _: B5 }$ \| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 r8 c" z8 F$ W0 B
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 l# m  g6 |7 @. S) j| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) b" ~) w8 N2 K& i+ @5 }& q! w# W  ?3 Z- N1 W! _
    经典递归应用场景
    3 j7 M1 B4 A$ v1 k$ I1 P& ~) T# ~1. 文件系统遍历(目录树结构)
    6 }) N# R5 x& H! B- d2 e2. 快速排序/归并排序算法7 K% x7 `& Y# s9 S; h6 J
    3. 汉诺塔问题
    & o& A" I8 u! s* V0 ^. l6 a) I4 n. |/ N4. 二叉树遍历(前序/中序/后序)
    - o: w' q3 Q4 y, m9 s5. 生成所有可能的组合(回溯算法)3 E6 W! B1 W+ m6 ?/ r

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

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 07:17
  • 签到天数: 3242 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    4 h' I% E# z; |0 w我推理机的核心算法应该是二叉树遍历的变种。
    5 ^4 [7 s$ X/ M2 O- h8 O另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ H3 m" i5 U. i! X5 i
    Key Idea of Recursion# Q: P' a; R* U4 O2 ]
    , g, ^0 C* Y+ H) R  G
    A recursive function solves a problem by:& i/ `# g; S/ T1 @+ x3 c

    / V* }6 K, i! k    Breaking the problem into smaller instances of the same problem.3 |& U: m4 [; B$ |8 V, v
    ( j# |3 r2 t% P3 m- v8 _
        Solving the smallest instance directly (base case).
    # D  A% K5 D/ `5 `3 k2 \! `7 s+ ?, Q( {$ B8 U5 Z( A' q/ q
        Combining the results of smaller instances to solve the larger problem.
    9 Y: d1 q, t- l3 W
    , q+ x' z, w; mComponents of a Recursive Function! |- T$ k2 c8 G+ A2 z4 @

    " @( }# O& K3 Q( ~" W* p9 j% u    Base Case:' G- q/ F: q2 ^) |5 w- T

    " w  ]& x4 d$ j' J5 c        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 d9 ^3 N) T% C9 b, [% P2 T7 _2 {3 c* Q  ?( H+ t
            It acts as the stopping condition to prevent infinite recursion., S4 I' M5 ^. ~2 y& B8 {+ @9 i+ _4 o3 x

    4 ]$ |6 Q) z: {3 @; w  B! h        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    1 _8 ?; C5 B) k0 K" y( f/ z, W; u
        Recursive Case:
    . x1 b, `5 g" S* j7 j, ^4 C+ S. R, f% y' `
            This is where the function calls itself with a smaller or simpler version of the problem.- t( J7 G" }% S' Z2 W
    ) ?& {0 S5 v: v% K2 i
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& G3 |0 _% T# C

    ; w: Y6 p. f0 Q6 p) a5 eExample: Factorial Calculation
    ! b, `: {. J9 S/ L7 y9 x3 [* J/ `, ?, u2 o8 V
    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:4 O9 M' O4 ~: Q. m# m
    & U: L+ d8 h/ T! W
        Base case: 0! = 18 I) f: ~/ E5 H- G' e
    $ _0 k, `) {% z8 O0 A7 d% J  P( g
        Recursive case: n! = n * (n-1)!
    : E6 x! f1 I# W* V$ E) n$ v8 E
    # ?4 B) w) q  t+ a# V# ~Here’s how it looks in code (Python):) t7 ?9 H! F! Q% t; l% I! ~" Z0 W  @
    python6 d9 r: X  I/ L& a9 ]" r

    ) a, N7 n  b$ l, N* ~
    % m4 y- f2 u; ?' D" R- h+ n& ydef factorial(n):8 D+ g; r0 \. K# @6 q) n' f* Y
        # Base case
    ; N: W) z; ^1 n1 K" @, `    if n == 0:% I+ Y- S  I; D1 `+ X9 @  ]
            return 1
    " E: y! S) w/ n7 z3 ~& n8 ]% u    # Recursive case  Z  Z$ `8 w& e2 n1 f1 S8 H* O5 O
        else:* ]. h1 [, \  n; f- O+ \
            return n * factorial(n - 1)! q! G2 d  v7 }8 x

    ( L' h, h; Q/ `" D# Example usage
    * K5 s; c; A' hprint(factorial(5))  # Output: 1204 I+ ^2 m1 H! U* g9 ~
    & l; v: V8 [6 c7 p% t! N0 e
    How Recursion Works/ C0 j1 S) x- t- ^

    ! s) y- c0 u) g) ~5 |6 L    The function keeps calling itself with smaller inputs until it reaches the base case.
    " s# S( G5 x- i
    3 u8 m: U( j$ R3 M3 ]- t    Once the base case is reached, the function starts returning values back up the call stack.
    9 j" Q" s/ U/ L
    & G( M& _6 x! ]    These returned values are combined to produce the final result.
    2 u6 ]2 P/ G8 C1 I) y( J- T5 ^" y+ n, _! C1 G
    For factorial(5):
    4 F% Q: h, P  T9 }7 U7 Z
      T1 n' k8 L; j
    2 I! m) N' W: i; o& D! b  hfactorial(5) = 5 * factorial(4)
    $ D' Y6 c) M; s) s5 E7 Sfactorial(4) = 4 * factorial(3)0 [9 w0 I4 [0 ^7 x7 h( G" H
    factorial(3) = 3 * factorial(2), o- m+ ^1 M1 T# Z, r3 Y2 k/ P
    factorial(2) = 2 * factorial(1)) s$ S/ u5 V/ s2 r, _+ h% ~/ S
    factorial(1) = 1 * factorial(0)( S. T0 t* |# K- [: K
    factorial(0) = 1  # Base case
    3 v0 v9 k/ N# h
    1 P# B% V6 H: s- ?/ WThen, the results are combined:8 @' B6 f& j" |! e5 X! W' E0 \

    ( _) O$ b. r! z8 z5 x( K% Q$ A! u6 _
    factorial(1) = 1 * 1 = 1
    ! h9 p, M9 S6 j5 Q8 B" u7 tfactorial(2) = 2 * 1 = 2! c! X# l& q7 f& ]- X# \
    factorial(3) = 3 * 2 = 6" ?3 o# b; {% O$ |
    factorial(4) = 4 * 6 = 24
      d( ]5 U- |0 K; nfactorial(5) = 5 * 24 = 1208 T: B0 \0 q, K" i: E$ s6 ^

    8 _/ a$ Z" J( W* A7 `Advantages of Recursion
    . C  f4 V3 J" s( C0 k" m& x
    2 R, _' y8 y. p4 \1 W    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).
    - G! x% j6 R9 O% B! C! Y& V! _* L, H3 x9 @, }2 u
        Readability: Recursive code can be more readable and concise compared to iterative solutions.9 c" \; V/ _2 i2 s  [& t

    9 I" \! k4 w2 {, ]Disadvantages of Recursion- y( a7 N& e, ]' d" [

    0 H' w) R2 w! J2 H. d1 N5 E    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.
    & X: N+ E' v; x/ }' U. }; a, r/ @
    8 V' r1 {1 x/ s) S. j    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' n; d6 L2 x0 M% P, B" p) k0 T6 E, G# r/ B
    When to Use Recursion
    " b7 |  _6 P9 \: N
    + P: T( l8 i: @    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).; ^  U- U; v2 g) r9 M

    ) n4 ~' _& b! D5 _    Problems with a clear base case and recursive case.
    + `) d2 J3 }. K. d% Y1 r( z; b; N: N/ A" d4 ]% [6 e$ S
    Example: Fibonacci Sequence8 Q6 M# O7 L; r  ?0 g
    1 o/ r5 s7 S2 `* b* p; M" s1 V/ ]8 p
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    . X1 l- w) t9 J( z* T) m  f  `/ x9 j
        Base case: fib(0) = 0, fib(1) = 1
    , M( X2 e  g, [! `3 I$ ~- |' [! I6 w4 C; `1 |2 C% }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ w7 p( f( p% P! g# [7 O
    # u& L0 m! `) L: D% Q- F! a
    python  E/ W; ?* v' e* ]  |

    1 ?8 G0 f$ v$ l
    : z% h( S5 m2 b! s7 }, }' I. M( ?. mdef fibonacci(n):2 r2 M$ |; q8 H6 G
        # Base cases9 ]4 N4 T# v# B0 p' {+ a5 q3 M
        if n == 0:
    5 Y( l, |, ~3 Y1 r0 C/ @3 E        return 0
    8 Y. T3 F# Z0 ]$ I# Y/ M2 @# B    elif n == 1:
    ; z  n( v$ F8 i* r( |, ]! F  }        return 1
    % b  `$ r1 M0 Y. E& }* H1 F: d  m    # Recursive case8 l& H0 ?7 J/ P- G+ l, [# l
        else:( T2 ^- v8 r, A% T
            return fibonacci(n - 1) + fibonacci(n - 2)
    & k8 Y. L( }, ]1 r8 V4 x1 w( v/ _9 B4 }
    # Example usage6 K1 G+ X1 b; m. e" \
    print(fibonacci(6))  # Output: 8
    1 v- o' {7 T  v. H9 a7 c6 c7 Z9 r
    Tail Recursion7 f. S- A: Y' q- G, }, y$ e- S/ F8 A
    $ w$ k  |$ X2 D: r& j$ N. V+ 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).
    5 O) {0 }# c6 ]% k$ ~& G5 C* z' j$ t) w( ?" f6 c
    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-19 05:55 , Processed in 0.071248 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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