设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    % ~& _0 p- l# ~4 @) N+ ]$ d2 n- a! L1 u# t
    解释的不错, y! a9 \8 I. n- f9 D' d, H7 `

    * r5 {# \: w2 h$ v3 x- s" `: G) Q递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 C: \6 r8 e! ^3 Q1 i3 B
    ) v* ^2 g# h$ u0 n) A
    关键要素
    4 l% @) o' X! d, @1 S, O1. **基线条件(Base Case)**
    1 ?. n/ K3 V* o6 q+ {0 F7 p: l8 a   - 递归终止的条件,防止无限循环
    - i7 t& O& y/ ]$ @+ C   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 14 g: E! A" M9 Q8 w4 z+ A  f! D. X
    8 n- w9 ^& F0 h; i
    2. **递归条件(Recursive Case)**5 ]( u. P! O( ~- w1 e
       - 将原问题分解为更小的子问题
    2 J/ f3 m. T( d" B- ?   - 例如:n! = n × (n-1)!
    ( F( i- ?: g  t4 |0 d. A* a7 N
    经典示例:计算阶乘
    & t: y0 m' x. x8 Y4 }& ?python7 a7 K& @5 G! c0 P9 `, a5 v
    def factorial(n):4 e0 q  M) o; e$ [( a% ?
        if n == 0:        # 基线条件% {2 I/ p/ I& h
            return 1
    0 l& m+ I+ }6 x) C: w    else:             # 递归条件
    ' _3 W* ^" B; g6 q/ W& S        return n * factorial(n-1)
      }* x) d, t2 D执行过程(以计算 3! 为例):: A2 G& C; ^. J- g% ^6 l
    factorial(3)
    6 s1 t# k9 y. M( G- R( G3 * factorial(2)# k6 _- G6 h" O% S# i! c4 W4 ~3 o0 ~7 {
    3 * (2 * factorial(1))$ q8 G4 z+ _1 Q* |2 ^$ ~8 f
    3 * (2 * (1 * factorial(0)))+ ]1 p1 y$ b1 U+ b" E0 h  K
    3 * (2 * (1 * 1)) = 6/ ?- n+ M0 v5 F

    ( m* f8 V$ |2 a1 I2 H 递归思维要点$ d; t" Y3 ~; ^2 J+ M0 }
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . h# F$ C0 i; ~& q2 E% o6 l: @2. **栈结构**:每次调用都会创建新的栈帧(内存空间)# ]: }& I2 I* U  q, {
    3. **递推过程**:不断向下分解问题(递)" Y" M  J) e. ?" m8 ~; a  y' |
    4. **回溯过程**:组合子问题结果返回(归)/ K; L0 _2 o; [

    4 f" @+ g5 B' E! K9 ?9 d 注意事项
    % k5 t8 t5 `7 Q$ D8 W, U必须要有终止条件
      u8 ^. \! D. k! u递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ) p7 F2 W- K9 t* e+ T某些问题用递归更直观(如树遍历),但效率可能不如迭代& Q7 y5 Z1 Q! z; `
    尾递归优化可以提升效率(但Python不支持)
    2 G" a! g2 f, }# ]) X# @6 s1 ?: j0 m
    递归 vs 迭代
    4 m: j* j- d& j- B2 o2 I& h& G|          | 递归                          | 迭代               |
    - I# [+ T- ?6 y) @1 I5 b9 V4 k|----------|-----------------------------|------------------|7 C. y7 p+ y: L1 o
    | 实现方式    | 函数自调用                        | 循环结构            |
    : E& x2 z, Q! d0 o' N& z2 a| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , Z1 S1 H! o* ?2 B7 U9 Y9 \| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; e9 ^+ e/ y2 Z  Q) |& z8 w5 }! D| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) J4 k- _  O7 S! l; P! _0 U4 _' Z9 W/ D* A: B' n5 E+ t
    经典递归应用场景5 b+ B4 s7 z+ O2 N) Z" {
    1. 文件系统遍历(目录树结构)
      `9 {$ Q6 v+ I% q+ b2. 快速排序/归并排序算法
    / U( S; [% B8 H3. 汉诺塔问题
    2 @3 ~, _) Y( V0 t4. 二叉树遍历(前序/中序/后序)
    + C$ A7 S$ h% y: U/ e5 E+ L5. 生成所有可能的组合(回溯算法)
    % _1 W, b( m6 w8 ^# X  p# H; ~) [& J% i  P: `
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    1 小时前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 w% z. [2 p' W- T
    我推理机的核心算法应该是二叉树遍历的变种。
    3 e9 n% I) B4 n# R9 O% ]9 k' q9 Y另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:4 O) \# i0 }4 R
    Key Idea of Recursion; m7 ]/ p" P* o7 D$ g

    ) r# L: G# Q; t' Y$ o, ~: mA recursive function solves a problem by:
    # U; t' d2 r7 k6 y+ c
    " B- k) V6 h" w1 D( M/ s9 ?$ P    Breaking the problem into smaller instances of the same problem.
    $ K0 X$ D# _+ Z& O# `4 s+ i) K: B% u4 [0 L% z& \
        Solving the smallest instance directly (base case).
    5 |7 Z* S* _5 i; f
    7 [% E5 n* i: F! m    Combining the results of smaller instances to solve the larger problem.# b5 K- v- ~" ]* d9 U  _* q1 M
    % l* y, w- ]6 `2 R7 y
    Components of a Recursive Function
    - e; a1 o% M) I0 a. S! X
    ! I! r$ P" u1 V! g/ U$ ]* B    Base Case:9 i/ Q2 V' m% v& c/ `
    ' D2 T* i9 z) Y" f; d) T* o
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 a7 a+ Q9 e; k- h8 R7 {6 N1 c" l4 P2 i: q1 I
            It acts as the stopping condition to prevent infinite recursion.
    0 x+ ~' Y) E, z4 _, j; a8 f
    + Q! w9 C6 a9 [$ L6 E& G        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    3 h# C  K! ]0 l4 |0 j7 T4 m& ?( D5 R
        Recursive Case:; }1 M0 `6 U; @7 U, c

    / O1 |4 I( ]7 G: ?, \" Q- R: R        This is where the function calls itself with a smaller or simpler version of the problem.
    / @9 X$ k: G* e2 F
    % X  u4 }/ N( W* z8 p7 s: b        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " ?+ A' U% @( y( i0 r' P  q0 A- ?
    3 A! t+ x8 W9 |: l% |Example: Factorial Calculation( o% E1 {" \  w9 Y5 D" S

      e" q7 M  q$ iThe 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:6 y/ q- S# \) \7 x, j/ Z1 N' V6 b

    + J  e- X! a( `+ K/ M( W) H    Base case: 0! = 1
      e- O: D7 e0 z: g' M3 K4 z: M6 v2 k  }3 k( h
        Recursive case: n! = n * (n-1)!4 N) k' m9 m8 t: @& [5 w& E  Z

    ) S0 A$ }; c- Z/ P5 xHere’s how it looks in code (Python):- R% ^5 a- u9 m' z9 L2 G; R
    python0 H; e* }( V/ g" e  g2 n

    2 n6 o- ^; M9 F% [
    ! B0 i: q4 a( l) d! `. bdef factorial(n):
    * `' T# [* U: T# q    # Base case
    3 Y. Y% n9 u. X6 H. i    if n == 0:
    . m% Q7 w. ?' x6 X; v1 x        return 16 _/ l4 j6 u8 Y# }3 c
        # Recursive case
    . ]& X4 K( y! o6 O: \    else:0 k# H0 J2 z: S+ f! N% ]. t6 C
            return n * factorial(n - 1)
    ! S6 E5 f1 E/ Q8 l  k0 }4 w! t; U
    0 L4 ^/ ^# f  J* B; p# Example usage
    6 m' p5 V+ N' t7 A: jprint(factorial(5))  # Output: 120
    4 a. ?, u% R# G. S- B: Q1 Q$ v7 D, d
    How Recursion Works
    % S& J1 ]& B2 J$ W( W) c
    5 |( }! [* y# U( V6 V2 p$ d    The function keeps calling itself with smaller inputs until it reaches the base case.
    ! y) w) z; y, p" C
    + X' y" D- ]# q. e% K1 S    Once the base case is reached, the function starts returning values back up the call stack.
    2 w: d* }& ~5 \, H, e" s# y. T: \6 ^2 P
        These returned values are combined to produce the final result.' l7 _& R. r, V
    " U: U, [* c; {
    For factorial(5):7 i7 l; v' ^0 _/ ~
    & K9 \  D* ^  ]7 F
    . I9 x5 `! a1 T6 L
    factorial(5) = 5 * factorial(4)
    9 F. Y" X. S; q+ @$ Vfactorial(4) = 4 * factorial(3)
    ( @' M! [- X7 z- \' f: }factorial(3) = 3 * factorial(2)+ L" W$ A# p2 _$ o) `0 R8 K2 O
    factorial(2) = 2 * factorial(1)
    . O- `; E0 p, j1 ^1 c+ ]( [- u- e& efactorial(1) = 1 * factorial(0)
    6 L# ?3 j. n) O: ^factorial(0) = 1  # Base case  w. C7 q3 T8 Q5 m3 Q* G" `! }, j

    4 D* s8 C  U) J1 ]' OThen, the results are combined:1 \. s& f1 G# s
    ( o6 `6 Y0 v2 \- G

    0 l) o0 h) Q& P: S4 sfactorial(1) = 1 * 1 = 18 L; n* p7 u. K$ o7 Y: V
    factorial(2) = 2 * 1 = 2
    / b" Y* r0 r3 |+ S& ifactorial(3) = 3 * 2 = 6
    6 w9 i0 `) r7 u. @factorial(4) = 4 * 6 = 24
    8 J' G0 K6 S- Vfactorial(5) = 5 * 24 = 1207 E7 p7 ~  G; }' |" x
    ) V: g# a! l9 U/ m; s
    Advantages of Recursion
    8 d2 Y% u: j+ S
    1 m. b! m" Q" B9 m- |1 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).
    0 \; I/ A- |5 ^  h2 S
    ; T, u5 F5 V& I    Readability: Recursive code can be more readable and concise compared to iterative solutions.: @1 M: V7 A& f% F: I! W( S
    % I% K" d0 b) Z. Z0 g
    Disadvantages of Recursion
    ( i4 a6 v: P% {( l  x& R
    " x/ Q& h! p, B; c    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.5 n$ o1 B- _* N7 T+ _/ F

    ) D3 s+ ?1 s! ~! \  e5 A    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 w1 j8 F; W# v1 ^
    , @% r$ J5 p$ ]. P  N! L
    When to Use Recursion
    ; v, @; o: \. t) r; `0 m8 G6 Z9 u- f* m, Y( O6 ?
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    1 u! d; ^, }0 n) ]
      K6 ?4 m1 F# N: z- T6 i) t* Q    Problems with a clear base case and recursive case." N, ?5 I* M4 A6 I& a% W
    - i; A( }2 p, E( u, W  f
    Example: Fibonacci Sequence  I9 X0 \. J/ v0 O  f0 G

    ! a7 K5 G0 b9 K& c, Z, UThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) J/ f/ Q7 G2 g$ f. V
    0 C. S9 u' ]7 Y, n0 X( G- k
        Base case: fib(0) = 0, fib(1) = 1
    ) s) t5 E- W# g/ n" g) R+ v( r; `7 J2 C! ~/ M2 y/ X- ^9 [' r/ }$ z
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
      F' N+ b: c$ ^3 I" `
    . q  ^, K9 F0 e5 r) Opython
    - }% Y1 \9 ]3 }0 f+ Y+ G( w5 [0 [6 b5 g+ R3 F5 Y
    4 B. o' m7 \: @8 v( K8 K
    def fibonacci(n):: U3 W" \0 S' S: g
        # Base cases
    ) T9 F7 O! }1 q. W, i/ O    if n == 0:
    $ f8 g% S+ v' A$ W" [  z% j* g        return 0
    & E2 d! H6 s+ a0 W: Z$ m2 u    elif n == 1:
    0 {6 S; }& X* s5 B6 t        return 1
    ' u8 K2 H+ n  {- D5 v! F, G2 o    # Recursive case
    , [* K1 F+ V) v. l8 K& n    else:
    ' }9 }1 W/ Y, @        return fibonacci(n - 1) + fibonacci(n - 2)6 }( k" m. C. u. C$ d+ I6 S- W

      |9 ], L8 i& ]/ w# Example usage
    4 Q' p! ?: a; f2 t: g1 E& ]6 |print(fibonacci(6))  # Output: 8( ]! H' l8 T# l' c

    2 k. W% t- O) }! k3 C# ATail Recursion
    + K( z  c5 o/ ^% w2 P. U5 a
    * k5 u5 f( r, }  GTail 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).3 K: o# o3 x: o7 v

    7 Y" M5 f+ P! n5 LIn 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-3-23 09:12 , Processed in 0.056697 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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