设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + u1 x, ^* Y! w8 r9 i) @' @, x

    7 O" F" \( i! s3 \2 k解释的不错3 M3 s/ ?5 |5 e7 W6 [# ]2 J) S

    $ S$ p  a, |+ Y" Z/ N递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 p- x7 h& C8 A& A% V7 Q

    " {1 R, ?4 t% x( G 关键要素
    ) l$ p% r% L' x8 s6 `9 t. C9 h1. **基线条件(Base Case)**
    & P8 ]$ n0 C. V! u2 y% {   - 递归终止的条件,防止无限循环
    3 y/ z! G- q7 s- F9 C   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1) l$ e2 |% c! \+ `6 r+ i% w* H

    : ^: C1 F; ?% w9 z( x  I2. **递归条件(Recursive Case)**/ L% `. W8 }: f) v! H, X- N
       - 将原问题分解为更小的子问题
    ! W8 o. t4 f7 T1 \2 c9 }   - 例如:n! = n × (n-1)!# c% e8 F( |) t9 W4 }" V6 c
    4 t$ q$ x8 r8 D
    经典示例:计算阶乘
    % h3 H' |; s6 R# o- |- o2 mpython
    % |; {! b  Q2 [' }/ Z1 mdef factorial(n):
    7 R4 R+ Q  [' i  O; F( ^1 f    if n == 0:        # 基线条件* w1 e+ b# ~4 n) H. g
            return 1
    8 j% d4 F: L5 z$ K! |0 }; e( h! s    else:             # 递归条件
    0 ~4 H& s8 i# ^4 b! o2 @        return n * factorial(n-1)# a( Y+ K; m( e, ~% T9 x5 p
    执行过程(以计算 3! 为例):
    0 o$ W* B! `( W9 _factorial(3)/ |0 b$ N: T0 Z4 b9 ?, F
    3 * factorial(2)
    ' A! g0 ^: U6 f, x' W4 B$ b2 K9 |3 * (2 * factorial(1))$ s8 i. ~* o; x" V1 F  Y) i# z
    3 * (2 * (1 * factorial(0))); \, C8 M3 D9 _( D
    3 * (2 * (1 * 1)) = 6
    . W: E# `) Q$ z: h4 q1 w1 P& d+ ~! B7 k  g& D7 V% O& e3 W* r3 ^
    递归思维要点. X6 v/ P; n4 v' \7 y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    0 v: D' o3 p; Z- o; {# U5 z, s2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    # G9 ~5 M, i: v6 p# H( ?2 L( Z& B4 a3. **递推过程**:不断向下分解问题(递)
    % Q, @' W, s% l* ]4. **回溯过程**:组合子问题结果返回(归); h0 G5 e  \, ^0 g
    . K( V6 Y. l1 y. t& }. q- f! ~
    注意事项: A3 v, _8 y# l/ i
    必须要有终止条件
    5 S2 d7 r2 w' K0 k1 l9 y递归深度过大可能导致栈溢出(Python默认递归深度约1000层). \, k# ^% J, n4 g7 q& e. t
    某些问题用递归更直观(如树遍历),但效率可能不如迭代* z7 T2 }# G+ E3 s* H
    尾递归优化可以提升效率(但Python不支持)
    ' O. s2 R: c/ m/ Q3 q' o1 R5 x) p7 |8 [1 u2 N5 K$ E3 w  R
    递归 vs 迭代% G6 [& c! n; P' k( ?8 P
    |          | 递归                          | 迭代               |
    # L9 w! m: }3 L  ]% G0 V! G$ M|----------|-----------------------------|------------------|
    $ R2 k* [' q+ O4 i" x( ]1 E| 实现方式    | 函数自调用                        | 循环结构            |
    : i1 K+ N% J/ ]# a* p9 b9 k| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 c) a7 t1 g, v+ ~% \+ c
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& A  j# e/ @% m6 H; m
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ' d+ v! _) O$ H- M
    ' q& m( M  ^2 v+ a  M, u 经典递归应用场景9 y2 K% g( e9 r0 q3 i' i7 {; K* G0 |
    1. 文件系统遍历(目录树结构)5 o( T- O. T/ k8 g& j
    2. 快速排序/归并排序算法1 s' J' ?' Y3 N) r- W8 ?- x* z0 A
    3. 汉诺塔问题
    3 H  k) h! X3 c' U* j" t4. 二叉树遍历(前序/中序/后序)
    7 _: j/ I4 V* k  O. u1 o5. 生成所有可能的组合(回溯算法)
    " w+ H0 B- K7 f# L; y6 U- D4 o* j# `* W- q
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    半小时前
  • 签到天数: 2967 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% d- [, E# ~- l; e3 ]! A
    我推理机的核心算法应该是二叉树遍历的变种。8 T# W/ X& ?% f1 o# s; x- c
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:- H. i. ]: Y8 W
    Key Idea of Recursion
    " d8 S* R3 x' w3 L! s* `/ K- D7 r/ R
    A recursive function solves a problem by:
    ' Z; f, [* `" G8 D/ ^5 f' A# O' Y( f4 ?- _- b# v
        Breaking the problem into smaller instances of the same problem.& E2 ?- @. j6 G% b* g8 A

    , ^$ N7 S5 R% \: R6 S7 F    Solving the smallest instance directly (base case).$ I& V$ ]+ P1 }6 ?& Z% t
    4 ~. U8 _& `& B$ b0 g# W! v
        Combining the results of smaller instances to solve the larger problem.
    % c" M9 C( z/ y$ |3 {& E% V+ |& T
    Components of a Recursive Function7 r+ [3 @( q) w- o9 Y' y! q
    + X" V# b1 R9 r! O/ `" r: Z. u
        Base Case:
    2 \5 V1 O% Y7 e! f: F/ i! F- @' T
    5 D( {7 z6 t( m1 y2 j9 J4 H        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # f7 g! X, Q  h* y  V+ ]; P
    0 d% L! E/ ]+ U( Z- Y% J        It acts as the stopping condition to prevent infinite recursion.
    - ^9 Z/ J8 g' z
    % E1 p& i- N9 A        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' `6 n2 `+ V- |7 e8 i! N# `# \& l8 S1 D- I9 p
        Recursive Case:
    ! ?+ B- B" f$ ]% C9 s/ G+ B3 x0 i& B- }: C8 A
            This is where the function calls itself with a smaller or simpler version of the problem.5 _" Z! s% f. d: S5 w
    % P9 a  P: }3 y3 @% N$ R% s4 s
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 J5 E$ T' H3 ]3 K+ [: H( L) P# V1 A* k2 D. N3 j. f$ i. I
    Example: Factorial Calculation8 u  D  o5 R. Z, }" w

    ) O- g0 T" `, B* rThe 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:. G) `8 m- N/ @3 F
    # ^, D+ G$ p  H( P$ q1 X
        Base case: 0! = 1
    % g% j% |5 ]/ \5 g6 N; J, l
    & }! |) L0 e7 y1 {% t/ {' {    Recursive case: n! = n * (n-1)!+ k1 T' R. E) v$ Y" F7 N: q

    : `  r8 y) ?6 p" J$ W+ WHere’s how it looks in code (Python):
    " }/ ]7 {. G0 s' xpython
    ( B8 S6 n- G2 ?2 A: E
    1 p1 L. t( o7 `5 S2 U' o
    9 x- e6 b3 N) w9 u  Idef factorial(n):4 e! q6 R+ M# ~
        # Base case
    + R. {/ M  [* l    if n == 0:
    0 _& d/ i: G; y4 v' k8 P6 y        return 1$ p  A# v" v" {5 Z* |9 |
        # Recursive case) s/ f+ N4 M4 m8 u  o' ~- M/ C
        else:  u4 f+ Z( d" e# _, B: M
            return n * factorial(n - 1)% l& A% m7 h! a% x

    $ [6 D! q5 u% x) u3 {# Example usage1 j0 S! ]5 [+ o3 |( }, [( C2 |
    print(factorial(5))  # Output: 120! j+ g% p0 a5 ^' \- I

    , b9 M) r- G8 x1 i- G: p4 I; IHow Recursion Works" y7 h7 J) P/ j

    1 m& v% r/ k" L' z8 D& _0 Z    The function keeps calling itself with smaller inputs until it reaches the base case./ O  L( F! g6 `) y: e2 G

    . V  m$ G, Y: C% R6 F" b/ o    Once the base case is reached, the function starts returning values back up the call stack.
    + x& ]! i+ ~; b- G: `, z) N% G7 ~+ ~% t; L
        These returned values are combined to produce the final result.! t; g: Z9 T0 \
    1 M( X4 B' F; Q! j" ]
    For factorial(5):! `5 @6 b# ~( e5 Q
    - P  |. }: x- x2 s* |

    8 ]/ u1 n. |7 J6 K2 Cfactorial(5) = 5 * factorial(4), Z' T( K! A# q8 V2 N- X
    factorial(4) = 4 * factorial(3)
    : Z6 u$ v: d$ v  _! w* @3 Lfactorial(3) = 3 * factorial(2)
    + ]& |, w2 z  _3 U% pfactorial(2) = 2 * factorial(1)3 }  ~  w* Z4 u* r8 H
    factorial(1) = 1 * factorial(0)
    * C7 I" [# e4 z( tfactorial(0) = 1  # Base case
    # u: T8 p, U  E( Z$ ?
    $ }2 E2 R* H' ?/ z, d& E" xThen, the results are combined:
    * m* O4 F! U; k& i# t" V$ [: j
    7 z, }) P( t. n) N4 y( c$ D6 V
    $ w% j, f3 D. K9 Dfactorial(1) = 1 * 1 = 1
    ( i2 e: u9 O+ gfactorial(2) = 2 * 1 = 2
      Y' P6 J8 h/ c( I) h6 wfactorial(3) = 3 * 2 = 65 A8 k' K& ^& j- t# O: f, h
    factorial(4) = 4 * 6 = 24
    . y/ `. m2 Y$ g/ p3 k( |2 Rfactorial(5) = 5 * 24 = 1200 \/ H6 L7 C* t0 E! b' C
    % T# j; ~& L! p" d# O
    Advantages of Recursion9 m' t* _3 Q: F. L, @; x$ ]  M
    + d; K- u4 p$ _; ~! N9 |' `/ 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).: T8 W4 w4 V3 O  w
    % ]8 p6 f6 m- w0 O9 l$ Y# q
        Readability: Recursive code can be more readable and concise compared to iterative solutions.5 a) N! ]. ^8 ^8 u/ E
    0 X- h8 l- t' v3 g- X5 d' y. ~( v! o
    Disadvantages of Recursion5 k# a+ t( u- I- T3 J

    ; P% E6 M8 \) t4 ^5 L$ y5 Z    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.! w/ j; ?9 {% r2 f/ T6 o
    1 f% I( y- F: `) t5 q3 c
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    5 j- |% s/ ]$ \6 m$ k
    6 r- X5 Z4 c) y; |: IWhen to Use Recursion4 @3 X0 e4 j3 ^% P5 M3 ^% N: P) R

    2 Z+ Y# K" j1 K. v# Q: {9 I) F    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 y1 c8 b6 q! c' x6 U4 O$ P
    ( C, S8 H! W1 N9 c% \3 u5 r
        Problems with a clear base case and recursive case.
    8 U5 L( M  \: _& v6 Y% h6 n4 M: u7 ^& W6 I  G/ D
    Example: Fibonacci Sequence
    / D5 o) [9 Q$ S6 M4 p+ x( N1 A# q1 @- a; \, h1 n  i
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:% K+ ^. _; @% L( `
    / p* b+ ?: q* Y; X" v
        Base case: fib(0) = 0, fib(1) = 1* m# @# k8 e8 O% e
    : Z9 y) J  M& n! j  _; C) ?5 @
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    : r5 }& C# B0 z5 s% j4 a4 t! o1 C. L" @; H( k5 N0 s; T/ E
    python
    ' @6 h* z6 U1 C* t, r% h: F
    : x! g) b! Q$ W& w, ~
    / W! b; J; \1 B8 {, ?def fibonacci(n):7 V2 r% ]8 Y' h- c& W8 R6 {& F
        # Base cases: ^- y$ i0 u2 j$ s; j
        if n == 0:
    ! `7 M- Q+ E+ O$ [        return 0
    , B" z* k0 D8 U: o% L4 g    elif n == 1:
    / p3 {- {" T, N8 z2 n! [" k! C4 ?        return 1' e0 W- F6 x( U' G1 p$ n
        # Recursive case
    . \$ `" T& R5 W) ]    else:
      i$ a/ ?# _0 K& z0 P2 r1 v        return fibonacci(n - 1) + fibonacci(n - 2)1 c" z4 L  n1 i& r. [$ z/ P
    ; l& ?0 J5 w$ F' s% `
    # Example usage1 l/ D* @% @/ D+ v4 v6 z
    print(fibonacci(6))  # Output: 8
    ( F5 b0 |% a5 W2 |# C9 j) S1 h: n3 v$ c- @; P2 c
    Tail Recursion, a+ u9 M, C" P5 z0 ~
    $ W4 y1 R' H6 j. E7 W5 a- k* C$ B
    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).$ B2 L, h0 n. k
    0 U9 \1 Y. v$ i+ C# P
    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-7-1 06:45 , Processed in 0.036880 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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