设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % F, A+ i7 ~7 s- F8 k! U3 A4 Y

    5 |5 }. Y' M1 ^* Q' m) H解释的不错
    3 x6 A& ?0 z- o8 c" _' M. _# B" R2 m2 j: S. X% \9 O
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    6 ~% u0 E+ Q+ l6 E! A; s% N9 e. n
    关键要素
    7 o6 q) j; ?) E8 u1. **基线条件(Base Case)**# q0 s& z' Y# [5 w
       - 递归终止的条件,防止无限循环
    : A5 V6 @! s0 Q% i: Q; X! q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 }. G: m( o& A; V. c# R

    , Z, u3 K# T- ^' C7 X( {: E2. **递归条件(Recursive Case)**9 x: q+ E* K* N) H2 A4 Q: d
       - 将原问题分解为更小的子问题/ n1 P% L( p  a+ W
       - 例如:n! = n × (n-1)!
    1 c, W% i2 q, e% f
    4 M0 l4 ~3 J6 Y2 a 经典示例:计算阶乘$ K' [& d6 K# d5 H  x: x
    python
      w' T6 A& X9 ]. U7 G! _8 v* v. P4 `def factorial(n):1 I/ N, b6 p) p
        if n == 0:        # 基线条件$ X+ W$ a1 M+ q1 M* _
            return 1' L# E4 t, a& I# d% P
        else:             # 递归条件9 h0 K+ Z1 J$ E+ C6 N
            return n * factorial(n-1)
    - J+ |  Z* m8 `执行过程(以计算 3! 为例):+ e1 V2 v: l6 [2 f. s) R
    factorial(3)/ d3 U- W  X" B7 ]" |3 r4 w
    3 * factorial(2)
    " ^2 ^# @, a" v. O, O3 * (2 * factorial(1))
    8 R+ u2 I& I5 \8 H$ h6 Z! [3 n8 D& a7 I3 * (2 * (1 * factorial(0)))0 V9 Y- d' D+ e4 A* X3 K
    3 * (2 * (1 * 1)) = 6
    $ x$ x9 o$ Q( m3 Y6 [, q- ?* c7 J4 K  O
    递归思维要点0 Z$ u, }2 v* Y$ x8 X" G
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . E) Z% P' n- O9 ?. O" S; ?$ ]5 \2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    : m7 C/ X9 p+ o& V3. **递推过程**:不断向下分解问题(递)  u7 u9 I1 b2 D
    4. **回溯过程**:组合子问题结果返回(归)
    $ L# J; Z1 A5 x1 x9 R& m
    $ K& w5 {. F( C1 f 注意事项& @0 j0 B* D. a  J. k
    必须要有终止条件4 D- ^  n3 F7 f* `: h  |. W
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    , |7 W% Q6 m2 l1 M某些问题用递归更直观(如树遍历),但效率可能不如迭代) w/ s9 c6 I1 t" L2 L0 |
    尾递归优化可以提升效率(但Python不支持), _$ M" Y9 n; O
    5 o" B% F/ A' f6 H
    递归 vs 迭代
    : z' x* K% V: l5 _|          | 递归                          | 迭代               |
    1 _) t' B& ~+ c2 W+ f9 @|----------|-----------------------------|------------------|
    5 @8 }1 E9 ]' B  b; s- q| 实现方式    | 函数自调用                        | 循环结构            |2 c+ z( g3 b# t$ R' o
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & v! K5 M& d/ c. e| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    : g$ G+ Y5 W5 _& T' O| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |, T, }) _6 E& i" @
    2 p/ z& x  f2 q0 ]5 f$ z
    经典递归应用场景
    4 C4 Z, P4 h7 e; ?/ J% m1 u4 D4 f1. 文件系统遍历(目录树结构)
    , y- d) Y7 @8 R$ k( s! F* I  G6 Q2. 快速排序/归并排序算法4 _# Q1 b* G1 j9 t
    3. 汉诺塔问题
    7 c! a+ W7 i3 f  Q( `% @% j4. 二叉树遍历(前序/中序/后序)
    0 S% n, F( r* m: H- }: F5. 生成所有可能的组合(回溯算法), }! Z1 ?# b) v& a4 M9 J8 w
    + Z/ d" X" Y" }! P8 h. _) m
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    11 小时前
  • 签到天数: 3124 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,- h) J1 `3 W$ O
    我推理机的核心算法应该是二叉树遍历的变种。
    ! {9 H6 w1 D" s1 X% `9 Q6 N  d另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    $ F; R2 r( W7 L- ]Key Idea of Recursion. r6 u& K6 x$ x" L- F7 o; S( o
    1 H/ w. Y1 W0 V2 P& l) M  D# A$ L
    A recursive function solves a problem by:
    . E, o  A+ P' e6 k# O2 [4 {" \
    ) n( L6 }" I$ w. j# O    Breaking the problem into smaller instances of the same problem.' G/ }1 G! W( [+ ], t

    2 C8 X  z5 S* c8 J# u0 w  }    Solving the smallest instance directly (base case).4 r/ V& j! S: U" m; P- f0 r

    . D/ n$ v; ~* ^    Combining the results of smaller instances to solve the larger problem.3 l2 @* R/ j+ i) Y! A, {3 p
    3 G3 G1 r" W) `- I% B, w, |: T
    Components of a Recursive Function* \( q$ i; _! j2 R
    / V8 m* d% O+ _9 g5 ]; o
        Base Case:
    3 _& N# D& Y+ [+ |9 f( J# y8 E7 A  |, h; F! `& x% q% ]- |3 @" g
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.8 Y$ F- d! Y2 g2 B: O7 l! A* s

    5 v* z7 k. P' K3 \1 |        It acts as the stopping condition to prevent infinite recursion.
    7 F  [" A$ f( J
    7 A5 k3 ~' y& |# [5 c        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - n" C$ I/ g" h8 B* x
    , R! |4 F& Q% {# t& H9 Z    Recursive Case:
    8 k- I, v& U! ~  A* [& J, ^
    ' ^% H9 w' _8 ]! l        This is where the function calls itself with a smaller or simpler version of the problem.4 V1 ]1 C# J3 Q. @' i8 ]( s1 \  A

    6 J! M. q4 }+ ~  L, ~9 s8 N# L        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 @; t" n2 J' F$ w; j  B, E: a# E) U% W5 p. f0 j4 s: f
    Example: Factorial Calculation
    8 a3 E% d) w5 T& E3 Y/ z8 ?4 G/ T- b$ @8 i
    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:
    6 O; l) K  G! \: z$ m8 {5 B& D: E
    5 @# ?) _1 W6 L, H: g% ~    Base case: 0! = 1' `' y; ~6 [. i6 N
    1 U7 t- n9 s. d: r
        Recursive case: n! = n * (n-1)!
    8 `( M! o: c# W/ g
    ( ^- i2 \6 D5 r( i/ H% DHere’s how it looks in code (Python):
    / S+ i( }4 }4 T! vpython6 a9 k0 U* M! \
    ; o8 D4 M, h# Q! _, T: Z

    . A( J: s2 S3 a: x0 |: e: Mdef factorial(n):
    # q3 i% s# c1 L* J& Y$ \* F7 [    # Base case: `0 ]9 l9 {" R: @6 _6 ^
        if n == 0:
    " s$ H; p  ^# p' N+ g. R5 v5 G        return 1
    3 [" Y# T0 T. h; y) N: z' f    # Recursive case8 s) C! A2 A+ J/ V& q
        else:
    ' p: x  z, r% {8 Y+ T. E- {: U        return n * factorial(n - 1)
    ! }% u* ^& `, N) X8 h5 ?+ l( E- y; S! a4 D- _
    # Example usage
    3 V  m6 E  R( F& q- M/ P! l) c- }print(factorial(5))  # Output: 120
    " r; B7 I7 \$ f
    7 t& \  ]& \9 L$ Q$ z# h6 @How Recursion Works! W" F  b/ `# Y, V! h7 {

    6 \0 \0 e, C% I) Z# N    The function keeps calling itself with smaller inputs until it reaches the base case.
    4 n- _6 n3 x: Q) K( ]$ g9 M' V3 ~" V0 X& F
        Once the base case is reached, the function starts returning values back up the call stack.
    : K7 W' O. k7 t
    5 D# B# _* D9 Z7 a' N/ B+ n    These returned values are combined to produce the final result.
      ?/ S. n0 ~' I0 n/ I, a! ^3 K. M8 o) {2 \7 `
    For factorial(5):; `: T$ @( s: E, {

    * o3 q* h  C# B% |3 e1 z3 h6 ]
    6 Y! Q0 Q- s' Pfactorial(5) = 5 * factorial(4)0 d- P  S; C" R3 n8 ^8 Q
    factorial(4) = 4 * factorial(3)
    , f7 @2 P7 m& G& N* R* c# k/ E3 rfactorial(3) = 3 * factorial(2)' h9 P% U' G) h
    factorial(2) = 2 * factorial(1)9 \: H  x; L1 v- R
    factorial(1) = 1 * factorial(0)
    9 T7 ?8 j- u! I7 W. X% e+ m8 F( Ufactorial(0) = 1  # Base case
    6 a! x' `3 P  R8 r0 Q3 [5 x& d
    2 c( c7 |7 {- i  p% aThen, the results are combined:  P/ z( c- a! Y, Y8 O
    + W4 u. a5 B2 o7 G& ^

    - g1 v# V5 `( R5 D$ Tfactorial(1) = 1 * 1 = 1% [5 b, h; k. n' p+ v, `, p
    factorial(2) = 2 * 1 = 2
    0 {# r! R' W' h6 }factorial(3) = 3 * 2 = 6% i* }! m1 h. ]7 m4 ?/ V
    factorial(4) = 4 * 6 = 24
    . p0 O8 R4 `9 w4 p0 i* rfactorial(5) = 5 * 24 = 120$ |' d0 `# k: j2 S2 x

    ; _5 I, ^) p! VAdvantages of Recursion
    5 j' U7 \; Y+ g* F, p# q' M- n
    8 ^. A; H! H7 v8 n- @    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).
    # X/ W4 U. V) p! L- O3 ?7 N$ ]5 i, X" b
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    4 v0 X2 }( R0 H$ G. d3 t! A/ d  [3 t
    Disadvantages of Recursion, o  L- k, F1 ]8 i  K3 i$ W+ g0 a

    * f) O. Y1 c, M0 f& m7 x& _, G    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.
    4 D8 \1 T' \& x9 n0 ^) _6 B7 e3 K
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. X! ^7 B1 j9 x  s4 ~1 v

    ; f4 b8 w# |5 x( nWhen to Use Recursion
    8 N" c4 N: y8 ^" O/ \
    1 j1 j# H7 b  o    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    0 b8 V$ v7 y; F9 P& l! Y; g; r; C0 K5 B
        Problems with a clear base case and recursive case.
      s( _- [4 f# L: O; b! ^. z8 G' e: [; U4 {7 W% M
    Example: Fibonacci Sequence
    * h4 f  P2 X& [6 u; f" D3 m3 ^
    8 ~, f+ h& L8 S% a8 zThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ i$ K: t* H9 w

    / v/ o+ I+ h# H3 x! J7 g. M# \" d( @    Base case: fib(0) = 0, fib(1) = 1' K, l' B+ Y' e

    / y& c' J- q% x' k    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . A% U; q2 i4 E; N& p3 U8 ]! [+ Y: j# z% Z9 Z- B
    python
    3 a) D3 H2 p; m9 H! W9 s7 A4 r  {5 R/ c$ `

    : L" J) Q1 r+ mdef fibonacci(n):" ?, N0 r3 Y; S/ W  E, D
        # Base cases& K9 W% Z  e5 B0 M1 m: G4 D( N. H
        if n == 0:/ u9 e* z# ]' n" T  s
            return 0
    ' _" R, |* [2 z' G# z    elif n == 1:
    , y3 Y, s2 Y* A1 F, [. B- l        return 1, w; p  t0 v& N' d; S
        # Recursive case( j& C  x4 E. _5 Q7 W: }
        else:" w2 _, T8 D% X
            return fibonacci(n - 1) + fibonacci(n - 2)
    5 D$ x9 `1 X- U# a2 U. d, }6 R) y# Q# e7 {9 i
    # Example usage
    " }$ x6 v2 L" y& n8 k; lprint(fibonacci(6))  # Output: 8; \$ h5 Z, v' v' Q2 n
    7 O: j" L& @( V! S6 w
    Tail Recursion
    ( i1 Z) t, s, i2 n6 }. E0 d$ ?# {0 r4 \2 B. J
    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).* h1 H  S* l  r( x2 ?' s

      q' d5 e0 F2 M$ ]" }% ~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-22 19:57 , Processed in 0.030081 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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