设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ) z& D  s" W2 ?

    8 A8 v% Q7 l7 {+ g9 ?  m, M2 P) h解释的不错
    5 D$ Q2 L3 Q+ H: U. B' T. f3 S$ E; ~
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    - \0 o* X+ _/ f( |1 G0 a1 o) L+ u0 j  \! N
    关键要素2 o& O: m) Z4 |+ a
    1. **基线条件(Base Case)**
    4 f( X; p$ x' [# u1 t( U4 s1 Z1 B   - 递归终止的条件,防止无限循环$ Q) Q: ]/ z8 M$ ^% k+ `0 {
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 N7 w: o1 l7 T) ], ?) Y
    7 D0 }  K7 a0 O0 b
    2. **递归条件(Recursive Case)**
    ( e8 n9 O1 k0 I* `$ z* v' P* L  E   - 将原问题分解为更小的子问题" S5 F- V2 b% Y! h* W0 C4 Q
       - 例如:n! = n × (n-1)!
    7 R9 [; `0 ]7 s# j6 Q9 `9 W" y& w/ b
    经典示例:计算阶乘3 P" Z6 _$ g8 r3 ?) `# l  D
    python' i- I# G9 }) H( f. ?9 E" R
    def factorial(n):* k% u# {3 [6 y  M
        if n == 0:        # 基线条件
    $ n% w! [/ y: l! C; y, o. H' ^. n        return 10 y* c" j5 X! K# c% l& r
        else:             # 递归条件6 I2 Y5 m; p) l* O0 n; A
            return n * factorial(n-1)9 v4 |+ h! x# X# n1 m" B
    执行过程(以计算 3! 为例):2 {; O; u9 i: a: a/ n' i/ @: b
    factorial(3)/ i5 ?: O% o0 L9 |! ^# b& |& q
    3 * factorial(2)$ w2 |( N/ F; X% \
    3 * (2 * factorial(1))7 b2 X8 f0 p9 u; B) n" t* ]- m
    3 * (2 * (1 * factorial(0)))0 z  B9 i( j7 \% e$ w
    3 * (2 * (1 * 1)) = 6
    $ ?, _. `3 s7 \( j
    6 Z5 |2 R  s) p 递归思维要点
    & }% M. R! c* b' ?9 s1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / ~& X+ X4 o9 |2. **栈结构**:每次调用都会创建新的栈帧(内存空间)5 W9 `, g9 L6 |8 |. j- h6 R
    3. **递推过程**:不断向下分解问题(递)6 X1 e5 f* d7 w) i
    4. **回溯过程**:组合子问题结果返回(归)
    , v8 F& c( k+ `, f' u0 G# Z
    / e' h/ u4 s: ], E0 |3 |- L5 ? 注意事项
    # {2 Z7 p7 \) ?. `. C+ e5 f* h' p" Y, }# J必须要有终止条件
    ) X: h0 G) L" s# W递归深度过大可能导致栈溢出(Python默认递归深度约1000层)- ]1 p5 O* S3 k# R& M) a, o7 X
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    - P9 S: d  \: P( V' }5 s尾递归优化可以提升效率(但Python不支持)
    ; M% B; D/ d) Z4 r4 \' g- [$ ?
    , E: [" p3 A1 z, z9 l% ` 递归 vs 迭代4 e2 A& \3 G5 j' ~2 T
    |          | 递归                          | 迭代               |% X  X$ q( l& Z: f* n  _
    |----------|-----------------------------|------------------|
    ; ]) T( P5 }  c9 m/ x| 实现方式    | 函数自调用                        | 循环结构            |
    5 }2 u4 H' }- y( ?# ~| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    . y3 S- ^: f& J6 m8 `8 r2 a| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- v2 @7 J/ Q. Z$ J1 D2 |; c5 s8 N
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % `* o1 h5 m( O2 O5 G9 _4 P/ l( V( K  A
    经典递归应用场景) Z: u+ }" w1 a1 [4 K+ m
    1. 文件系统遍历(目录树结构)& d8 P3 O+ V0 n% W' s  b  {( _- |8 M- o
    2. 快速排序/归并排序算法* U) J2 `! Y1 X+ y9 y$ P9 K+ f
    3. 汉诺塔问题
    * Z" y& |/ d7 d# w4. 二叉树遍历(前序/中序/后序)
    0 S, p3 v) B7 T  j5. 生成所有可能的组合(回溯算法)2 I2 u) R1 f' d) X
    4 N: h; M/ O/ H7 d2 |
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 05:37
  • 签到天数: 3309 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 I1 [- Z+ j8 v* V; j
    我推理机的核心算法应该是二叉树遍历的变种。1 h0 x  r% }( L/ m# |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    , j* K6 G" ?! c4 y% \Key Idea of Recursion
    $ H2 m) E0 {+ l/ T8 K  ?
    , F4 Q( p& N! WA recursive function solves a problem by:3 \4 q; T8 `" z8 G% F

    * A1 l$ G" G& S  S" ^" C1 R    Breaking the problem into smaller instances of the same problem.
    1 k" i8 j) d1 T; _" E( ~! ~2 x: E% G9 b8 p6 E8 |* J
        Solving the smallest instance directly (base case).; Q) D$ O0 ]2 I" Z$ `9 m

    : M, ~. c9 \& z0 A+ q+ S    Combining the results of smaller instances to solve the larger problem.$ _' x4 Q) E  o, K( j" @/ `5 p8 \, [6 A
    ( L: r5 k: H. L* `" K# q8 a' w
    Components of a Recursive Function
    1 a' J# C' G# d! _  u+ ], t
    5 Y- y! k. s9 \( h# E3 K- I    Base Case:; D7 G( Z+ e4 z, x( [

    ' V: m" o  P6 ^2 {6 j5 ^' j        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    " T7 p# ?& Y' s$ M3 ~# m3 C) x, M! n. g
            It acts as the stopping condition to prevent infinite recursion.
    - d2 h% y+ J- J6 \+ z
    - b* w" ]. v, Y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.4 @7 i5 Z5 ~, A

    . X# d% s8 a! @1 `    Recursive Case:
    9 d6 O: D" k* ~4 d* j5 ]
    $ D' B) c) n0 a( Z        This is where the function calls itself with a smaller or simpler version of the problem.
    ; G6 x) a0 k; ~  m4 x  r" P
    / V  U+ `# v- f        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).% S1 f  W0 G% }

    4 C, N. A8 F6 _7 K+ Q2 Q% ?Example: Factorial Calculation$ e- ^5 N  P) o. h
    / X& z; G" N: N. V6 r; J6 M# k
    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:
    # q$ K# U$ |1 U* b1 D8 \2 R3 P! D! W
        Base case: 0! = 1
    5 I0 k$ M+ w6 f( m
    / e, C$ ~( W" D/ B( F    Recursive case: n! = n * (n-1)!
    ; R% v7 b2 F  P+ _4 O% J/ C( Y" F; W0 A  K! S& b& L
    Here’s how it looks in code (Python):
    2 x: S7 c! u5 _2 Q+ spython( \: u% H! b/ N

    6 W: H5 c6 |3 ?* y" h1 |* }6 P7 e& t- A  l+ _' N& R5 Z2 d
    def factorial(n):6 b% X% I. a1 p2 Y' z
        # Base case) C3 j" p( L  A8 |- F4 W' @
        if n == 0:
    8 P( V+ p6 U0 V, [) A% B  s* M& B        return 1+ ^$ X+ W5 y4 n) A; \5 d: b( r
        # Recursive case6 \4 L& J% b  A6 I4 c" J9 ^
        else:  j0 N2 c) |' `. D" Z* f5 S( E
            return n * factorial(n - 1)
    9 }0 m/ J3 E9 ?2 i6 A  I0 F' G' _. W/ [* M! W* H2 ?5 z
    # Example usage
    ) @1 {3 B* Q1 fprint(factorial(5))  # Output: 120" _& r  N; F7 b1 H  R" |* d3 Z9 `* C
    6 q+ j% K4 f1 T: k6 }/ N, t/ Q
    How Recursion Works
    5 b3 G. ], @5 O2 F( p2 M% I1 j% O  k' S9 _
        The function keeps calling itself with smaller inputs until it reaches the base case.- V- M  g; C$ k$ s3 z
    0 d0 ~8 }4 E8 h+ a" L
        Once the base case is reached, the function starts returning values back up the call stack.
    4 T2 v/ d' X4 G+ o- L2 S7 j
    ( y2 W/ l  ]3 j% e) M+ j    These returned values are combined to produce the final result.
    + d6 D/ z/ t* Y) x0 ^( J# n. ?6 G4 ?
    For factorial(5):
    8 D. S# p9 ?7 {2 G
    ! r3 z9 n" P# k6 U9 A+ B: K0 M+ t, R
    * J2 [! z/ M/ Nfactorial(5) = 5 * factorial(4)4 G9 E: u; ]; Y4 j3 `
    factorial(4) = 4 * factorial(3)
    % w5 C3 n# V- v7 g) A; rfactorial(3) = 3 * factorial(2); k8 O. e5 d: x2 b* y
    factorial(2) = 2 * factorial(1)
    + |- |$ x/ H& i' _factorial(1) = 1 * factorial(0)
    9 F: d5 Z6 ]6 \% C% N) s: i0 tfactorial(0) = 1  # Base case3 v3 n6 `% Y* f$ C
    7 r: Z' L6 S+ o: N4 P+ G
    Then, the results are combined:
    * V- x3 w, ]  L. z6 r. v0 f
    9 z# ]% _2 D$ g: u" D' _
    4 N. e9 m( o6 e" E8 cfactorial(1) = 1 * 1 = 1
    % b& ~& C- u6 t$ X: C  V  C% v$ pfactorial(2) = 2 * 1 = 2& C$ }9 O# k' x/ B. D
    factorial(3) = 3 * 2 = 6
    ! o: a/ Z1 e- \8 @: X5 Qfactorial(4) = 4 * 6 = 249 P% Z+ f2 U5 {9 G# s* M- a. b' d
    factorial(5) = 5 * 24 = 1203 g& G" n( S& `0 l' \" h

    5 K) |  G, L& F9 N" O. g; cAdvantages of Recursion6 |% Y! g. W  T; Y
    4 V0 f( S, E+ j" 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).
    6 \0 H" e, r4 J, g7 }# ~& _, d  Z" g1 x; b
        Readability: Recursive code can be more readable and concise compared to iterative solutions." R# W! M8 b+ J: b

    + O: u6 O9 O9 P7 ^5 g9 NDisadvantages of Recursion: k/ A' }* t( F; p
    + a! q3 @' |4 u. {( T1 }/ o4 m% {
        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/ j9 b% h+ b; Y' M
    " M* B* S1 j/ g8 F4 P$ v+ k
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)." f; e7 ], T+ K  `7 _  @' F) G) h2 _
    - \9 ]3 C9 q7 f- b
    When to Use Recursion1 W( E) P; l( e% y' \) w
    ! t2 M; n; r& _- B" b
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 m1 G( G/ ~5 G8 A3 c

    ) X4 J% \& [, j( ?" R7 r    Problems with a clear base case and recursive case." }4 V/ W. U) x' y. ]

    6 T2 ?9 M; i# {# }) eExample: Fibonacci Sequence
    $ n5 P, v9 q0 K! e1 P2 |0 ^
    5 G0 e7 z2 K: ~7 C) s* H# VThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 X+ J5 Y5 M& C7 z
    2 Q3 T' o8 k! z: u  `
        Base case: fib(0) = 0, fib(1) = 1
    3 n' a' H9 B3 C, S! n  Q
    + T% S1 `2 C, Q4 H8 T    Recursive case: fib(n) = fib(n-1) + fib(n-2)8 G! q, G7 U0 l( j  }% d

    : H* [% @" H. d- Upython& i- @2 ~$ T. K+ M. q9 |# _5 H( g. ?

    ) p0 ]5 i) r8 k7 @
    3 x0 q& q2 d) mdef fibonacci(n):
    0 m- E. `9 H+ j    # Base cases
    + i$ K6 ~1 l4 V2 v# D' [! |7 O    if n == 0:
    / P) d: K+ Y6 O# S! l        return 0" _- r" S1 j& p2 `$ W+ E
        elif n == 1:# ]2 n( J; y2 Y8 V* l) ]
            return 1
    / \9 v7 ^6 D9 P. P6 ~    # Recursive case1 X  }/ T+ D* X. T9 f- z
        else:
    4 I1 [* F9 Q- {) \        return fibonacci(n - 1) + fibonacci(n - 2)
    ) ]  ]. \$ Y, u. m" }( K* _* _/ }( T  E4 p0 M
    # Example usage
    0 p) q) F  R9 j0 k0 H1 ]1 D  A9 ^% Nprint(fibonacci(6))  # Output: 8* a7 ]8 y' d8 _* p

    $ _! T) I- G$ ^9 z0 ETail Recursion
    ) z" w; T6 B0 G* B/ q" a, [* {
    . c7 [  l; C$ ^- dTail 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).0 Y2 ]# i* i& h, f9 H  p3 D
    & w9 H; w) ]+ Z" P: q
    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-7-29 05:07 , Processed in 0.058046 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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