设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 s' u1 r  |) z, E: r9 u

    * p7 n6 N& W3 J解释的不错
      L9 k; R# P$ c: B; k, P
    ) N0 h" c2 t; ~# b递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。: i4 j& O4 h  j* s

    7 H/ [* t* P8 ~ 关键要素" T& H1 {' [4 T( u# e. S- E
    1. **基线条件(Base Case)**
    ; t# G" U5 z" ?1 `2 ?1 g+ V# y   - 递归终止的条件,防止无限循环
    ) n, `, v+ I5 j, y. X; W   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 12 k6 }' `1 D- q! N& L9 m& ^, d
    4 m  y/ D. j- `: Z+ W$ Q
    2. **递归条件(Recursive Case)**2 B$ y  H$ G4 `+ d
       - 将原问题分解为更小的子问题0 j9 V0 d$ a. S1 m4 B
       - 例如:n! = n × (n-1)!
    : h. J6 O% Q- x: N% y: R
    / {: s5 x! s8 j7 J# E, V" P, |% V' e 经典示例:计算阶乘
    ( ^) X6 t  G# ^python" Z: i2 y8 @( f
    def factorial(n):+ a- ^7 E+ G( k- W
        if n == 0:        # 基线条件
    6 q  I# `) B0 r        return 1
    , ^* p- D* _0 N( F, ]* X    else:             # 递归条件
    0 J. ?2 `6 a- `5 W' W- q" ?        return n * factorial(n-1)" p/ D& R4 h1 _
    执行过程(以计算 3! 为例):
    0 w: U; K1 l% d0 g- v2 t; @factorial(3)4 M$ Y# ^. J# Z! M: O
    3 * factorial(2)
    - F' y6 j: N! o0 g/ f: ]3 * (2 * factorial(1))
    ( e& _4 h3 Y6 K  y6 F+ C  s3 * (2 * (1 * factorial(0)))
    9 A0 L1 n( o* Q2 _3 * (2 * (1 * 1)) = 6
    1 v& i: H- I( `$ `, ^3 i) X. c3 o' f! K7 |& `1 j$ A. \1 x4 t
    递归思维要点2 M* o0 p! F0 j7 R
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! |1 M" e. N" Y. t3 f/ b0 w2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" \+ q& L1 @7 Y$ d9 n/ W8 b5 Z9 x
    3. **递推过程**:不断向下分解问题(递)# J+ P0 ?+ g, ^7 E2 |* W7 N' j9 ^
    4. **回溯过程**:组合子问题结果返回(归)7 S$ U4 V( \2 |8 f) d

    # g) n+ }/ Z8 j) w5 }3 i 注意事项  v- N" i8 D- z! S8 r
    必须要有终止条件5 E+ U" P3 d+ {4 |5 s
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' @- n. t0 C$ N7 I2 v4 J0 D某些问题用递归更直观(如树遍历),但效率可能不如迭代! o6 ^% J: \# Y! X/ N" G4 M
    尾递归优化可以提升效率(但Python不支持)9 A9 k+ F- D& n+ F* X" X( L( m
    9 e1 {" l5 B" T% M9 c
    递归 vs 迭代( i2 K6 N2 O1 R) `+ N. L2 |% g
    |          | 递归                          | 迭代               |
    ; @( l2 \9 `! J, o6 }# D) T; |* E|----------|-----------------------------|------------------|
    . h; k, B- b; \' |+ s| 实现方式    | 函数自调用                        | 循环结构            |
    ; \5 r9 Q! Y0 A; d. j: t  A! l| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 Q. g; c: D) G+ q, Z$ `| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- N" _5 A' n" t" ~( [" D
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! V* w- u9 z! {# {
    * O& V+ Z; ]/ O
    经典递归应用场景
    & }5 D9 j0 U- c" k) [, y1. 文件系统遍历(目录树结构)
    6 {5 e7 {7 y5 b6 o- r9 W) U2. 快速排序/归并排序算法& y+ i% B5 I7 ~0 [* m  \8 ^
    3. 汉诺塔问题% h7 X9 b! A" A) v& f# {
    4. 二叉树遍历(前序/中序/后序)$ O3 C% i$ H! V% t
    5. 生成所有可能的组合(回溯算法)
    * a1 i! K9 n+ q2 l3 {9 F
    ; x* I7 b& g0 O4 C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    4 分钟前
  • 签到天数: 3170 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ P4 O* |& P: A- c8 Z
    我推理机的核心算法应该是二叉树遍历的变种。7 J- P) j0 H& h0 K# e1 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:
    ( }1 }2 f( E8 m+ Z# IKey Idea of Recursion
    , b5 V6 [  v& S1 ~& Q7 y% d" e8 H( s
    A recursive function solves a problem by:% C3 Y" P$ c: T7 K1 _* [

    * M/ R( P' X9 B    Breaking the problem into smaller instances of the same problem.6 l0 R3 o6 ]1 S4 |
    ! g2 l9 n% T" s! \$ v9 [2 r
        Solving the smallest instance directly (base case)." f" [; h- p6 h

    ! k3 j6 {* m6 N3 n0 |    Combining the results of smaller instances to solve the larger problem.7 k6 k8 c% {. i9 e
    9 z+ N: v' g2 P: Q
    Components of a Recursive Function" }( A9 {% R3 l6 p' o
    ' F' U7 ]' T- `) E
        Base Case:. M4 P7 q- I" b2 h
    % T! I5 l9 B' v/ k
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 b# o1 H( G  |7 e; B6 a! W4 e, ^9 b" w% o' ]+ E* m
            It acts as the stopping condition to prevent infinite recursion.
    % E4 O: J) }( j. w$ [; w1 e8 o: R5 c( l: Q0 O( C& G5 A9 @
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! v' H! x  [' I7 p
    % b+ |! K$ v( B' o/ F! ~8 _. U
        Recursive Case:/ s3 }( W+ K7 ^

    $ p. \% O( B" C+ H+ }, k        This is where the function calls itself with a smaller or simpler version of the problem., q) k) |' J/ l) d2 k; D6 c( R
    6 E8 N9 w1 \# C
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).7 ^+ ]# X4 z2 }

    + m/ g) o5 d+ ~9 r4 A) G- z, Z0 \9 fExample: Factorial Calculation
    % x$ C: l3 v3 q+ j6 B6 m
    ; w5 g4 P0 l$ H! X* p7 X/ G+ \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:- W9 r$ w8 `0 K- A5 V; A
    / }+ f8 T8 _- S, L
        Base case: 0! = 1- ?* W3 {& z; v1 {+ {; Z

    , b- A- k/ ^; Y: {5 L    Recursive case: n! = n * (n-1)!/ @+ ?1 ^& Q8 W* ?$ T

    ) g& O$ x  Y/ VHere’s how it looks in code (Python):
    " B& _& O4 M7 Q+ v% qpython$ z) L. k: M" _8 K& @# S( g

    . X% X: d# v$ W- d. h* {0 V1 Y( O7 W$ a1 `
    def factorial(n):
    1 a" y4 k# Z# r+ B1 b( b2 _    # Base case; q7 @- x* y& A2 s% J! v  K' W
        if n == 0:
    & A, |# D8 G1 {' |5 `# {        return 1  k) p4 y0 v1 i# o/ e2 S4 G
        # Recursive case9 U4 l2 Q9 p7 k: A% ?2 z
        else:
    ' T7 H6 Y9 E5 p        return n * factorial(n - 1)
    - ~# h/ n% Z; O) C; O
    ' v" T8 a( p, W! D# c' h, ~, Z# Example usage9 Z# R" b) f0 u
    print(factorial(5))  # Output: 120) w% n4 {5 ~7 a' v. R- Z* @& ~

      y7 G1 k$ [) O( G7 ^( kHow Recursion Works* w: q/ j) T" g, y6 ]

    6 K: L# ]7 b" o' ?  f    The function keeps calling itself with smaller inputs until it reaches the base case.
    - l* G0 ^9 h9 E& Z  W: B! H' u" T& P. T" C6 u0 B
        Once the base case is reached, the function starts returning values back up the call stack.
    / d: I2 E/ u3 t# a! O; f& i6 W
    / O/ Z* L7 X1 o; v! y    These returned values are combined to produce the final result.3 {9 |8 G( F! e1 N) U

    6 I) X! A2 }- L* G! X  lFor factorial(5):3 I4 X. ?1 H9 f& t

    8 e$ l6 Y& K2 I# L; N+ X& }+ `6 }# }  v7 y* A8 V* u+ d3 l
    factorial(5) = 5 * factorial(4)  z8 e* b. ~0 M
    factorial(4) = 4 * factorial(3)9 y5 O# i$ O- D5 p7 F/ O  A
    factorial(3) = 3 * factorial(2)  h- D7 m. l5 [; v: `1 V3 x" ^
    factorial(2) = 2 * factorial(1)
    ' G7 v' n+ `3 B. Pfactorial(1) = 1 * factorial(0)
    % O5 P( q# Q0 Y& `; Z5 B7 l( ifactorial(0) = 1  # Base case% {3 _+ b) S5 Y2 L5 C+ \
    , e; u9 ~4 ], L3 m" f. B
    Then, the results are combined:
    ( d% ~7 w! Y! d) H9 m. d+ [, s4 _6 y+ N9 I/ F/ B! N

    8 j' `. U* ^$ I) Q9 tfactorial(1) = 1 * 1 = 1% ]( e) O1 C7 ^' a) [6 M1 X
    factorial(2) = 2 * 1 = 20 ]. U1 k4 T; Q- h7 z( S
    factorial(3) = 3 * 2 = 6  I6 s$ \+ Q! Q- M
    factorial(4) = 4 * 6 = 24
    - r8 s( k' g, Gfactorial(5) = 5 * 24 = 120( y9 k( H: _8 C) _2 S
    & K# Y) C4 T0 q) Z# T
    Advantages of Recursion$ ?/ }4 d  b: H& k/ ^3 `& b, W$ m; M2 q

    # ?7 s- |- S" R6 c5 \2 H% 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).
    , V; Z5 G! u- ]9 B- G- O0 C+ t9 Y* c6 N% F- h. _
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * A$ ~4 z3 ^; v% ^$ x# E( S% E
    9 p; h( {" s& I1 HDisadvantages of Recursion
    / r0 a+ i! X- \, p
    . m1 \5 \* S7 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.
    $ {5 [4 H5 C* F/ L2 S
    ; x9 _, e8 \, |, Y1 Y5 v/ F    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 `. S# O1 j3 y4 O4 m
    , u% h# b3 ^5 j/ M8 wWhen to Use Recursion
    0 @  D2 j) x& t
    ( W1 k; t+ \2 i/ o/ d' I    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    8 n7 N) K$ G% H; z0 [
    8 s+ G# g. A, S3 n% ]+ A    Problems with a clear base case and recursive case.
    & ?  v( e6 r4 r" y; V7 q% C
    & X! e4 {& ^! h* YExample: Fibonacci Sequence
    , V4 B8 D, e. E+ v6 p% N* Y2 U' M
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    7 M& x: `2 i% J# l7 I1 U
    , J, Q0 `7 e; `0 a0 c8 P4 c6 |; y    Base case: fib(0) = 0, fib(1) = 16 Y$ r/ R+ K8 j: C! Z
      t( o9 d9 z& |# y. M( a  @- g
        Recursive case: fib(n) = fib(n-1) + fib(n-2), L4 y9 n2 o. h  q
    . [  N# _! I6 T; a; `" E
    python
    % m' p, |0 Q4 F
    ) q, q$ Y0 o3 P4 G2 Q0 q
    4 I  m5 v$ U  n: m9 F% J2 ]def fibonacci(n):) l/ a5 D5 g, H
        # Base cases
    * ]8 I( h" |+ H3 A: Q    if n == 0:
    2 l' C7 k& H! v5 _. Z1 F2 X; Y        return 0, l* P# X; B% j: m
        elif n == 1:4 h/ P; W7 ?1 ~( i
            return 1+ K0 |. L2 J: A3 Z
        # Recursive case
    3 J3 p7 I! ]4 p; G; ]8 Y. N    else:
    ( X% d2 t, a# E$ [5 G6 A        return fibonacci(n - 1) + fibonacci(n - 2)+ ?6 X: O0 I+ D2 P
    " H0 h1 `1 f# ?* ]$ N. ^
    # Example usage9 N6 ^# v+ k  i
    print(fibonacci(6))  # Output: 8, E5 n$ D2 @0 b7 P
    ) p' V& Z9 Q4 z# \6 D; T
    Tail Recursion0 B( B: I- _4 D" ]. V
    8 F0 t, H$ h, x- [: X
    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).
    " a2 k8 U: m# y9 L9 J8 _: x5 Q  q; j: r; K& t
    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-2-11 08:18 , Processed in 0.056598 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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