设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 " N' C& q/ Z! o$ Y7 I

    ' {7 v+ P1 B* @# g& F解释的不错
    0 d, V% K, c, f. W$ }6 j7 m& l8 R1 I" ^+ x
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% ~3 Q: M7 \) [+ k* O/ K5 W

    , A' `7 y0 N4 \$ f 关键要素% u8 m; V! S' ~! g2 B
    1. **基线条件(Base Case)**
    6 r& ]. r- j  N   - 递归终止的条件,防止无限循环% Z3 U* @: V! w7 \4 ]) N
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    0 @4 Z- _% p( f  g- Z# R2 T" S  a% v/ ?$ g5 R
    2. **递归条件(Recursive Case)**
    0 {0 u2 A$ k/ L) G$ P5 p$ [   - 将原问题分解为更小的子问题9 Q0 w1 a+ S8 r: _1 ?; L6 l- \
       - 例如:n! = n × (n-1)!+ @' A) N+ y; `+ C6 ^# a5 l6 S
    * e. b; U- }( I) f. I" A( x
    经典示例:计算阶乘
      r7 H3 r/ m2 G0 Npython
    : K+ ~5 U* P* N2 O/ l6 d& {7 Edef factorial(n):( T; C6 {. w8 j5 D6 a
        if n == 0:        # 基线条件
    2 C( s4 C: b& k        return 1' I. ~+ {2 v, k: j' I0 N. Q
        else:             # 递归条件
    9 P" L' g9 R3 W% f9 g" `; \6 ?' M        return n * factorial(n-1)
    4 |$ Z6 y) X  t/ V执行过程(以计算 3! 为例):- R' w- X9 H) e/ Q. R3 ~3 o7 j4 g
    factorial(3)
    $ a' a7 H9 |6 R; L# P/ U, ^( `3 * factorial(2)7 A* X7 A) c* r( y* `! e+ b
    3 * (2 * factorial(1))! J) i2 q8 |4 j1 J" d' I
    3 * (2 * (1 * factorial(0)))
    # @% I! J& O5 D* k3 * (2 * (1 * 1)) = 6( w' _# t$ i- R% H

    ' J3 }, ?8 p1 C; D0 f2 w 递归思维要点
    ) ~5 [6 o6 R% j( L; s; S1. **信任递归**:假设子问题已经解决,专注当前层逻辑) l" h% M# R4 G5 r" j. T* F) x
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)$ J( O" a8 l& \1 l3 k
    3. **递推过程**:不断向下分解问题(递)
    5 Q5 y8 P+ M$ ~4 g/ X  v! V1 a4. **回溯过程**:组合子问题结果返回(归)3 k9 w- Q& |9 ?7 e. o( B

    ! x% t0 c- u! a# X! t 注意事项: {' f6 C# `8 ?
    必须要有终止条件7 J' _, J6 o, i' h4 l7 Q
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 e' O3 b  U2 e, P$ X( O- j
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
      E: O! o; {! J: c9 x尾递归优化可以提升效率(但Python不支持); m+ x2 g2 {! G* A

    3 D6 B- o/ H" c9 c" [( z 递归 vs 迭代9 n7 N) E8 v) s- s6 `
    |          | 递归                          | 迭代               |
    ! }# ^, Q( D1 [+ ~5 J! a|----------|-----------------------------|------------------|
    " B4 A( w2 m) [| 实现方式    | 函数自调用                        | 循环结构            |. U9 D+ B, i) S2 K
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, E$ e. O, G+ Q+ ?. x
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |+ M' y5 i$ r1 v) A
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 N% [/ l4 o; c, `- H9 R8 U+ g' q" W7 i$ ^$ X5 r( d
    经典递归应用场景
    : R! a5 q* h# B6 t, U/ V$ S/ Q6 E1. 文件系统遍历(目录树结构)
    3 s; D& c: }5 P! `, S8 V2. 快速排序/归并排序算法$ Q6 o& D7 ]6 w: z  {( p% z7 x
    3. 汉诺塔问题+ A; {0 h7 r) g0 x
    4. 二叉树遍历(前序/中序/后序)2 n" q2 p$ p" g, V3 T0 M- @( @
    5. 生成所有可能的组合(回溯算法)1 l7 N$ [9 w8 |4 }: }

    # ]% z8 u5 n% R试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 08:41
  • 签到天数: 3228 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, {* X* j7 j2 Q; [3 W0 E
    我推理机的核心算法应该是二叉树遍历的变种。
    . s% R, a# n1 |$ @2 X1 b3 ^+ P另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    + Y$ j! J$ A0 {* V$ ^9 nKey Idea of Recursion* F  s- f$ z0 `

    " K0 m" Z, o& k& v& g4 ?( ZA recursive function solves a problem by:) O) E# {9 q: F/ N
    2 A) [6 y3 \$ ~
        Breaking the problem into smaller instances of the same problem.. \* b" {3 C5 k

    - @* F6 E5 R) [5 v2 A' i+ A3 W& c    Solving the smallest instance directly (base case).
    ! L! v) L+ [6 z  r& g/ ?: w/ i8 _; w7 D- I+ P
        Combining the results of smaller instances to solve the larger problem.
    1 s8 W5 R4 q2 N! @/ b1 S
    ( S0 i2 Q2 H3 B% ^1 r# H' |Components of a Recursive Function
    0 H$ x4 Y4 d5 j+ @9 f4 _) j8 j0 I/ l
        Base Case:
      I. w' Y+ H3 v& O/ N" m* W: M$ h4 m3 y
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.$ O' ]+ f; X: F+ ]& W

    0 J7 f; P( {/ x4 d' p& |2 [% ?        It acts as the stopping condition to prevent infinite recursion.) t& |$ n$ m  U7 v7 R3 G
    / |- ]" R0 d  {2 u% |3 {
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( W5 I. J- h3 f! W, b( h

    . `7 F* b7 H) U/ \    Recursive Case:$ H$ }  T& Y3 V# x

    2 W% ?8 Y% g; b2 C* G0 d        This is where the function calls itself with a smaller or simpler version of the problem.2 i3 X+ K" p/ E3 W# w/ C0 E

    , g: z8 |' |/ Z' C+ U. L" G7 z! q  E        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).$ G5 p( n1 a% ?0 U6 j
    7 O# U2 P2 e+ l: V
    Example: Factorial Calculation
    # v4 I# r8 x7 v, I2 V; L
    8 W! i$ {9 Z  A( P- u/ q* 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:
    : ~  s' p8 h- X
    ! R6 x" p4 U; w, c9 e0 k& q    Base case: 0! = 12 d3 X5 w/ q. T; @) d- P! r

    ' C% l# ]3 u. K* S- n0 \    Recursive case: n! = n * (n-1)!  R( {  Q1 }% {& o
    4 i3 N! b- I0 ~/ `$ z& l
    Here’s how it looks in code (Python):
      {9 u" K6 j1 Y/ B' I, t+ upython
    1 X$ n2 A3 N# y# k1 |' ^
    ) ^" [1 [2 ^# _/ G' u
      C- V  ?8 S4 \def factorial(n):* `+ s7 {* k  G( M
        # Base case
    ( `0 F$ I* y% \3 ^" g3 @  j    if n == 0:
    * ]/ B& P$ H+ [( r        return 1
    % X: R- e- S5 N: ~9 W* G2 M( V5 O    # Recursive case
    . i- s# J0 ]# W  Z: E: Y5 m; I6 Q    else:
    6 o6 Z' P" X+ p( }8 g/ @        return n * factorial(n - 1)7 m9 _" B, O/ l& i& U
    & I) H( `5 y9 ~- c. @& k' h* D" x! F4 w
    # Example usage
    2 V( Q% R, p& A, }/ eprint(factorial(5))  # Output: 120
    : A% y% r9 o$ @5 x) o5 c: g1 x4 L: ]: @
    How Recursion Works
    + {0 b" d1 t" j$ u& f' P2 x+ d/ o2 O9 R: h
        The function keeps calling itself with smaller inputs until it reaches the base case.8 D. P% P; Z- X5 J- M1 O7 x

    7 g6 {( U5 k& X5 ]% E: V9 m# D; B, i    Once the base case is reached, the function starts returning values back up the call stack., p$ K9 V! k1 Z: u9 H% p
    7 V! G3 y! e; a7 p% m2 D) L
        These returned values are combined to produce the final result.$ E7 ?; a4 F1 R, }" K% R1 F
    9 b0 s; |7 U" _7 M
    For factorial(5):
    + U; T* {# M6 [+ w1 ^8 N# S! P$ n( a3 Q* i# R! ^; e1 Z

    - U6 f+ C* k7 B# K5 s$ u0 B' q3 v) zfactorial(5) = 5 * factorial(4)& a& F; }2 v4 T- h0 s* r2 S
    factorial(4) = 4 * factorial(3)
    , [6 C0 \5 C3 p1 |factorial(3) = 3 * factorial(2)' y5 K6 s# P# A- k8 F" Z  N
    factorial(2) = 2 * factorial(1), G8 X7 V* l$ y9 v
    factorial(1) = 1 * factorial(0)
    " o" E2 ?% ]5 \/ u, t7 |factorial(0) = 1  # Base case. L! Y% T; T3 ~% [

    4 ~, L6 ?: R5 t4 }6 hThen, the results are combined:2 g6 F* a% b  ^, ~+ B" P. Z0 n1 B$ Y7 j
    " f. o8 \& I8 Q* }

    8 ?4 |& i3 @2 h/ }factorial(1) = 1 * 1 = 1# _: I# {. C; B3 ~0 e2 q9 w
    factorial(2) = 2 * 1 = 2
    ; Z8 K7 Q' R5 k; m$ A+ Q8 w0 v! N& ~factorial(3) = 3 * 2 = 6" o* B0 K; G& q4 u; _+ X
    factorial(4) = 4 * 6 = 24; Y$ }6 B9 t2 s+ o; o9 W9 }
    factorial(5) = 5 * 24 = 1205 o; ]& M# I' B9 e: t

    ! N1 e# S5 K. v( u3 O% LAdvantages of Recursion1 H1 l2 Q5 U: e8 M
    + ]5 k# p7 F+ h4 s5 X
        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).# s- l/ p6 I5 ]5 p# x

    1 z* a5 P: u$ F1 C    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    0 H4 ?' |; z+ j8 R9 s1 ^9 r2 ]: b8 B7 l4 N/ E! c; n* y" S7 o
    Disadvantages of Recursion' _4 ~9 s7 W. }1 ]

    5 `: z7 L' n2 O5 S    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.
    . O9 a; K  D! u
    / n* Q# O, S( q& X& _6 c  f7 [    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).9 W2 F; `$ {; K' D! A. w) `

      N* `9 O$ h7 d' u; @: p+ jWhen to Use Recursion
    7 i" v) E* G2 m& b* s$ R
    4 \$ T" ?' a% m  {/ G, O+ z; \    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).4 ]5 {" i; [& ?& p" l: U. p
    ( J  ~! k  k* e. g2 ~2 P+ ?7 x
        Problems with a clear base case and recursive case.# e/ E. w2 {/ ?' m' w; D# z
    + ?6 ?0 l# _* y- ]
    Example: Fibonacci Sequence* x! y! f3 v) F* J& [. I/ U
    8 }' M! E8 ?3 j% h: S: w
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 W  u  p4 V" f$ g

    1 ]5 a# U8 D/ b) w$ _2 g" E    Base case: fib(0) = 0, fib(1) = 1
    $ E% b) \6 r9 X& y; Z
    , x) w' Z+ _/ c# V8 ^" h    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      h% u( k+ x% O0 x4 I* ?9 i# T; R/ q* Q  N; R! o
    python* C$ H7 Y$ G; d5 O
    6 Q* g( A' \% B
    - w$ R! W1 I+ D( M* @
    def fibonacci(n):" A0 T5 m. v) F
        # Base cases
    8 n$ P! v0 z5 K) W7 y; {    if n == 0:
    ) Q# m" p( \! I- N8 J" M: P1 p9 ~        return 0
    " M0 g9 M" p7 H- i9 D    elif n == 1:
    % r9 T/ D& N$ ~" D        return 1: ?# a' I' |3 M3 m9 v
        # Recursive case
    & K  z& @! g  e9 R: q! c8 p    else:
    . m4 p# H$ i/ |8 c# J  P1 I* V        return fibonacci(n - 1) + fibonacci(n - 2)
    # T  `  ^8 Y/ L, P! @# A% \
    ( M" F& s0 p* Q+ p$ W# Example usage0 h' }! A# G6 P0 @, s
    print(fibonacci(6))  # Output: 8
    " \; I& K) ~) z: ^2 C! |- z
    8 Z3 @7 X& s! v3 u5 d2 xTail Recursion' @& x# ~* K( S+ V, [

    5 \; L" B2 x- o) L8 n" g( p. NTail 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).' t# j+ d0 C/ M- I
    4 w: ?3 }- @) N( G
    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-5-2 16:38 , Processed in 0.060776 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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