设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 # U: w; P9 N# ?5 a: e

    6 {6 v  M4 m3 T. B9 i0 i! d/ M$ G5 Q解释的不错
    7 _" G* E* W  r1 a/ [7 l% u' r5 X: m7 S& `8 _! b
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% f2 M7 r! L5 K9 ~3 m' O

    : F$ C  N. L1 c 关键要素
    $ v1 h, T" a# Z# V& R1. **基线条件(Base Case)**
    / A, j% K( S, W7 [8 P   - 递归终止的条件,防止无限循环. W+ |! Q, ~+ R* w
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 15 B- T- ^8 Q* d" Q5 \9 R

    ) d) m+ n, w) Z' o2. **递归条件(Recursive Case)**
    + [& n" n9 s- ]2 v# H   - 将原问题分解为更小的子问题1 c2 [! D! ?8 p, u, _
       - 例如:n! = n × (n-1)!
    ' @; f8 g/ K6 C
    1 u+ J, O: e% ~3 i9 \ 经典示例:计算阶乘
    : s8 @* c, B6 j0 |- U1 c, epython1 H5 \  V( R* ?' X
    def factorial(n):
    . Z, q3 ^# @( }    if n == 0:        # 基线条件
    + q6 x9 X, v& M7 O% M' _0 y# E        return 1
    * D- q$ _! d: a7 P$ ~  J    else:             # 递归条件
    8 h( L6 R1 f9 D6 n' o1 f: I! s5 j        return n * factorial(n-1)/ W; v1 I4 N9 t! E6 E) n
    执行过程(以计算 3! 为例):5 V" s$ M0 D0 z) _4 i+ D
    factorial(3)! d; E$ L' Q  X7 [
    3 * factorial(2)
    ! m/ R( G7 t4 M3 * (2 * factorial(1))5 m, h0 k/ Z* q- w/ [1 |
    3 * (2 * (1 * factorial(0)))# J/ p9 Z0 v8 R" C/ h
    3 * (2 * (1 * 1)) = 6
    5 n  _4 M2 `( s# o, d
    8 T8 f, a" D2 p6 b0 H9 m 递归思维要点* v5 ?, g/ z# i2 i; t2 F
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
      J% t1 ?6 m; ^- i# Q2. **栈结构**:每次调用都会创建新的栈帧(内存空间)- x# B4 V$ a+ q3 U" A6 N7 }
    3. **递推过程**:不断向下分解问题(递)& b3 U7 W: b4 D  |8 a3 f
    4. **回溯过程**:组合子问题结果返回(归)* y' G. K6 `9 B  r5 R
    : R3 n6 i  U$ [/ {& V) S' e+ b  `
    注意事项; r8 }3 `  n: I% d
    必须要有终止条件
    * a2 c2 }. D9 V  o递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 `/ W+ c5 n3 q1 U/ J1 J) p) R7 Q: x某些问题用递归更直观(如树遍历),但效率可能不如迭代4 c) h3 E0 A2 n0 e! k% M- o
    尾递归优化可以提升效率(但Python不支持)
    3 ], P( X4 w2 q2 g( {3 q3 i1 J
    ! @, w" t3 h6 p; [* @5 Y% {9 \ 递归 vs 迭代4 E; M" ?0 W+ A- k
    |          | 递归                          | 迭代               |3 ?+ n. q, l# U3 @( J& q
    |----------|-----------------------------|------------------|$ |" I9 h5 K2 D' R  M4 {
    | 实现方式    | 函数自调用                        | 循环结构            |
    / }3 @) F( ~8 b" Z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |( V! k6 B/ b4 H" U& O( ^! T
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |; I% i: [+ ^$ U: ^% U# I" K/ e! z
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
      p( J) V+ ~9 n9 i% x, W* q
    + E9 k- F7 h- ]: T$ P 经典递归应用场景* {: H$ x. M. p- B7 k2 `$ E/ J
    1. 文件系统遍历(目录树结构)
    + {  @! _' W3 A2. 快速排序/归并排序算法
    ; b4 ^* G  A. i$ M8 @, s2 P3. 汉诺塔问题
    * g% s, g+ @; @: Y5 B8 ~8 q9 y) p4. 二叉树遍历(前序/中序/后序)5 Q2 u* |* g! J* v+ m: s5 s
    5. 生成所有可能的组合(回溯算法)
    1 M; I/ F4 B, C5 H1 L. {) F4 @; \( M1 V7 @9 W6 I
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    1 小时前
  • 签到天数: 3224 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, A& q; B/ x) k
    我推理机的核心算法应该是二叉树遍历的变种。/ D+ A# i6 g: g+ t
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" P4 A0 j) G, f4 z1 m% M
    Key Idea of Recursion
    # J# V, B6 B  w6 a1 V9 L5 r8 Y0 `6 o% K; ~1 Z
    A recursive function solves a problem by:
    9 m7 D3 f+ O9 L
    ' i! P* S3 u' g$ S0 ?    Breaking the problem into smaller instances of the same problem.4 t4 W6 s- i. n* m$ u. w, X( L

    9 J7 C& D* b3 ?: L    Solving the smallest instance directly (base case).3 P5 |/ |4 H6 b$ G& M6 e
    2 s" C% V5 C, X" f4 |1 b- v, E
        Combining the results of smaller instances to solve the larger problem.  `3 d9 z: ^. A1 z5 T
    9 A2 E( _9 A3 _* \6 X
    Components of a Recursive Function4 j, B) Q9 A: \+ t, f& }

    * v4 E7 V( R1 {) _4 ]4 D3 e" N    Base Case:* d  D/ ]  \% n* G7 p: Q3 s

    8 h( }  N) t, G' p1 V" _        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 t. M9 `9 L" a- X' B  y" y! _
            It acts as the stopping condition to prevent infinite recursion.8 V; T0 F! w* l5 t

    ; }! m& O9 Z$ [) f; P4 j        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# U  U1 |/ V! R& h: W3 @

    6 t# L% j( N$ Y1 J- j( i    Recursive Case:9 L+ Y% P: S: U& u/ K8 v
    % i' j) y1 E0 ]6 G, R! A4 n
            This is where the function calls itself with a smaller or simpler version of the problem.# J0 n# R7 a  ]- f

    - Q. w) ^3 Y7 ^: B7 K        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " v; O$ F8 x1 E7 p1 L; \2 y, E+ a# q5 Z9 K: ]* S
    Example: Factorial Calculation
    - _( v! P0 j' F4 I8 Q5 D# g- v* V# L# P
    : a0 K0 {: `8 ~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:. M# w- x' b) p6 a0 z" r8 N
    ! J4 Y- Z4 M1 e! g
        Base case: 0! = 1
    / b$ c# _$ u- E: @/ e# p
    ' O/ C  B1 o" h! W: a0 t    Recursive case: n! = n * (n-1)!# o8 @8 K2 C# P; p% P+ Y

    2 B) A1 E+ a8 c5 q5 UHere’s how it looks in code (Python):
      q  |9 i; [8 gpython! m" ]$ j, I6 }4 c# S. ~9 {
    3 ~/ E% \: e& j: x" \: i" E

    ' g" e% L$ C  h; {# D% z4 x" Ndef factorial(n):+ P. b6 O9 y$ H# e8 {
        # Base case
    2 O& }( R6 P  S. e2 h5 h    if n == 0:
    7 }( N: p  [0 K/ k        return 1
    6 G1 ^4 N- `6 M! a8 n+ }( W% k    # Recursive case
    4 U1 S+ R. v0 H+ G7 \" p    else:
    ; V' Z( a; Z- h! |0 }* D* ]        return n * factorial(n - 1)# n& L& o" S' a# z- o* I, F
    ' t' [. }5 i/ g# \. ^
    # Example usage
    3 T, j9 v/ ~# }4 @* N* Gprint(factorial(5))  # Output: 120
    # i# P/ }$ Q- j' ~. s  \  e3 S, e6 o! f" }) b7 V+ x. c- d% x
    How Recursion Works/ v( b% B4 N2 W+ c
    3 r4 g  {/ c# ?! t! `
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ; k- Q: I5 T4 K3 f2 H# o( u7 H& V* F8 |
        Once the base case is reached, the function starts returning values back up the call stack.
    & g# c. J" |( `! P/ j8 `
    ) e+ d0 N+ `, x  L3 X    These returned values are combined to produce the final result.
    ! h4 v! t& \6 Z) D4 B! [5 W( Q0 `" D4 O5 i
    For factorial(5):" y, r+ C' z9 y

    . q! O8 A5 P* |% _$ G4 ^# }5 {6 q! k3 \. A1 ~9 U7 U
    factorial(5) = 5 * factorial(4): q9 ^4 i1 {7 p! I/ R
    factorial(4) = 4 * factorial(3)
    8 o' b1 u" k* p! V- j* S, h  e0 U6 Tfactorial(3) = 3 * factorial(2)
    4 ^$ v$ @* D  u3 X$ p6 J. K4 Yfactorial(2) = 2 * factorial(1)
    & c" h5 O0 V. x4 ^8 cfactorial(1) = 1 * factorial(0)
    ; K" d4 u% W0 {2 a& ~& Gfactorial(0) = 1  # Base case
    & D4 W2 w) S: ?7 m# L* E
    - P# }' n0 _* @& g6 nThen, the results are combined:
    " R" k. D( u% E1 {1 n* ~( ?9 W
    * }0 v8 F- n' F: g( m  P2 s
    $ n9 b/ R) W7 b/ o# O' Nfactorial(1) = 1 * 1 = 1) ]$ s1 S( b& c2 W: l
    factorial(2) = 2 * 1 = 2/ A) \0 C7 O5 U& y9 T; _* }
    factorial(3) = 3 * 2 = 6( d- Y4 a) H+ i) X% _5 m& |( x
    factorial(4) = 4 * 6 = 24* F$ a" B( V3 C- T- s6 Q
    factorial(5) = 5 * 24 = 1205 X) x6 Q# j. B1 S! i1 a; R
    8 p8 |" I) ]# ~# `' v! d! G
    Advantages of Recursion% R: s. Z& J/ V

    6 q5 K3 A+ {( \* L& K    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).
    " W; ]) f2 Z9 F! z6 K' W- j
    , Q2 W9 e' W  U7 p' k% o    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    0 d; ]. T7 f0 @. L) v' j) @$ |# J$ W$ t" ^3 z& r4 R- A
    Disadvantages of Recursion
    ( [+ R3 X, H! e% l* Q6 e( e% v; K) d
        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' V0 K6 @( w& a9 Q) R" D# u  k
    ' N# L1 o* ]% P' y3 P1 r
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    # y3 j& K4 D+ ]* Y; M- Y& \4 j0 H7 `
    % t7 r$ s7 Z) U( J/ C5 [! A5 g+ TWhen to Use Recursion
    / ]4 I8 n6 A2 d4 Z
    : S5 D2 P) [" T; @* D, `: y    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 F% a3 N& Q+ Z5 O) ^& W
    1 U# i9 ?; B; F9 h& [    Problems with a clear base case and recursive case.# q. ~$ B- u, |0 h8 u

    & B4 \4 V7 w; p; \2 vExample: Fibonacci Sequence+ Z4 G% z1 i) t1 ]3 M! ?5 V
    % j. Q) U* x8 F& u- f
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:; I8 e$ M0 z6 }) b9 _) R

    & Q- m6 l/ o9 v8 g  P" z0 z    Base case: fib(0) = 0, fib(1) = 1" ]! E: v; P9 ~  m4 c6 V5 B
    " t$ T. s) w6 h
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    3 N: v8 m+ J1 b5 H+ j- I7 H$ H; f$ V( M: N1 F' i) x! j
    python
    8 m; C* k- v+ o) ~7 c; B1 t
    ( R2 z8 c, |0 n7 D) c! K
    , G3 p8 ?# s& p5 Adef fibonacci(n):. S7 j0 A3 O$ x, F- ~" _2 X/ s5 B
        # Base cases
    7 g0 B- ~5 D1 Q8 }+ _    if n == 0:! D  {  d8 |: [0 f
            return 0
    ! T6 q! e; K) B1 ]) V    elif n == 1:
    & E+ W0 i' X2 a0 ], E  T* _* l        return 1, [) P2 V$ E$ L( A# W* S; {
        # Recursive case' N# Q( p, r  H$ S" {, a
        else:3 l" r$ P9 L: q1 q9 @6 Y0 Z: U& s
            return fibonacci(n - 1) + fibonacci(n - 2)" A1 f3 Y% R# n/ [* y/ q

    + A9 u' M/ k7 d# Example usage& G' h. d& m4 ^9 ^
    print(fibonacci(6))  # Output: 8- `9 M- x1 f# M% S

    ; I# S/ A, s# CTail Recursion
    9 C: P+ Z" W6 _( e( J
    6 Y' C- k4 ~1 }6 Y7 jTail 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).& C. ]2 T4 @$ q# z# U

    ; v: y  L% G( S& ?( D# T1 \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-4-25 08:35 , Processed in 0.066490 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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