设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 9 P, j) c2 S8 d  |, \: y
    3 J) [" P  S7 o4 d
    解释的不错$ L$ A& S7 }: U- [3 L4 b% X
    ; k7 ?, b* w0 H' v
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。# u3 y4 a- x0 u

      T7 q# k3 ]# x* t( ]9 D 关键要素1 v, _* [  [/ d2 Z1 H
    1. **基线条件(Base Case)**1 g5 c! H& C* I5 Q& I( z9 `; A
       - 递归终止的条件,防止无限循环9 X3 X/ ?7 \; ~, e- t' F
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1( u9 I. T: U+ f; d$ {. I
    3 c* G  c" |7 A" ~3 |
    2. **递归条件(Recursive Case)**1 A/ k7 ]% [0 j6 Z, x
       - 将原问题分解为更小的子问题1 k* n! T3 d2 @# r2 |: R
       - 例如:n! = n × (n-1)!8 z9 \% b" t" u9 Y2 {( Q$ U
    % A8 B' w  h$ A0 }! G
    经典示例:计算阶乘
    5 g, u6 _, T6 }6 o% Kpython
    4 R. D6 T2 S& h4 m+ P/ G. R8 Kdef factorial(n):
    6 Z8 i* ^* R1 ~    if n == 0:        # 基线条件
    * x* m7 O3 P- o5 n0 o7 M0 F        return 1
    : r: [. M" Z4 |. k5 G    else:             # 递归条件
    # S$ u% T. m  O; S, b  H5 w4 W* p        return n * factorial(n-1)
    0 q. i6 C6 C. O) N, `) w' X执行过程(以计算 3! 为例):: @+ g. v3 H3 U2 `
    factorial(3). b* C7 S8 ?. ]+ O& H  `: ?
    3 * factorial(2)+ O1 @4 G) O7 z- d, c# ~2 |( w. F
    3 * (2 * factorial(1))
    ' u6 ?5 r- N. r' N, E  n! u3 * (2 * (1 * factorial(0)))
    5 T9 j5 o7 I* S- J/ Y' g3 * (2 * (1 * 1)) = 61 m, e( G2 \6 r6 l" E4 j% [7 I* q
    : m# \& M) F; ]# ?- i0 _
    递归思维要点! b" o# F# R# h( V
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑1 D0 t' e4 W# k, j% V: s
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ) T) h& t! R/ ~% s+ J7 A: @2 P3. **递推过程**:不断向下分解问题(递)$ i0 K4 K; m- x: P; L& t0 T9 u9 C
    4. **回溯过程**:组合子问题结果返回(归)6 w. w3 x) A7 N+ Y

    % d; w2 C- W8 G7 D( C 注意事项
    " A) |4 ]2 s: V; p必须要有终止条件
    + ^6 r2 j! j( Q0 |递归深度过大可能导致栈溢出(Python默认递归深度约1000层)* i. x6 {% A9 w
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " b4 P; B0 B3 c1 O尾递归优化可以提升效率(但Python不支持)$ Y' d; r* {8 y$ I! U
    2 A* R  B: H- H, A6 f- d& A; x8 J
    递归 vs 迭代
    2 p% i! l- l% ?9 l: D8 t|          | 递归                          | 迭代               |
    & v& Z" _- i+ ^- ?|----------|-----------------------------|------------------|
      Q  K- H6 v3 x6 i% k8 w' F4 m| 实现方式    | 函数自调用                        | 循环结构            |$ r: P0 W' ~0 g& [* a
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ F5 h. J$ R9 d6 ^" L5 r| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; [& H2 n* h9 Y6 M| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |0 c9 B  w; I* }2 f8 O- a

    6 ?9 t! |, ?6 s7 j4 E* m1 X# ] 经典递归应用场景1 q6 x9 ]8 U+ @( d' w8 h5 c% Q
    1. 文件系统遍历(目录树结构)
    : f  f# |9 ~- [5 S2 P2. 快速排序/归并排序算法1 [- ^8 P$ M6 n( E
    3. 汉诺塔问题
    / w3 n9 w2 W( h. _( W" _0 M4. 二叉树遍历(前序/中序/后序)
    6 T1 ^0 ^, c) x2 o( c% z5. 生成所有可能的组合(回溯算法)
    % S$ d3 |8 u  e- `' [# b% b4 d* I+ Z6 o: \" O! j
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    . |# v! I3 v: b; U我推理机的核心算法应该是二叉树遍历的变种。8 r8 y  u% q  F3 H8 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:7 W- ~' l% n2 q& v; D( X7 L( l
    Key Idea of Recursion8 x9 F  ^* {9 F# x  \% Z

    3 V9 d/ r& u6 J9 H# jA recursive function solves a problem by:% H7 \) _, l9 T3 U* x

    9 t5 ]8 z% A5 H* z) Q/ o, L& f+ Y    Breaking the problem into smaller instances of the same problem.3 ?( }; \7 \$ o

    / j8 N7 J) z+ h& R- \    Solving the smallest instance directly (base case).
    , p  X7 |+ N; j" w
    ; X6 R# o, T5 [    Combining the results of smaller instances to solve the larger problem.1 j/ e" p- W" v$ M0 a# Z" p" m
    ) i6 d' T1 q! i
    Components of a Recursive Function  W) H5 V# N1 s
    " `4 t* F# r4 B: r5 Q- j! i
        Base Case:
    6 b) n& J6 j: o3 O) r% P0 I
    3 [- z6 p- b& V& L1 m3 N/ ?9 P        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& ?1 t( S  r0 S/ ?* [, i
    $ B' G  ^0 a0 b6 Q
            It acts as the stopping condition to prevent infinite recursion.
    ! R  Y  H7 |% y. E3 I
    7 V8 ~% ^" i5 N! L3 Y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ( T$ k- M% z; v1 H- I/ v: h5 q( g3 }) f# ^# a* e' n" n
        Recursive Case:
    0 ~9 J9 `! j8 G5 w3 g+ {; x
    & D6 @1 U* W' K& L6 a        This is where the function calls itself with a smaller or simpler version of the problem.
    $ i+ H7 S& r% }- ^) B/ ^9 d% Z6 ?3 d0 [; f5 E) B# X  n8 e2 K
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    8 N% l0 e6 C$ ~' E5 {
    6 o+ d6 b4 n& d) e& EExample: Factorial Calculation
    6 j; q9 y$ z4 j% ?' {9 X$ _: p6 ~' u( E
    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:
    9 O( r4 A, O# R8 i0 X+ u- X/ _5 H# B' J3 g9 W
        Base case: 0! = 1/ C4 h( ~2 S/ A7 o1 T! `
    7 J, W) Q' q1 ^: p" u
        Recursive case: n! = n * (n-1)!8 @4 S1 n6 x1 v. i) b
    3 b: l5 j( F9 _; V, f4 D3 {3 l
    Here’s how it looks in code (Python):$ W, ~$ ?3 T! [" x; A' Y: F* P
    python$ G; f2 g6 O% q' Y. W2 b8 v

    ! h% r' |/ U! q9 ?
    ; R' Y: f9 b0 d% hdef factorial(n):
    % |* |) F2 z8 K. e" ?* [    # Base case
    - e$ k! V" o7 e3 W( e( g    if n == 0:: L3 k! s8 I6 B  q; o/ o) O
            return 1  n/ x- A+ o* |; m0 S
        # Recursive case
      V( q* X' ?5 X0 @+ g  v    else:; x0 `7 p' q$ \( D/ E
            return n * factorial(n - 1), ?- D9 I, ^0 M; D3 h: x
    6 E+ C9 ]6 m8 V2 _" v7 y  S
    # Example usage! Y* r8 J( }% i( n# u/ K' S4 {
    print(factorial(5))  # Output: 120
    - n8 `4 F% E9 K/ E! {
    * k- W7 D- D) j- GHow Recursion Works
    & ~0 m: T+ j+ a! u0 n) X, z/ J- D2 |1 E) Y
        The function keeps calling itself with smaller inputs until it reaches the base case.# _' }, o+ m( E6 f

    ) M6 k! x3 b+ g: P    Once the base case is reached, the function starts returning values back up the call stack./ r0 _# s3 |" b# a5 V' |9 y
    , z5 j+ D3 u: g8 V: P
        These returned values are combined to produce the final result.7 C: x6 @& q+ q9 c

    " r8 }1 y% x; h- W$ JFor factorial(5):
    / i0 c; j0 M2 i+ h2 G9 Z
    7 ?2 E9 u- X& @: e  R( m
    0 d' i/ q1 ~5 v  U4 i1 k: afactorial(5) = 5 * factorial(4)8 C3 u' {2 [" p
    factorial(4) = 4 * factorial(3)
    + @5 v( q8 ^+ ~" j$ Cfactorial(3) = 3 * factorial(2)# X: m  G) V) \$ r3 d4 ]
    factorial(2) = 2 * factorial(1)
    4 R4 }) f" a$ d( X. G" afactorial(1) = 1 * factorial(0)) Z, E$ f' v/ a  T" K
    factorial(0) = 1  # Base case
    7 k3 U$ s+ K' B; \2 p* U
    & }$ j9 F9 m7 G4 V. n4 tThen, the results are combined:# {/ g7 m+ Z' ]+ s8 ^

    4 t- ]- B, F2 [- |
    ' |# U2 z' q$ t9 ^factorial(1) = 1 * 1 = 1; [+ E! ~8 Z' l2 n1 ]
    factorial(2) = 2 * 1 = 2/ i0 k) {" i, u) S. p+ }5 B
    factorial(3) = 3 * 2 = 6
    ! i1 u( k% x: ~' x/ _factorial(4) = 4 * 6 = 24
    4 Y& K/ n. l6 p9 Y9 C9 Yfactorial(5) = 5 * 24 = 120
    1 A# w0 b0 K. v+ B% u' D1 N  g9 _$ A( i4 m6 r+ n# Z
    Advantages of Recursion
    9 g% w- e5 W) L% n. I: b' I, l* D5 P  D" a  d$ m2 `
        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 y# B$ Q# F* G% X  d
    5 V$ A' m5 `) L8 j    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    3 N. r; f) c7 {2 [, i% e5 I. f! M! e, X
    Disadvantages of Recursion
    8 u; n: L. s9 ?' }- h9 f; G; r1 P% D" t! N$ g+ 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.
    " s" w  @2 p. k0 p8 X* R, ?& n, z" l* e$ F
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    % l2 I% O) z$ N: a' v
    ! q* d! ?- E8 j. O/ }When to Use Recursion9 V. n% Q" i. k! C! m0 U7 a1 D
    . `. x' _: y- N# j0 v4 }
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    6 p7 o$ q0 L" k9 \# G5 b$ o" ]6 H+ q1 |- q, A3 h  a: m8 Y
        Problems with a clear base case and recursive case.
    3 U" }( X, L) K& c. J0 f, c* Z8 G# h$ U1 t& C- _4 p8 R# x& u8 I
    Example: Fibonacci Sequence
      o+ [% X# \2 ^1 \, H& |
    3 `& P8 b  \! |/ B2 dThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    % A6 b7 e" g# w- x/ q  [
    7 x2 G  T9 c4 B0 S4 D& {+ q0 p    Base case: fib(0) = 0, fib(1) = 1
    8 B$ I* ^$ g8 _* P8 d2 \* }4 s# N, Z" N
        Recursive case: fib(n) = fib(n-1) + fib(n-2)$ T1 r! l" e5 [6 X, Q  w
    9 ?( J$ Q" J4 t7 u# Z! P
    python4 L" {% r/ I" K9 L
    ( i% }7 |) `  g# C% V% P
    . i% i/ {, `7 m/ ]
    def fibonacci(n):
    0 O% L6 C; g, v    # Base cases
    / `* _0 v9 v6 B0 c! S    if n == 0:
    ) N8 {. L$ m  T        return 0
    6 I' Y: }! n( ]9 _; B3 r- z    elif n == 1:( B: h) u1 y9 M' z$ |% F0 e: v2 t
            return 1
    0 g$ T  D4 A# t# U    # Recursive case
    ' Y2 q6 j, s9 X$ y    else:* D6 k; j" L" \* s
            return fibonacci(n - 1) + fibonacci(n - 2)* N5 {0 _) I. k. p0 l3 [  C* A

    / B% @5 B% C; D5 O. J  ^3 |, j# Example usage
    7 ?! W/ m9 ~; M# v4 |5 l  qprint(fibonacci(6))  # Output: 88 }. w* l2 @( G
    6 ^6 A3 I0 |" |! u: O  w
    Tail Recursion
    0 b+ \3 c) P% L" p9 m9 i+ A9 w1 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).
    5 g/ s  ?/ `3 f+ l: h" B0 @
    0 v1 C4 Z% v$ a# ?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-13 18:27 , Processed in 0.030331 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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