设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 r% T8 _- X/ D, H. k! b; |
    2 r, d7 y# \; D1 b! P  w/ C解释的不错
    3 @/ O( g, O2 m5 }# G7 r
      G* h, p, \4 P* [递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. d9 F+ t0 X9 _2 F* S0 A
    : M; x  u( ~: K) w' c: S, _3 b
    关键要素/ P4 Q: K) j4 i0 W) J, U
    1. **基线条件(Base Case)**
    . g  r! W; \, O. _5 X* [; C) c   - 递归终止的条件,防止无限循环
    & X: W8 o0 J% w' B5 L   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 `, \7 \- y# Z

    6 N% ^9 u1 K9 w  j" X+ X: D7 L2. **递归条件(Recursive Case)**5 D7 c4 G9 w) z' ~8 a1 a
       - 将原问题分解为更小的子问题1 x/ H0 V4 b" `  \% y; n
       - 例如:n! = n × (n-1)!  m7 B# T0 u' Q

    & `" K9 B; Y  X 经典示例:计算阶乘
    $ U5 ~" C# R- L/ _python6 L: ?) f$ Z9 ~" {/ U# A# c: s
    def factorial(n):
    " v% w! @6 a2 k3 g3 l6 i    if n == 0:        # 基线条件
    ! \( P" p$ O" N1 n8 T* W        return 1
    ! H) n% a+ ]( I% F7 h" J1 o    else:             # 递归条件
    6 ]/ p1 W0 B* |        return n * factorial(n-1)  i3 {# ?) b7 q8 O( R
    执行过程(以计算 3! 为例):
    7 E. M. u( ~6 P" l" {2 bfactorial(3)
    6 c* `7 p3 ^6 c9 F+ z3 * factorial(2)3 m% ^' p- O5 R- m7 N! r
    3 * (2 * factorial(1))" K$ ^; d# i: P6 e$ G# O( _
    3 * (2 * (1 * factorial(0)))
    . o; f' h3 Y$ i8 \7 C6 R. O2 p: u3 * (2 * (1 * 1)) = 6: C/ X8 f6 T4 g) ?& y
    , S1 f4 X$ m1 m
    递归思维要点& _! P% m' x# R/ E' R& W7 h
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    2 M& F% |& A5 M( x2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ t* y1 N1 }6 P8 z" F
    3. **递推过程**:不断向下分解问题(递)0 l! E; \6 f8 g6 J  x  y- @3 Y
    4. **回溯过程**:组合子问题结果返回(归)
    " e* a! z0 F/ {3 r
    * v3 F7 c- _) Z, q& ?: z% P 注意事项& E% q/ D/ W! E) i
    必须要有终止条件
    + |" y6 v9 _4 i4 j' C0 u递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    3 W' q$ x& ^! k) W某些问题用递归更直观(如树遍历),但效率可能不如迭代
    4 f: ]' A( P& ]尾递归优化可以提升效率(但Python不支持)
    * n" P* c( }4 F7 J
      d* v9 h( ]+ C, K' t* h 递归 vs 迭代% \/ u5 V/ C# ?4 g3 m. j. C
    |          | 递归                          | 迭代               |
    1 V& o3 d& C5 d* d3 n1 c6 @|----------|-----------------------------|------------------|9 L  s) i7 C0 z1 }& {
    | 实现方式    | 函数自调用                        | 循环结构            |
    + o* {% O5 g# M+ F| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 j8 A5 O2 P, J! w6 O! h0 {# J$ G
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 _5 v3 d9 P/ I! P5 [| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    : u7 V1 ~; I0 X. F; F, C$ Q7 f. i" b2 f5 Q
    经典递归应用场景
    . l" M: M+ r" }" e/ u6 ?1. 文件系统遍历(目录树结构)
    ! B% e9 J+ ]; c% O( L( u. p) t. y+ A2. 快速排序/归并排序算法
    % ]4 ]2 e0 V! A6 @" i1 }/ ]3. 汉诺塔问题9 V6 G/ K% B5 Q2 A. L" j9 L
    4. 二叉树遍历(前序/中序/后序)
    , h. n* F6 F7 X* ]8 m! S5. 生成所有可能的组合(回溯算法)$ w8 `8 D: N3 b- F
    2 Q6 _- h* O7 J0 k" ]9 i; w
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    11 小时前
  • 签到天数: 3020 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    + s: u. w, N* L$ E/ I我推理机的核心算法应该是二叉树遍历的变种。# [6 S+ Z" ]6 U
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 e$ H! L; i& t6 x0 j
    Key Idea of Recursion
    9 P0 W5 L1 g1 ?' d9 n9 _; e
    $ k5 d! |0 h. ?% ~A recursive function solves a problem by:# b4 K, |. ?* ]/ `3 h: a

    6 q/ j# `4 {6 x. g# |9 o2 B7 j7 E# O! F    Breaking the problem into smaller instances of the same problem.
    . G* S1 t0 t7 E+ R1 N
    6 |0 A, q) ~) i$ V0 _    Solving the smallest instance directly (base case).
    & N. v4 }( h" ?" J
    ; r, a. W$ G" r  J    Combining the results of smaller instances to solve the larger problem.* m% F# o' \$ J3 |' c2 q

    3 @" A; B/ X; j0 X. w3 X) |- JComponents of a Recursive Function
    7 w/ H1 g* R" I8 y' j
    * H  O/ ^5 G0 `- C1 b    Base Case:# d+ G2 \5 g6 H
    * E6 |- l1 r* b* T
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    $ x$ |1 F) W, J! X
    ' n5 b2 S; y6 N# R        It acts as the stopping condition to prevent infinite recursion.* b, c3 M4 a0 Z, w& C  b) ^
    . [* j( I' p# z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! Y/ ?% ]* n& d1 Z* D

    ! C8 }7 j# E4 T3 C) n    Recursive Case:
    ; W5 P  q0 a8 |6 h4 W% ~, u& J7 N! f
            This is where the function calls itself with a smaller or simpler version of the problem.
    7 A" f, {; v; ?0 l- X' K
    6 N1 A, ?4 s) t5 p/ C# o        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).  S9 w% j5 }: c6 |4 x

    5 m/ [' ^$ I# x1 H( VExample: Factorial Calculation
    0 s( v$ P% n" E: K
    & H% l& A% L3 M8 B8 RThe 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:7 ~" @* v8 V: S( t6 R/ {2 U
      Y  p- o  G  W/ ~. N- Y  h
        Base case: 0! = 1' @0 a2 K" N& I( O9 t6 a/ u

    ' v. y! Z, x5 l" J# h    Recursive case: n! = n * (n-1)!- j7 k/ P% k5 n4 Z

    : ^' `: C' s, R: LHere’s how it looks in code (Python):
    , w/ j6 S4 n$ x% S8 u4 \$ ?* o2 wpython
    4 `4 f8 d8 M& |( G5 p
    7 K# w& a% y! q9 [4 d. T3 Q+ {2 n! g( g/ E- C
    def factorial(n):) w% T$ L, ?9 A! O
        # Base case7 S& r: J1 l0 U
        if n == 0:$ o- O% d; Z; B# e
            return 1) S4 F7 T) a+ T: C3 \, A" O
        # Recursive case
    ! h0 Q9 f/ }4 w3 A    else:& S5 s* u& z  M
            return n * factorial(n - 1)' ~& G. |" j  z6 U  d3 @

    ! e0 ~1 T2 W7 S4 h# Example usage
    - k/ k% Z9 q1 W8 ^print(factorial(5))  # Output: 120. h# p. l, ]' U  @' R9 @, p7 C$ x1 ?5 l
    / N* L' r( E8 Z" n
    How Recursion Works
    2 d. [& \5 c0 M& A# j
    0 R* Q# I! l. m8 N, u5 H) H4 W+ q    The function keeps calling itself with smaller inputs until it reaches the base case.9 t+ A. Z; g4 ~  ?% l  x

    5 T, K- _. I1 ^" m6 F4 b    Once the base case is reached, the function starts returning values back up the call stack.
    , `1 d. D+ _4 F- F
    0 u# E1 B: K7 _/ P) |0 F) ~8 c! n    These returned values are combined to produce the final result.* Q3 _2 }3 j* x6 T
    : q6 w) I3 P. x# b5 D
    For factorial(5):1 u! T: N; v0 G1 `* l4 P% r

    / d5 M) d1 J% e- c/ D! F! p2 {* h$ k) V
    factorial(5) = 5 * factorial(4)
    7 |, q  _$ ?* `/ d' ]factorial(4) = 4 * factorial(3)
    4 Z% u+ W$ B* s. `0 O1 n! Rfactorial(3) = 3 * factorial(2)
    9 _$ {. g- @& Z4 b$ |8 A8 j; kfactorial(2) = 2 * factorial(1)4 R8 L- I; {4 z4 G: [' j) X& [
    factorial(1) = 1 * factorial(0)  ?8 t  l  B1 G) r5 W) f, e7 k
    factorial(0) = 1  # Base case4 I3 H( s% z# R& _
    8 r0 J5 ?  w$ {# ~
    Then, the results are combined:
    % b/ G. R3 T: G. J. t7 h' \( J/ O' k; H8 e! j4 G

    ) S1 a5 u  b. x& u# q; Efactorial(1) = 1 * 1 = 1
    : u$ _9 b0 y, Afactorial(2) = 2 * 1 = 2
    : {( F, A# q% Z' jfactorial(3) = 3 * 2 = 6
    % V- W" i  u& W) P& ofactorial(4) = 4 * 6 = 24# w) l5 i) F' I- W  j
    factorial(5) = 5 * 24 = 120
    7 K: F4 r  g( ?+ n  ^$ G
    " ]( o; V( f& k8 `Advantages of Recursion
    * |) A& s/ b3 v% U  w# I( O7 ~: c. A6 I
        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).( y. D2 E- J# k

      D. ~' K! ~, @) B/ }    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / T7 b* b/ X4 F  E: y. X4 `  X! x( m2 d: J9 P
    Disadvantages of Recursion
    ( g2 N& r. T' U/ m& b0 ~" b* j
    ( ~% M& Z8 ~( a2 |! ]2 U    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.
    - j- K& ~1 X9 r
    " k- o+ @- j/ E! x$ D: K/ i; `    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ) z0 U2 l2 K7 m+ J
    ( i& C* f' O- e6 |+ Z; q$ BWhen to Use Recursion4 n% x* ?6 U6 c1 G

    4 `( g7 Z' B6 f, Q' l: c' h/ P; _7 l) P    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 ?: B5 d0 `: H! o5 u/ f8 g* J  O' R1 w' W% |% R7 c
        Problems with a clear base case and recursive case.0 c' ]; |& A% w( f% }/ S6 o

    - y0 \& N& n# e- u7 \) {Example: Fibonacci Sequence
    9 u8 k6 o* S0 s6 J0 u* Q
    , b7 P; M% ^7 a; D* HThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ s, W1 _# M9 ]3 P, V5 Q7 C

    ( [% @! Y( e6 x' J! z    Base case: fib(0) = 0, fib(1) = 1
      _0 q! g- D1 I1 O, ?( q6 I
    ) T' K$ C  D3 ~. h5 _  u    Recursive case: fib(n) = fib(n-1) + fib(n-2)+ b6 n6 p( |& R

    8 o6 I+ o/ x! b( n8 I' Rpython' R1 E0 j9 p9 x+ G, G6 w
    # ?0 F& r2 W$ Z- D& `! l

    " x7 ?- S) s1 t" m6 ^: j, N0 vdef fibonacci(n):
      y8 V' j9 ?) ~    # Base cases
    5 z2 U6 s* a1 N! a' R4 m+ Y    if n == 0:1 F5 E% ]: u3 T, H
            return 0
    9 Z# u4 X% G5 p4 n4 H& a. S0 w    elif n == 1:
    ' a1 ^/ \* b4 r        return 1
    / u) G! u$ E; b    # Recursive case* T( U( J( b6 j- j( Q* C& U
        else:
    5 ]! H1 u  |- y$ j- r        return fibonacci(n - 1) + fibonacci(n - 2)
    - b1 P( m, E% H) S. q- L
    6 T* {3 p9 G0 O# Example usage
    3 D* v4 L$ y6 {( h% }print(fibonacci(6))  # Output: 8$ v$ k9 S) ?4 q1 }& k, K$ }
    7 T2 p# {6 m+ g! z8 L7 E
    Tail Recursion8 e- H  I5 p  i

    ( n+ b  l3 J, k; ?$ tTail 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).. {9 ~0 q5 C# @- O( ~

    ' ~; h% l  W+ m8 m5 r* `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-8-25 18:11 , Processed in 0.048813 second(s), 23 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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