设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    + u' D4 t& j  E' C  f! \$ J# t' y0 r
    解释的不错
    " b( \6 x* ~* l3 p; e1 e
    ) C- J/ ^; A8 X) C) r+ C( |递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。* X( k" N& X) {* z
      D; L1 w- l( w# c- C! `
    关键要素
    ( i5 h3 C% Z0 n1. **基线条件(Base Case)**
    ( @; \% A+ H5 p/ N7 e/ v/ h   - 递归终止的条件,防止无限循环
    ) r1 `1 V0 a: Z6 h/ X8 Q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 17 J$ [3 u6 I, o+ u  x
    . X3 F/ Y4 V1 z$ X' [) R
    2. **递归条件(Recursive Case)**
    + W; E3 z& X' @3 r, e  H   - 将原问题分解为更小的子问题" E8 E; k& h7 W+ D, j
       - 例如:n! = n × (n-1)!4 H2 l; @4 b2 j# q/ @

    6 C7 ~7 I$ e$ p, ^( a( |. B 经典示例:计算阶乘
    " Y! K9 o, ]$ fpython% Q% u" Z: C8 l( l1 A# b. ~
    def factorial(n):
    $ u' ^9 S8 Q, _7 C# v* i0 Y1 r6 l    if n == 0:        # 基线条件
    0 v# |  i  x; ]) }8 e: }( Z        return 1
    ; n6 ?7 @3 I6 W0 r5 Q% V    else:             # 递归条件
    5 B- a* p0 }! A; R, v5 J9 E        return n * factorial(n-1)4 p# i: p3 S5 D* P, g
    执行过程(以计算 3! 为例):
    5 W" ?/ _9 t& ^5 |8 Q& Lfactorial(3)
    & [# [, O3 }4 V. B4 |+ Y. `; j3 * factorial(2)6 g3 ]! G% S! o+ W! k
    3 * (2 * factorial(1))
    8 g% o) n" j& M" l3 * (2 * (1 * factorial(0)))$ f8 O/ a3 c! G5 W7 k+ b+ ?
    3 * (2 * (1 * 1)) = 6
    ; _; y6 k0 c# ^# u7 Y0 n
    & c& M- T( t* h 递归思维要点! a* n1 p+ Y, _- q0 s
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / t: Y! q0 c* R, t2. **栈结构**:每次调用都会创建新的栈帧(内存空间); M% x/ v: w! H8 [: T
    3. **递推过程**:不断向下分解问题(递)4 P# t( M( J$ V" Z# k" b% r
    4. **回溯过程**:组合子问题结果返回(归)9 b; s+ e% S  v% O% `$ j
    8 n  b4 o! w* ?# Z& i$ j; G7 @
    注意事项
    , u, w8 d0 N: \必须要有终止条件  ]( h1 s6 D, ^. r3 e  c3 j
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)6 y0 F: d, i& o7 O
    某些问题用递归更直观(如树遍历),但效率可能不如迭代/ }* k/ M6 J# ~# @8 @% Q! }  C8 X' A
    尾递归优化可以提升效率(但Python不支持)0 J3 U( N5 P7 G/ j& l8 |# K  Y1 I
    ) e3 R$ J& a% b% Q3 _7 e
    递归 vs 迭代
    0 c; g3 T5 `- T* G8 O0 r% X|          | 递归                          | 迭代               |! E# E4 l$ h% U0 k& D; o
    |----------|-----------------------------|------------------|, z4 {( P+ I8 z. X; ]# J
    | 实现方式    | 函数自调用                        | 循环结构            |. l" `0 q$ N3 D9 Q
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    2 e, K# Q( t) f| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 B  M3 Y' M$ i# T/ S$ i5 W6 P| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |' t- F+ Q: }1 k2 f( s! g3 L

    / f0 q* u8 C5 a" F) B( q: D( M) g( h 经典递归应用场景# c* S* p. Q4 _5 t4 {" r/ |* F
    1. 文件系统遍历(目录树结构)
    $ k$ G3 |1 b2 g# t, m- Q2. 快速排序/归并排序算法
    & j" E* d( c6 U7 v9 [1 e3. 汉诺塔问题
    2 L1 }% i; K! E$ ^5 F6 X2 ^/ \4. 二叉树遍历(前序/中序/后序), c, H: J# o- Z1 r+ D$ v  S
    5. 生成所有可能的组合(回溯算法)2 a1 [: C$ B  ^3 f2 T7 }

    ' W7 Z7 s2 e0 h# J% I' g. I试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    4 小时前
  • 签到天数: 3108 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  }0 c$ j# M, s4 O1 H# e% p7 p
    我推理机的核心算法应该是二叉树遍历的变种。) y/ K2 G8 x* s- }9 L
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    9 G" y4 C7 j0 o" |' i7 F7 J; P; LKey Idea of Recursion
    & P  Y7 Q+ x; Z  P; [' Y, B
    7 Y; ?, X5 N9 T$ f  y- AA recursive function solves a problem by:
    3 }% a+ p0 o% m: B
    6 O3 B! t2 d7 c    Breaking the problem into smaller instances of the same problem." x, e7 ]% Q  f1 V3 n
    ( I& u2 n5 X/ D) h
        Solving the smallest instance directly (base case).
    " Z$ x0 ?+ N' G1 }/ c( n7 O# _
    0 w( W7 p1 x7 ^3 V. o* i* z* p4 R    Combining the results of smaller instances to solve the larger problem.2 U/ I9 H5 h7 j3 n& M

    0 Y) Q" A" x8 SComponents of a Recursive Function
    : |5 A% E# ]: S# ^. |( I1 E4 J. f4 u
        Base Case:
    ! L8 w9 j2 B% t( G
    " M& x9 s/ G& w* P, H4 j, O        This is the simplest, smallest instance of the problem that can be solved directly without further recursion." U% E2 u8 K3 g6 U, ?

    ' s% Y6 X: I/ r4 C1 F        It acts as the stopping condition to prevent infinite recursion.' o4 N$ K% x& p
    & P, O  c% v; t3 H
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    . e. q& h- G' n+ W( _. D- d1 C* }9 S: k$ h& R
        Recursive Case:
    " f$ i; ?/ {/ F/ t- G8 F: _9 w" r  z: s# W# D
            This is where the function calls itself with a smaller or simpler version of the problem.3 W/ e7 K3 a' C: A3 Q

    2 y; |! q: U: g! k' |4 t        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 w/ J' P; w  z5 Z3 U
    7 L/ _3 S2 j& Z! u7 NExample: Factorial Calculation$ s- E. u! Y- r. v6 {4 B0 a

    6 Z( ~/ }4 B+ f1 ]. n8 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:* [, R& f! ^1 v

    / A1 Q8 U% o" \: b7 G' Y; |    Base case: 0! = 15 O& a' Z9 a( o; i) Y' a
    ) N" c; O! `+ [. f' Y( v
        Recursive case: n! = n * (n-1)!
    2 T' k6 D/ H2 X+ |& K
    % c( D! D2 ]5 M1 d$ @Here’s how it looks in code (Python):
    5 P' [( x; W+ J( z, W- Npython" a$ a5 c+ m4 h' {7 A6 h6 p

    / `4 A3 D- g% z6 @5 M6 t
    9 u" o: \" d/ ldef factorial(n):5 ~; q9 Z1 M7 D2 i7 x( G
        # Base case
    # G1 }5 o- ~2 I4 X9 Q4 i& |& x! G    if n == 0:
    7 n% T4 A  ^* f; ]        return 1* f2 i9 w% S8 g- V" a* |
        # Recursive case& w# f; _7 D) W, q
        else:/ e( M1 l; b6 m& w: {( L
            return n * factorial(n - 1)+ h' ^6 ^9 q- j/ F$ n
    0 w4 J7 J$ Q8 i& f
    # Example usage0 d" K  ~2 q, @( S) A& `! Q
    print(factorial(5))  # Output: 120
    9 j9 b0 G, X6 c! j5 j5 U; a1 n& x
    " ?- o9 M: `' Y& m, @How Recursion Works  ~$ o/ o+ O6 n; J; i. ~

    ! _3 M* G% E7 f+ `( c$ u8 r    The function keeps calling itself with smaller inputs until it reaches the base case.
    : J0 m4 f- S9 Z) ^7 j
    " x, h4 o$ B% @& \+ T    Once the base case is reached, the function starts returning values back up the call stack.
    0 @6 q& J3 }, M  Z! q$ a5 W0 a9 W8 _. g
        These returned values are combined to produce the final result.
    1 O4 i: w- n/ e8 b7 Z7 r! E5 o' \; }1 m) ]
    For factorial(5):
    2 t/ x" B& T/ H3 q
    ) ?- e# d/ W) E- m" S, g) _  S  g; s1 X5 p& b) ~4 O' l+ i
    factorial(5) = 5 * factorial(4)
    " a0 y1 y& C" u- |# hfactorial(4) = 4 * factorial(3)* @$ w4 O! c- H# I3 d
    factorial(3) = 3 * factorial(2)
    / u  N! }8 q  h' S1 nfactorial(2) = 2 * factorial(1), B  f9 b8 C8 p9 K- ^
    factorial(1) = 1 * factorial(0)/ _$ g9 A+ R* q% g) J
    factorial(0) = 1  # Base case5 w) z) T8 Z1 R  m. Z# s# w

      i; ]! Q, c4 k! IThen, the results are combined:+ l) ~" Q3 S' K+ N+ E- |" B) h3 _

    & N; _- l, N3 g' ^
    0 ?( k- E" m  ^6 S/ G* Jfactorial(1) = 1 * 1 = 1
    7 B, \3 V0 L/ v5 p% Zfactorial(2) = 2 * 1 = 2- x; F. ^7 j3 a% u$ B: q# [
    factorial(3) = 3 * 2 = 6
    5 u5 S- n8 d" j+ x: @. x- K+ hfactorial(4) = 4 * 6 = 241 b4 f% p  B1 Q9 ]- ^- `/ ~7 G
    factorial(5) = 5 * 24 = 120& F3 Q# X3 \- e! N8 |2 B

    3 {4 p" ~% k9 A$ Q' \' N; |Advantages of Recursion
      r! b# J! t7 m
    ) w: T3 D' s! D3 m    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).2 ]! B" x  c' M8 D3 F
    - i: c. V% p' `) f& s6 {
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    4 P  o) x/ p+ c( N9 O# F; m' N# u: _) i* E8 D6 \5 W
    Disadvantages of Recursion
    4 A! u7 W$ A6 V" v8 F' O( k) k; [1 o
        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 Y6 r  [5 U2 n. Z9 |

    * r& b! z( }7 T- A! R9 X    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ T+ b& G& o' i* [

    " I7 O* j7 w/ G+ a) IWhen to Use Recursion$ `6 {: \7 p- ~3 ^8 n- \4 ?

    / W5 H, q  |. ~, w5 f' \) H5 M    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 `/ K0 ~( ?) L/ ~
    5 X0 {0 Q; g/ a" x. o" z    Problems with a clear base case and recursive case.
    0 c7 X, C3 j# L, ~% z' o# U6 [
    : o- I6 A5 S% f- p! Q, O3 v* h) _Example: Fibonacci Sequence
    ) `; W% ?, _- {3 a6 B+ J$ C8 P- m
    $ z/ T8 B  {- T  U6 b; y% TThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # ~  |+ E. v2 j, P1 o! ?( l% ~1 z& ]: d+ D9 `' ?6 t- b: v
        Base case: fib(0) = 0, fib(1) = 16 X9 `- K5 ]8 x2 e) E; Z1 _! f
    ( X- T. y' x3 B" c4 h3 B0 u. R& y
        Recursive case: fib(n) = fib(n-1) + fib(n-2). |) S/ y' P$ T* M4 m+ L

    3 }  A  w) M8 [, \4 }' |) k* @python
    8 Q" t: e' G& g) ^! G) }( V6 Z0 m6 Q3 l

    ' Q+ v& ?7 W6 q& a; u5 D9 T) b( ddef fibonacci(n):$ \/ S; t$ M5 M6 {; c
        # Base cases) ~, f% Q# o) `3 U7 l: h" _
        if n == 0:) y' p7 |  V1 p( G; |1 G. c" u
            return 0: O& f( C6 v( `7 K4 `
        elif n == 1:1 G' x% p* n: Q: ]) W
            return 1  K* D7 H3 d6 p
        # Recursive case
    6 p: A3 }: |4 `; R) A- y: z9 V4 E    else:% C) r0 r  n$ m$ a# z. \
            return fibonacci(n - 1) + fibonacci(n - 2)5 A1 A* @; P! ~4 y

    " X7 S+ [7 {6 u5 d6 B0 d# Example usage
    1 Z4 T( x: Y- ^/ F2 t1 sprint(fibonacci(6))  # Output: 8) C% h$ J( z# u; @
    ' ?0 N' L( \) V  A" y) F
    Tail Recursion; }, I' `" _+ o' a

    " T8 L% U  S- }! M# e* T8 y0 MTail 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).
    * ~# M+ U0 n: q9 h8 k- w& m+ Q7 l0 _0 b! 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, 2025-12-4 15:31 , Processed in 0.031057 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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