设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    # f, M/ W; f# e# k" S2 W5 t& x( B: l% c+ k( D" A% z/ W6 Z
    解释的不错
    ) g. k5 P9 [% V( q5 R0 p/ z2 ?0 ]8 s, ?! n  J
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  R1 _" Z% a' A  |) @

    7 e. X- C! E% _, W9 i 关键要素1 s$ P/ q+ Y. d# n& i9 x+ |3 \
    1. **基线条件(Base Case)**
    ' h3 J$ z6 v0 r1 r5 L$ u- X2 V   - 递归终止的条件,防止无限循环4 J' g7 R$ p9 E- Z0 B0 x
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1  S- v( O  _& X8 K( o$ X# M

    9 t: i4 G" N9 w) f" V& F% n* a2. **递归条件(Recursive Case)**
    7 I7 R0 }0 o: ]: o' `9 U6 h8 F   - 将原问题分解为更小的子问题
    . v  x) V, ^0 b- |  L) {! L4 \   - 例如:n! = n × (n-1)!* O: K" W3 ]/ v) _% @
    " d4 k! V( [9 k* C" K  R9 [
    经典示例:计算阶乘
      ?( p" v( L8 bpython
    & p" ]( \* Y# m8 cdef factorial(n):( j$ o, L% P7 L: z6 w  q% p
        if n == 0:        # 基线条件: z3 t0 K9 E: ~9 q  N9 q
            return 1) u7 A. T8 v8 F  C6 Z* i( {- P: B3 h
        else:             # 递归条件
    ) P- F8 U3 W8 s  U2 r        return n * factorial(n-1)
    * x, k1 j' d+ m执行过程(以计算 3! 为例):
    * B+ J# b, H" D9 K  x4 S1 tfactorial(3)
    - E4 s* W) d# i& i7 R9 S& X- D) e3 * factorial(2)1 {, Y( r" E% }- M
    3 * (2 * factorial(1))
    0 l# N3 d! Z7 \+ f; f( Z3 * (2 * (1 * factorial(0)))5 [( M8 ~! ^& c3 j
    3 * (2 * (1 * 1)) = 6
    & O7 c8 X; ^/ Z; c. {# \& ]1 p% P
    5 E2 U1 V( j  K- L  d3 h 递归思维要点! K; }! Y8 _( @/ E- Y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! d: J1 K/ e% z) a. J2. **栈结构**:每次调用都会创建新的栈帧(内存空间)2 e- l" Y$ w* c- O
    3. **递推过程**:不断向下分解问题(递)* m4 ]: ?. z; K) k
    4. **回溯过程**:组合子问题结果返回(归)
    7 U( V4 i( x4 o+ ]4 Q+ h3 c
    , e) F; R5 i! i0 k) E" _ 注意事项' r- ^+ \; w. O" F, |1 j/ q% T0 E
    必须要有终止条件
    7 p" n6 P5 c  Z; |6 y递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    " C8 ~4 w* F  D/ ^# g# x  N某些问题用递归更直观(如树遍历),但效率可能不如迭代
    $ O" `$ N( v$ G+ _尾递归优化可以提升效率(但Python不支持)( y  x1 B" h; h( K; B0 ?9 U& V) h

    3 q" q2 y  T) s9 H% v/ f7 Q' I6 ~ 递归 vs 迭代
    - C6 J' g& b  R& @* q: r4 D|          | 递归                          | 迭代               |! o7 U! o# u. v, o# T4 |
    |----------|-----------------------------|------------------|
    * n2 D8 K' i. L$ W/ y| 实现方式    | 函数自调用                        | 循环结构            |
    1 h' P( c  \/ Z& J- e| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |$ V4 b* V1 i4 \$ M' ~4 v
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |0 m. b1 ^) u7 Y
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) s) H) i* `& U
      f* \0 g" Q& A 经典递归应用场景2 s6 ^# y2 v# N2 L- X
    1. 文件系统遍历(目录树结构)
    ' S( S/ g. w3 {' u( ^- B0 [2. 快速排序/归并排序算法% o2 ]& ^# _$ j9 r
    3. 汉诺塔问题6 g3 d  _4 Z/ @4 r
    4. 二叉树遍历(前序/中序/后序)
    6 ^$ R6 E( j, _5. 生成所有可能的组合(回溯算法)' O  W2 O/ {+ |

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

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    10 小时前
  • 签到天数: 3112 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    4 p8 ]2 \6 g5 ^9 ]0 x我推理机的核心算法应该是二叉树遍历的变种。' N- Y- F( c8 c% u" 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:
    7 K/ L$ a4 Z' L5 Y8 i: ~3 FKey Idea of Recursion
    0 T/ r$ J6 H  M) ^/ Z: @+ r) K1 V& S# W, v
    A recursive function solves a problem by:
    ; j3 _7 z8 q' J% Z# L+ O  J+ Y7 {, }
        Breaking the problem into smaller instances of the same problem.
    ; Z/ o9 B, x' w- ^/ S4 l( w$ C! {& F, o# S
        Solving the smallest instance directly (base case).
    0 v* {0 k: u. N7 z% P9 ?9 |; x& x0 a" ^  K( ^( V* c' X
        Combining the results of smaller instances to solve the larger problem." K0 p# H) N9 T' y

    7 P: n& i  i5 M* w* {" i( }. B$ mComponents of a Recursive Function6 t$ N! D0 f) e
    1 [# m9 s9 P& h% }
        Base Case:
    0 s9 W$ q* d. e5 y" A2 ^+ h& X# r9 N  Q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: c4 \/ Y" l$ l5 v0 r
    / h( Y5 l3 B6 y. [! O
            It acts as the stopping condition to prevent infinite recursion.
    , Y# @) ^5 L" R; f. }2 Y. t4 \8 x0 k& X- M0 ?
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1." `. t0 f3 B0 ]6 y9 v1 k* d0 G

    4 O6 U+ a+ H  Y, v! [1 O; w3 t    Recursive Case:
      ?0 k$ ]9 ~/ U& e
    6 F' @& b7 m, B% ?6 K        This is where the function calls itself with a smaller or simpler version of the problem.
    ) T/ h+ h: p- t0 T$ C( B6 X, h# H9 F( Q0 W$ c6 A% M+ y
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 W( i# X( w: \1 c2 v3 G7 x

    & s- h; _- u0 `% ], \$ OExample: Factorial Calculation
    * v. K& e& q! v. R; G' S& c! x$ _% ^, `$ |. T# @" o5 |8 d
    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:6 O: `' }, c8 f/ T) v" j

    7 j+ y0 W6 w. L5 Z. F1 {    Base case: 0! = 1  M# L1 t% E& D  T3 d2 d3 i- N
    * M% Z' n- U0 ]  D( v
        Recursive case: n! = n * (n-1)!
    / u/ R# E  g% y) Q  }$ b9 o& ^1 v. [6 p) @; W
    Here’s how it looks in code (Python):  ^6 E) e" T! Q, {  [4 a
    python3 W; c& ~( E* e( `
    - w) T2 w" O4 Q( i
    * H: \! p$ S* m  M# G' R. S
    def factorial(n):0 o' s( ?" I. w5 f- \7 J  Q+ j1 u6 S
        # Base case
    7 H9 ^' R% L  _# ?# ?, _: n  ~    if n == 0:2 @+ {+ e; P/ b# \" {
            return 1- X/ O  ~! i" j
        # Recursive case
    2 B% M1 h- J& t) Y) Z+ W    else:
    3 ~+ c/ d; ^" r; K        return n * factorial(n - 1)
    / v3 q0 Z( K0 H& o7 e! d6 }4 a- b" e5 P8 x2 H: j  k( b
    # Example usage. D( N. `% Z5 j3 d) g; A
    print(factorial(5))  # Output: 120
    / q7 ?, b: r, A% e/ w
    $ {4 X$ {4 Y( I6 X0 DHow Recursion Works
    ! ]" `# \, ?) ]+ B  P
    0 a  G7 c$ j0 F    The function keeps calling itself with smaller inputs until it reaches the base case.* r8 z( G! ^* E4 ^
    / n  s7 p( L7 r$ h7 z% \! c3 L
        Once the base case is reached, the function starts returning values back up the call stack.! |" J, K- Y: Y6 o7 z
    4 |; Z4 }. I9 g
        These returned values are combined to produce the final result.( p3 Q$ H% @% t6 b: a

    ! K& x! T+ X; f# A0 HFor factorial(5):
    # v8 m5 `( V( q( i% l( P/ y+ a& v; B$ e: g4 j
    ; @/ h* z% j/ t# g% v
    factorial(5) = 5 * factorial(4)
    9 D- l$ X2 u+ ?* V2 U8 s# Gfactorial(4) = 4 * factorial(3)
    / N1 q. L& @2 sfactorial(3) = 3 * factorial(2)4 e2 T0 {. m* P& A( Z
    factorial(2) = 2 * factorial(1)
    : c, S" n: a3 @, Wfactorial(1) = 1 * factorial(0)1 v; `0 ?* _+ M
    factorial(0) = 1  # Base case
    2 L5 J$ w$ D6 e, A/ k9 |0 Q# k
    8 \* }( y' u. L& s5 RThen, the results are combined:- S8 Y. }! B' g/ R" r1 Y
    , z2 ~* }# E+ }& D6 J) ~8 R/ m/ X
    ! D- `0 i3 ?1 a* q% @+ f; G1 q1 p0 z
    factorial(1) = 1 * 1 = 1' m6 f- b1 @2 _# _/ @8 r" w
    factorial(2) = 2 * 1 = 21 S5 [9 H  {; O& y( ^) x* h
    factorial(3) = 3 * 2 = 6
    & y; r. x, X2 J4 Rfactorial(4) = 4 * 6 = 240 A6 n, @# I# B. U( u: a* e
    factorial(5) = 5 * 24 = 120
    , X9 n( [+ L6 a5 m" v( x
    4 V+ F0 Q( R# o8 wAdvantages of Recursion% j, ?, V" [3 N# c

    5 c: z9 k: k/ ~# x) E7 }7 Z) V    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).
    2 S9 [- T% T/ b! L
    & r, f$ b! ^( l. k2 W+ A" M    Readability: Recursive code can be more readable and concise compared to iterative solutions.3 s6 z! X/ d& E
    9 S8 x9 s* |% a, z# j4 m
    Disadvantages of Recursion
    - P, W1 {$ N0 z! _' L( K5 [* P, C3 X( r; Q
        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 }* \5 J8 \% g8 |5 I  {# j
    8 ]1 ?( X' P6 q2 L1 `: ^/ a
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    1 t/ k& P5 M4 K1 M$ K
    5 m# Y" j: X# Y# _5 ~4 xWhen to Use Recursion2 |3 C! O  C) e. X$ {$ G! e
    1 d2 r6 n! \; i
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 B( }& T/ G4 c; X/ o7 v' f: I9 S. T4 V$ u
        Problems with a clear base case and recursive case., A: Z: j3 r4 a) V8 k

    * g& h# {) X% r3 I" }7 M- h  U2 xExample: Fibonacci Sequence
    1 [3 }) I3 K( f/ J
    4 V4 K$ w. b/ ~4 xThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# r/ c) P2 }( F1 U$ g! \& X
    2 c8 a3 [. j: z- c
        Base case: fib(0) = 0, fib(1) = 1- s, i7 A: X* m7 k9 f

    & c2 z  e+ a7 L# |* M+ S    Recursive case: fib(n) = fib(n-1) + fib(n-2)& K' a3 N: ?( H: B* H

    4 t$ O1 n9 o; h2 q- B9 Y& Gpython
    ' r- ^* b- F" `) F( P$ X2 R- l4 W, J. Z' t4 g! j  B  v  N

    $ {9 O' p  Y. ~( B# \5 Sdef fibonacci(n):' P. H6 P" G8 j) v* a- u
        # Base cases
    ! f5 b. E7 C1 m) X. m: e    if n == 0:2 b' d  e* O. L2 p- F0 D" D; I$ G0 x" u
            return 05 i! q; W/ }- z+ _% l3 t. z
        elif n == 1:
    # h6 Q" C7 Q8 {; y4 d" u) e        return 1
    0 y; }" w0 k$ |& c7 \    # Recursive case: _+ ]- U5 y1 O# k! F* |
        else:  @4 `9 [9 V  X3 |3 M" B4 t9 ]6 c- C# I
            return fibonacci(n - 1) + fibonacci(n - 2). `8 Y+ v( H# @' t% R  T5 b
    * J& }5 L' m6 V4 `! u; i" k
    # Example usage& s) |) r) Z( ?: F  \
    print(fibonacci(6))  # Output: 8& w' }7 M+ ?! Y5 P5 r, v3 a
    / U, }: C  Q8 X% W7 g9 q0 E; q1 M! P
    Tail Recursion
    6 l2 s  z& w( P7 v2 L  [" V9 x$ i/ ]6 ~0 `$ R) Z
    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).
    - u) K. O2 Y6 o/ ^% W- a* L8 ^2 `
    4 l7 g8 Z1 R! ^8 l* g/ 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, 2025-12-8 17:14 , Processed in 0.030857 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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