设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 : l* @. i6 w( }1 b4 A0 @

    2 t" N/ J. H, b  S* ?解释的不错
    9 K9 |/ O3 y8 j4 Z3 g0 f4 S1 z: P6 j: O) D# |
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。) x! w* E, F; n5 K0 ^" s

      x- R4 R2 q5 B, c2 L 关键要素
    7 p, d$ z5 a; c; t6 Z5 m2 l1. **基线条件(Base Case)**
    6 a% c+ Z; q& [7 ~. D' y   - 递归终止的条件,防止无限循环
    1 K8 X( v2 g6 N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1& Z8 D1 ?* o$ F; ?4 ^: a! [9 F

    : c% @* v% t: j9 J2. **递归条件(Recursive Case)**$ c) y3 c) G; Z! `, @# k0 _
       - 将原问题分解为更小的子问题" z4 }& A7 N7 C6 z7 t4 W# i2 C8 ^
       - 例如:n! = n × (n-1)!/ o4 f! w( M; F+ I. _2 y% \

      l% P) ?& D& c 经典示例:计算阶乘6 P7 Q5 F8 ~- l* _
    python$ ?' C; a% V) Z/ d" `/ }
    def factorial(n):; O( o- e5 ~6 ]* m( L" @# n# x
        if n == 0:        # 基线条件, I# I& V1 H' L6 Y) J
            return 1' _9 F: I$ ]! Y* q! ]% g
        else:             # 递归条件
    ) r1 d  t. b& k        return n * factorial(n-1)
    # t+ S9 ]5 `/ s7 U3 @! C8 H执行过程(以计算 3! 为例):
    ! I4 M6 `+ g) h; m# [, _. r4 Mfactorial(3)
    $ C* o" I, F6 ~3 * factorial(2)( Y) @1 ]/ F2 w. @
    3 * (2 * factorial(1))
    6 }3 p) A( u4 D6 f3 * (2 * (1 * factorial(0))). T& J  X, z3 w) N0 x7 R+ |( r- Q
    3 * (2 * (1 * 1)) = 6
    $ y/ A3 \6 J& H7 Z( r4 P
    9 i( [/ F) ]5 \+ s 递归思维要点8 l& L" U& S' @3 w' ?. Q( |
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    " S9 q) e8 I4 f" j! O6 {0 J. W2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" N9 X9 Y( {4 e, @# M& ?2 q
    3. **递推过程**:不断向下分解问题(递)
    , d0 Y$ h0 u* k2 q3 Y  C$ z- h4. **回溯过程**:组合子问题结果返回(归)
    * l! S% P. J% |; e5 [8 x& p4 c+ p6 D5 w
    注意事项
    % _6 d$ L5 k0 l0 \% p# M0 }& `必须要有终止条件- a; }7 H/ ]5 k, y( @% j1 A0 c
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)( z2 r6 F. T2 W! s
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    5 X3 A; z" t: c6 `  @: q5 b1 o尾递归优化可以提升效率(但Python不支持)
    ( w% K* X! N. I& n, R5 I  Q* B4 {. k$ K5 Y7 e) X, B
    递归 vs 迭代
    : b$ Q$ x+ x3 p* X|          | 递归                          | 迭代               |. i- L/ w/ @8 I
    |----------|-----------------------------|------------------|+ o1 A! P, d9 e
    | 实现方式    | 函数自调用                        | 循环结构            |0 }/ {; c6 D7 b7 m; ^' k
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |! ?- U: S! |% I" [. W& {0 Y5 R
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ }, _, O8 t+ C  X( m" T
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    4 h# ]! h* N1 U& D, }9 i
    2 g1 ~! g1 H+ U: R; s& o, e 经典递归应用场景
    ! i  i# B. }* G6 \7 V) a1. 文件系统遍历(目录树结构)9 k1 P7 Q9 d7 X* j+ E
    2. 快速排序/归并排序算法
    3 |; y( A1 T  j9 ~$ n5 o8 B3. 汉诺塔问题
    ' b  M- A% Z) u  `& y+ D7 E4 ?4. 二叉树遍历(前序/中序/后序)& a; [. A+ \. I* W" h& Z, x
    5. 生成所有可能的组合(回溯算法)/ [& |* c5 f+ q) ?/ z* t; Z
    2 |4 m! k. _" D1 w8 i  u
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    3 小时前
  • 签到天数: 3118 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, P. Q8 X, l5 t5 c- S& g" W
    我推理机的核心算法应该是二叉树遍历的变种。" L8 Z+ ?: O; i2 V* z2 T5 F, 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:
    7 O( @5 y" k; b7 s( X' {# `4 `3 mKey Idea of Recursion
    5 @' y1 e& [( V+ A' Z2 l% z6 \
    : {1 N1 v# g* `A recursive function solves a problem by:
    % g% Q, ?2 S# A  j, P& B% V" ]$ w$ o. z0 q/ ^
        Breaking the problem into smaller instances of the same problem.
      D2 [5 O7 j& f. ]% ?8 U$ d  m/ C/ L" Y5 V9 w0 |/ O
        Solving the smallest instance directly (base case).
    / q* d+ }! B  g! l( Y& r4 w# G* M+ r! u% J0 t
        Combining the results of smaller instances to solve the larger problem.$ j0 `2 D  r. _4 }8 p$ {

    ) b3 U. P. z' Y6 W+ J  k  `2 }: pComponents of a Recursive Function
    " @; F$ [* O9 s$ V* F! ^2 Q, x# U2 L& g% K3 I
        Base Case:' `' Z! v) m  p; K

    4 i) |0 m2 M4 e" R/ l        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 @7 z' m* v, F6 F1 u# `: {+ N+ Q0 P8 q2 n& j8 |
            It acts as the stopping condition to prevent infinite recursion.3 o. o" n4 I: S  W& ?3 a7 K
    . c1 z* \" t5 S
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    5 W0 i; B- E' x+ r' R0 ~
    ( X4 a" G* L* ?2 L  a$ x4 D    Recursive Case:8 W6 A) _4 ?0 ]/ P" P
    8 h- X. b/ S4 s0 _. t
            This is where the function calls itself with a smaller or simpler version of the problem.
    ) l7 \: u5 B3 h) E% c4 ~: K: u
    0 \; r: R' A/ u( N+ z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# g4 w& j1 K" l( Y2 O

    7 i& }( e* n1 @) J4 D( t* CExample: Factorial Calculation( d+ v; [. ]& X( ]8 o' n

    0 F( \0 t5 U  d3 lThe 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 N  J7 t! _. y  F" h. N8 f5 G
    $ c% h& h( W% Z3 o& }- V) n/ T
        Base case: 0! = 11 L2 }& ]+ N! f- I
    & X0 f0 \0 [, J- A  m
        Recursive case: n! = n * (n-1)!$ B' o2 x' R! ~) o; t8 y& e$ k
    2 j6 ~  Q% g  W4 n" @
    Here’s how it looks in code (Python):4 }8 z* s( ^1 B* H2 Q0 t
    python
    - W3 ?. a4 e# b& ?+ O4 ?/ m% Q; i2 P% F6 l) [1 }" [
    2 ^! o+ T1 d1 `3 @% K& v
    def factorial(n):& A: I. i1 K( f0 b8 d+ W( t
        # Base case
    & y, p7 W4 }, Y# v* p1 ]    if n == 0:
    ; _' w# h2 z6 ?; Z, u8 C        return 1
    . r; _6 Y4 V# b1 O; h3 v( ~    # Recursive case
    , @, S! \, v0 H7 M    else:. f' D: K3 X4 K2 }
            return n * factorial(n - 1)! ~$ @5 K; p+ ?
    7 G. E0 B2 u" V8 x, h
    # Example usage
    $ V3 T$ E. y) ?5 i: sprint(factorial(5))  # Output: 120
    5 h* f* d% g9 u* S- q- _5 Q# b* J7 ]' u
    How Recursion Works4 z/ ?: s' }# I3 |- c& ]; A

    9 s2 L  T5 z8 o8 k# I    The function keeps calling itself with smaller inputs until it reaches the base case.
    : h4 Z& i  W' u7 N4 Q& n; N$ b* j' r' r  |% q4 F, c7 q1 s
        Once the base case is reached, the function starts returning values back up the call stack.
    9 n, ]! C; v3 P, k4 h, u8 ^+ V. d3 }/ I7 M1 E5 @
        These returned values are combined to produce the final result.
    8 H! Y4 J. \2 j$ }# n4 s5 s& A% q" R
    For factorial(5):* O2 J+ ~& j# \7 W) k! c6 `
    - M8 f  r1 [  I* h- _

    3 {8 r* d0 U' |9 Dfactorial(5) = 5 * factorial(4)/ S" g/ ~" q1 m2 ^. F
    factorial(4) = 4 * factorial(3)
    6 A, h  E/ t) v; |+ ^$ xfactorial(3) = 3 * factorial(2)
    7 v0 \1 V2 g4 M! efactorial(2) = 2 * factorial(1)3 n  d9 H- G4 K/ e  P
    factorial(1) = 1 * factorial(0)
    ! h* S6 ?4 y2 f5 Z0 k; Gfactorial(0) = 1  # Base case2 b( N. v3 A6 c" ?
    9 m* H1 T: ^* v
    Then, the results are combined:
    , I5 g7 T: k. Q- ~
    + j7 ^0 u4 p: o  a: s' R* {- Y$ U& [! b% F. q6 e1 H0 @
    factorial(1) = 1 * 1 = 10 I" e6 |, G$ i; ~
    factorial(2) = 2 * 1 = 2( f$ e' L4 B7 a9 t5 b# b
    factorial(3) = 3 * 2 = 6
    $ i  Q' R/ v+ e% t5 b7 Z6 X( qfactorial(4) = 4 * 6 = 24
    & x. J/ o$ i* ~2 _3 j3 Z9 N% yfactorial(5) = 5 * 24 = 120% G5 l8 _% p1 n; H

    , @- D  L6 r. a! rAdvantages of Recursion
      e1 u4 H- I% V$ Z
    / f) c# M  \, v  U    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).
    $ H) P- q( S8 c# _
    9 a  a) n# ~4 {5 p1 _$ Z) U    Readability: Recursive code can be more readable and concise compared to iterative solutions.- S' r$ v: ?& P- J+ d* p

    , ~0 u( k% m" t, d! W3 a  VDisadvantages of Recursion0 q' b  K. s2 m3 p! R
    ! `$ C' q( |$ ]! x7 w- S' f/ R
        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.' E0 J5 Q8 Q% X% y+ Y! o: w9 c0 ^
    / z! j! c- X" g
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    3 K: \5 d$ P, {+ l0 E8 j. s
    $ K3 Z& ~! U  h+ N2 |+ N+ P  _When to Use Recursion
    : }; {+ G! r6 i& ~" A0 n
    ( }8 P; c( D+ P    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ! r9 v' u; A( l. t$ Y1 r' U) m" i+ @7 ~5 F
        Problems with a clear base case and recursive case.  @" W2 ^- t$ m7 R6 @2 n! T) E
    $ G. W. z* j$ E9 x  W
    Example: Fibonacci Sequence
    , Y( W( Y& E# k, N4 m+ y5 G7 K" p; k0 x( ^6 [" P
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    4 g+ u* r& e6 X5 v* T' u- S+ p5 V6 T" L0 x0 w
        Base case: fib(0) = 0, fib(1) = 1
    " Z9 T3 z6 F. x
    / a; a; O/ X! H( P    Recursive case: fib(n) = fib(n-1) + fib(n-2)( X' V- R- X1 i$ A

    $ [$ y7 G3 p! o/ z0 qpython  M6 J/ C* r0 U0 o4 {1 H
    % o. U: g7 T3 {+ T4 V
    & I* k& Y9 I; @; B% l  F
    def fibonacci(n):
    ! z4 m1 t2 m8 R- r  c/ v    # Base cases
    7 Y; l' D; B1 k" o( f    if n == 0:
    * f! e) K) c0 G1 V8 s. L5 a        return 0
    4 O% Y7 K3 f! S5 \8 U1 ]$ G4 q1 {    elif n == 1:4 T4 Z2 F7 a, v3 Y5 [; w' G+ `& P( J
            return 1- b9 R; k4 M& d
        # Recursive case1 h6 ~4 e$ K& _# M  |  S7 X
        else:
    $ ~! M! s% }# ]  @  B        return fibonacci(n - 1) + fibonacci(n - 2)% B% a3 `# K8 D8 x$ q7 ?! f# I# c

    : }' A5 ^& x7 {3 G- G# Example usage* `% N% W5 Z% z+ F: {) x* x
    print(fibonacci(6))  # Output: 8* l3 n8 |' I% o2 J

    ( i, S) Q+ ]7 Q& g1 O- J+ @9 N! x3 [Tail Recursion6 P+ c) \7 y- y. h. t. ]1 P" i! ~

    ) D7 y8 F$ n+ T$ C" T( |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).
    + ?8 q/ t3 G: N. Z( C% [& ^" }' C1 y+ G! J9 R& v% H+ 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, 2025-12-15 10:32 , Processed in 0.031640 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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