设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ' f! c8 o+ D2 y7 C5 S1 ?1 W2 W9 q6 ?4 `; c3 U* ~
    解释的不错
    % d, j# Y/ K8 z: o- p2 h0 H% ?/ ~' H3 J& i
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    6 o. T" |9 @1 P
    6 B6 f1 _$ ~7 q, W9 t 关键要素* N; r; X+ z/ X4 o) |# R
    1. **基线条件(Base Case)**; N6 r- S  q4 o
       - 递归终止的条件,防止无限循环: H+ v3 k& Y! z% E! X1 v, ^
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 12 F6 M( T  l- e2 i8 R1 c* H* }+ P! V) J

    2 T0 z) X5 T' I2 D2. **递归条件(Recursive Case)**" r$ o7 `. b( H2 j" d' b; Q) P" m
       - 将原问题分解为更小的子问题
    % s' M$ L: D0 D- _1 d   - 例如:n! = n × (n-1)!7 F6 s. }2 J1 J! p) K6 J

    ; G; F) J( @" k* ]1 e; Y4 ?, h: b. | 经典示例:计算阶乘
    % i7 W8 M9 m' J; M6 k. Qpython. Q% _; b3 Z: u5 B' s1 \
    def factorial(n):- g$ H; Q# b% y# T: Q
        if n == 0:        # 基线条件: o4 m1 t6 |! P7 Q+ c8 x4 M
            return 1
    $ z8 E& T, ^1 K  j( Q( V    else:             # 递归条件
    6 i4 @- ~% ~* e0 A        return n * factorial(n-1): ?. r$ o( u, Z" _: z4 q, @; A
    执行过程(以计算 3! 为例):+ m! e" z. Q$ ], T, w! p* \
    factorial(3)
    - a% G* E- z' R/ l( ~9 }3 * factorial(2), U( F! F8 f0 G1 j6 c5 n0 D3 U
    3 * (2 * factorial(1))' U6 T# \* g$ g( R' `- m5 t
    3 * (2 * (1 * factorial(0)))# r) v1 u& ]8 x/ S$ S/ S$ v
    3 * (2 * (1 * 1)) = 6
    ; L8 |& Y0 c3 B9 k3 x0 _7 x6 p5 j$ n5 l; ]
    递归思维要点
    : K% G3 G; u& P" o1. **信任递归**:假设子问题已经解决,专注当前层逻辑" i8 u7 ?4 N4 X9 b$ u' y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 K# G' T, _0 e! p$ b1 l
    3. **递推过程**:不断向下分解问题(递)
    - W+ D# B% H& U+ ^. }0 s4. **回溯过程**:组合子问题结果返回(归)
    6 N- l5 O, J7 s" i+ N4 O8 X4 V$ ~# |4 J& Q+ b
    注意事项
    6 [% ^' B- w: y+ g+ F8 r9 R必须要有终止条件( Q7 Y, B! i# B
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    4 a7 u# b9 k- }- j某些问题用递归更直观(如树遍历),但效率可能不如迭代/ _( j3 j; m. h: H- s% z
    尾递归优化可以提升效率(但Python不支持)
    # R$ p3 J$ ~+ n% d! [4 @% ?& P
    3 b3 E" w- {; b( p# j 递归 vs 迭代1 f& a, `. B1 \: N9 S
    |          | 递归                          | 迭代               |5 ^7 [/ b! L9 q2 J- v% T: f
    |----------|-----------------------------|------------------|
    , T: Q, S4 K7 X' r% C+ \| 实现方式    | 函数自调用                        | 循环结构            |4 n! _' _2 t1 J1 x  a
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, I9 Q# W( F% R% w
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 ~3 J0 v2 F& f8 _| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 Y( H8 D0 I' F) ]! N7 ^
      Y$ W: Z& S; u
    经典递归应用场景
    % c( G' |1 x  l+ A3 d1. 文件系统遍历(目录树结构)  o4 R! Q! M! }8 Y* J
    2. 快速排序/归并排序算法* t, n$ a- M  t/ y6 }/ V1 Z
    3. 汉诺塔问题
    1 G/ u8 Y& S6 R2 B3 A4. 二叉树遍历(前序/中序/后序)- ]. ^: ^7 U) A8 w& g
    5. 生成所有可能的组合(回溯算法)0 o3 d! K8 G" a! Y
    1 j7 [3 T7 h( r3 p: \  Q
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    8 小时前
  • 签到天数: 3245 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,9 U% Q' A5 @: z: ~. P+ N) D
    我推理机的核心算法应该是二叉树遍历的变种。8 u1 T% z' `0 W% R# ?
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; |  o, B) A9 r  X
    Key Idea of Recursion+ `- U- ^1 k: i" I; v& F/ Y

    & O9 `  Y% }+ g8 Y- I' kA recursive function solves a problem by:, S- K" x* G" ]/ n, }4 X' v
    2 I- ]7 g. g  K& R
        Breaking the problem into smaller instances of the same problem.$ J  K1 e/ P% u0 y

    7 p8 A$ u7 F! |' z# ]0 m! H( ~7 x  f    Solving the smallest instance directly (base case).# n! U. r0 H7 V* m% P! u# J0 j9 i, \# [

    . x/ T6 ]8 g0 x' L9 o# Z/ [    Combining the results of smaller instances to solve the larger problem.
    ; \3 W: Q, t$ _8 w/ O# @( R# B- O
    Components of a Recursive Function* t6 B+ |  ^# f& O1 \, a

    4 Z" t& ?2 g1 A0 w9 E    Base Case:" ]& A+ f' L" C1 a, T; N

    & ^& [( }, J4 m4 T: e        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.+ d7 H8 U( q* @! m# ^4 ]

    . }) |8 M0 ^5 U+ ~8 Q& O3 U# a        It acts as the stopping condition to prevent infinite recursion.
    8 A9 \0 S, Z' w9 [
    ! N  F; {- R/ J        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ! F* ^& Q7 T5 X+ ~" _. C. m, h" D  s& i: V; @; w
        Recursive Case:! A8 i0 U6 H$ Q% G

    8 K' T/ [3 ?0 M! H* z        This is where the function calls itself with a smaller or simpler version of the problem.' Y% F  Z0 @' [1 l  C- r, t8 d: N

    ! K2 d$ M" _. V        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).( [4 p$ ~8 o% H5 B2 l6 n. b
    / `! _2 @1 C! w
    Example: Factorial Calculation" T5 A. ]9 B; @% M" L8 u/ I% [

    9 Z/ C/ j  s2 m$ n: r4 i# CThe 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:
    1 {. o6 g) _  n) B+ }6 y8 O# H( o% L: K. p$ [' b
        Base case: 0! = 1: J) }" M( P  q  j9 z7 z

    , L  d0 g+ d/ u: l( o, N, R9 M    Recursive case: n! = n * (n-1)!
    " P# ~4 ]! ?, M& X2 f6 g( ]
    , H" U; K; o9 ~6 b. fHere’s how it looks in code (Python):
    # y0 |! x) ~$ J2 B; K2 z8 A( Gpython$ o! l( v3 S/ T2 Z# H. s/ G* i4 y
    ! `3 q' k; e8 A- z
      \; p2 P% P6 @1 i2 d/ H8 o
    def factorial(n):
    % k" u) B4 J6 M" V: I& ~    # Base case/ T- N; v& I# F5 v! m, f# l5 I
        if n == 0:5 m1 l7 a6 r/ d3 z: k
            return 1- S" M5 `8 E' j5 f+ V+ K+ ^# s
        # Recursive case
    % w: E  I# a- v1 T# T    else:1 x" J% P3 Z3 o0 K; H* e" c& y- ~+ @! O
            return n * factorial(n - 1)# \& e9 }( b1 n5 U1 j6 x
    ) ]) S5 Y5 V4 i# \
    # Example usage
    - Y; o* a$ x! ]4 Eprint(factorial(5))  # Output: 120
    0 t- G( ]9 e' W( L/ |) `, M) `% d& n5 N9 ~  g0 K2 |& j4 K) C
    How Recursion Works5 U4 G( ?+ x; L) c6 b: N

    7 F* r7 _4 K8 u" }$ U# X    The function keeps calling itself with smaller inputs until it reaches the base case.
    + [4 H" a8 s8 S% @* J8 w; U$ b0 {; ~: B  K. e- V5 A* ]  x% P
        Once the base case is reached, the function starts returning values back up the call stack.+ O/ j$ G% \9 @$ i* y

    ; x2 W$ _# J7 a( g# G8 z* m3 y    These returned values are combined to produce the final result.
    * w! P& }; I: H
    " V, x2 {' M/ u# C. |. ZFor factorial(5):
    5 O0 d: ~( f0 M. _$ h2 ~) F5 r8 M6 i+ B3 D! g0 M0 ?4 K$ y, B* O( S

    ' g8 m! m  D5 J7 L) F& Afactorial(5) = 5 * factorial(4)( Y; R( W5 j+ K; o* [; y* `
    factorial(4) = 4 * factorial(3)
    ( P& v( [! j' j% y: [' n" Kfactorial(3) = 3 * factorial(2)
    6 k, V1 Z4 w( J' e3 lfactorial(2) = 2 * factorial(1)1 I, B' S* D$ A1 g
    factorial(1) = 1 * factorial(0)% }8 L8 C0 U8 c
    factorial(0) = 1  # Base case- b. O- }% e+ U$ }

    * N3 i, {2 i9 a( p7 a. j- V1 TThen, the results are combined:' ^1 t. S4 I' f

    - W$ B; ~5 X  Q, Y0 \/ ^( Y. B- C( \0 F; {. D9 T
    factorial(1) = 1 * 1 = 1
    ; u; g; a/ F+ j- a+ s$ U' s% Nfactorial(2) = 2 * 1 = 2
    * w# ?: P3 R2 M( j9 \factorial(3) = 3 * 2 = 6* l- Z; t+ |* A2 N/ Q
    factorial(4) = 4 * 6 = 24
    # J# V5 u( e; d; ofactorial(5) = 5 * 24 = 120. r" A& \, T. G. G/ _

    2 C9 V! [6 r: w$ ]4 WAdvantages of Recursion# \* \. ~3 n2 W4 c
    ' N" y* p, ?4 H& u6 ?9 Z
        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* k* N+ d4 z& c: X, a% H. O- U) {4 ~& V1 s
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " T" `1 M7 L0 j2 U8 b" u' s0 F5 e1 Z+ \) G5 `1 \1 {) m* J
    Disadvantages of Recursion+ V' o. I1 p* O4 w4 m) Z
    ! h( W3 ^* A4 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.$ G. a4 K+ N3 u& }. m5 u4 i
    * c% n7 m0 C& Q/ G( B8 L" Y& ^  n) F, `
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    , n0 D. |' Z# Y2 [* u) w8 V( {- g
    . @& N3 m. g" J! m0 a/ gWhen to Use Recursion+ S# J: N& ~! {8 U5 m5 f

    8 g) D  K, o+ n8 N2 z! K% J2 z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    $ F$ w# m3 A5 h, ~" [# B4 {% U; @- ^% {3 k0 x  R
        Problems with a clear base case and recursive case.
    : G3 [! U3 t( `" A% ?
    * f# F* s) }' C7 e! {# LExample: Fibonacci Sequence
    $ S! b- Z. ]: d- o( N2 X9 ~3 P4 ]
    ( o9 [6 T# {6 {+ W2 M* MThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 u: E) r; R/ L) J4 @7 E( M7 }
    3 I9 A9 T7 q: A  F7 F8 Q+ z: d
        Base case: fib(0) = 0, fib(1) = 1
    - a- K5 f1 W& W( j0 Y, v' n/ B8 c  R: v. j; |  A5 ^. Q9 M
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # ^8 {2 ~  |* Q$ u" o" y+ B0 I
    python
    % ]  Q* u# a- V8 G/ V, S
      }8 k/ \* `: p! c1 n& _- [9 Y4 t$ [# _, E
    def fibonacci(n):
    ' z( Q6 @: D, K/ y: R+ g    # Base cases
    9 a3 f* a3 Q5 v7 A6 E, R/ ]( b    if n == 0:1 `, D" h7 b- X$ @5 M
            return 0" K1 V, i# n0 e0 ?
        elif n == 1:
    6 a8 k- I$ @0 k( S        return 1
    4 b, \! l& n$ i/ E9 Z    # Recursive case* G1 v& }% _1 f0 q2 |, @5 h
        else:5 l% C/ {" s" U' r! w- c* S
            return fibonacci(n - 1) + fibonacci(n - 2)
    " i& ^/ d( c3 R) z6 e0 A2 z8 V6 B% |% g
    + d3 J! u7 f6 [5 Z# z* k: a% y# Example usage
    * `6 H) Q4 o1 t. B! ^print(fibonacci(6))  # Output: 8
    # v. f9 q  n8 L- Z* v8 x* Y/ ], L( L. i' w5 i: j% E+ Q
    Tail Recursion( o- f5 c* C! f2 o& y8 v8 [

    2 |  H# k, Q( V. ^3 U$ CTail 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).
    " C5 p; v  ~4 W$ _
    4 ]! s) N; K8 C& k; T4 R, _( {6 pIn 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-5-21 15:27 , Processed in 0.063004 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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