设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    4 b% R/ T; D8 y* {2 F4 |
    ( s/ U, Z, f9 \解释的不错
    ' E+ l3 l' x9 W- v, b6 f
    + @. K$ K; [% H' M递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  Y+ B6 X: g( _; z8 W
    8 P; g! K5 T6 M9 N  O8 Y
    关键要素/ Y2 a5 i9 a/ E9 p
    1. **基线条件(Base Case)**) x0 O: w; W* O7 F* h1 D
       - 递归终止的条件,防止无限循环
    : X4 u- @6 {8 c   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1) W8 H" ?4 v" k$ g

    6 p0 o# ^4 e) H! {  \' M9 @2. **递归条件(Recursive Case)**9 C4 A! m9 |. K
       - 将原问题分解为更小的子问题7 f+ p* |' v0 f( }( T: I. f
       - 例如:n! = n × (n-1)!
    % n/ a, f# N+ Y$ S, f  B( A, l
    ' h7 v2 e7 @( s 经典示例:计算阶乘
    8 M/ i7 ]. {1 V: n" v* L, Ipython
    + I& u' s0 U) h- {" }def factorial(n):+ `2 g, d8 w" G( |, ^: @4 m
        if n == 0:        # 基线条件
    8 Q- l  V' o: w' `        return 1
    4 t0 j2 [; n4 Y5 s/ r  W. J2 o) L    else:             # 递归条件2 v7 u) L0 H5 z5 B; }- t" O( v
            return n * factorial(n-1)
    " r) w' z3 N  _# Q1 N3 A执行过程(以计算 3! 为例):& H. }0 i# `. l0 P
    factorial(3)
    2 M$ A3 F9 y* i. u5 F# M3 * factorial(2)/ D; @" d* L# [1 B
    3 * (2 * factorial(1))/ b: z( Q0 m  I/ A4 d4 W
    3 * (2 * (1 * factorial(0)))  `/ Y6 S0 e% g7 V
    3 * (2 * (1 * 1)) = 6$ h! S& V. ^& W2 M; T

    4 J6 |- B3 D- [' q' P) H 递归思维要点
    0 N  X1 g4 c# f. ^9 @1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 H4 v, \6 N6 {; m8 X2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ s- g. y3 I0 m4 ^# D& k4 x- g
    3. **递推过程**:不断向下分解问题(递)6 S# G5 b5 Y) H: \. M" |
    4. **回溯过程**:组合子问题结果返回(归)
    4 V% O' Z# e" s3 z* J# U) I$ A6 |) |" y" J2 M( i
    注意事项
    % l0 e) j) a* L& ]# I, x( i必须要有终止条件6 X. `: i5 k$ e0 L; z$ |1 |/ i
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    " X# [4 n# d$ X; [% s- g- ~某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ! ?% \# J: |, C尾递归优化可以提升效率(但Python不支持)/ Q6 S. E2 H; t( R; C, _) ]  P+ ?) a

    $ M+ |+ d- h4 ?' o 递归 vs 迭代& r! \  a/ V. f7 U+ m5 P% b% Z
    |          | 递归                          | 迭代               |
    5 K" B, X5 x( I7 Q$ t  r|----------|-----------------------------|------------------|
    - b$ E4 x% ~; I7 x% C, }, l  T! V| 实现方式    | 函数自调用                        | 循环结构            |
    + W. j- {- Y9 V1 K| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 ?3 Y0 m: e: Q) C3 x| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |' D6 x- O$ L+ H; N
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |; k* _" Q0 O5 p0 s( f5 L

    8 Z6 V$ b) t1 |- i* e 经典递归应用场景
    # t5 G' `4 i! c( p( z1. 文件系统遍历(目录树结构)
    / g! R7 _2 r! s2 h& e- y2. 快速排序/归并排序算法
    " {4 L- p, M% C  ]/ `1 j2 |9 [3. 汉诺塔问题3 n" v" Z: Z0 J
    4. 二叉树遍历(前序/中序/后序)8 l# ~1 k2 F6 z* ?0 A( @
    5. 生成所有可能的组合(回溯算法)
    5 y! F" T- X7 q& E. f! m0 B, O& ~7 A" `3 n  L& a0 M, J, {! h8 x
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    2 小时前
  • 签到天数: 3151 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ; B+ |8 i( ]; s- |" n! I我推理机的核心算法应该是二叉树遍历的变种。
    ( O0 z  @- O1 C( M: }另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ; \, I1 \9 y) }% k4 \) u; dKey Idea of Recursion
    + r' ]" ?# T! `- r% u* d1 b( N: H; K6 H& \4 e- ^
    A recursive function solves a problem by:1 k9 U, w  e$ m+ g
    # z! f0 {" S: d& r4 }
        Breaking the problem into smaller instances of the same problem.
    . d" d+ R* u+ E6 b. c! F7 ^: `. \0 \! g1 Y- n' c6 E
        Solving the smallest instance directly (base case).4 j! T" d8 x2 k. V# P

    # B+ G! }; K. W0 m: P) k8 L    Combining the results of smaller instances to solve the larger problem.& o& ]/ m' i, s+ h
    ! J1 a8 R6 A0 p
    Components of a Recursive Function# s" z% e: D: u$ ~3 d0 R# `; p9 w

    $ v1 T8 L$ X3 C% T    Base Case:
    6 |& N+ W' \  C0 E, P
    / `. q- H1 C3 @        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # y# d7 D- K0 V" {
    " u- l$ S5 U" w* Q        It acts as the stopping condition to prevent infinite recursion.
    4 ^5 D# |  d5 Y$ F3 Y
    ! }: ^$ ~" N8 Y" z+ U        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # S- J5 ]6 n1 P* p7 X  A( [
    , A) V8 t" Z0 k6 z9 s5 J0 p- X    Recursive Case:
    * f& S4 S5 c9 s: k; u! c% k5 W, w/ F% G. \8 \0 F
            This is where the function calls itself with a smaller or simpler version of the problem.% Z* C* r4 x$ b7 m
    $ m, K" \3 o, L+ Y8 [6 a
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).( A( G. E3 w# Y) ^6 w7 ?
    1 i' l$ G# x1 }1 n' P
    Example: Factorial Calculation1 a* d6 n0 [& Z0 z0 b& i( c  k+ r
    7 L1 I0 y9 y6 b7 y  ^
    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:
    ; l% P! k6 b) m, ?9 _0 B; t3 x3 o* v8 u: J6 C% H# n0 Q# c
        Base case: 0! = 1
    , ]( y: ]; t7 r! R% _
    , i2 U( F+ ?# |    Recursive case: n! = n * (n-1)!% U4 x% u; i, }# @! i* k
    # [  z, R2 ]( I% D3 h% v7 d5 M- Y
    Here’s how it looks in code (Python):; Z1 X/ K  f6 }$ j+ @
    python
    9 g! m% j7 K8 g- h6 x$ ]7 L
    5 A8 ^  X5 z4 H0 Q. Y0 K7 k' K, y7 x3 ?8 v
    def factorial(n):/ G7 Z% o1 w3 }) b. |% w  Z1 e% S
        # Base case) H& ?0 a; ^; E. K4 l
        if n == 0:+ e' p" P* _- F; o4 n" |1 p/ ?$ I
            return 1
    + S% B, K: J" [    # Recursive case7 e+ L- b0 E+ e3 w% Y
        else:
    " l+ c( K9 i& `0 ]( e& L  Q        return n * factorial(n - 1)
    % Y/ b$ l& P" u4 C1 k# f! t
    " m! j1 @' \9 V! x) k9 @# Example usage
    8 V3 l. e; L* C$ L0 i* Lprint(factorial(5))  # Output: 120( \( h2 t9 @5 |) u0 r

    ; P# x9 N8 W& c( _" G9 bHow Recursion Works5 C1 q  j7 s/ f8 D5 J, m

    - u- y" B7 n) L% H. s5 }    The function keeps calling itself with smaller inputs until it reaches the base case.
    + V4 K& E) {5 D. y( g, [
    7 Y6 O- u+ ^0 r4 N5 l    Once the base case is reached, the function starts returning values back up the call stack.: E# Q2 ~4 u5 k/ S+ u7 H3 F

    8 t0 o$ s" y6 C    These returned values are combined to produce the final result./ U& e& u% G  f6 ]* r9 h
    ( M: E% C: Q+ z& I6 J1 e* x
    For factorial(5):4 f' `/ F6 y* V3 j4 E  U

    * Q  f6 j: Y9 R" Z6 v# L
    8 @( j. c  W' T2 W% ?7 f) Sfactorial(5) = 5 * factorial(4)
    2 k% h/ s0 S' W" e+ D1 }factorial(4) = 4 * factorial(3)1 h/ g. J( @3 H$ U$ X* k
    factorial(3) = 3 * factorial(2), ^4 W; L( K  V$ V" g1 g2 |
    factorial(2) = 2 * factorial(1)
    . H0 ]. O) ~  zfactorial(1) = 1 * factorial(0)
    7 r" K' f, r) X  r. i& d' @; u( ~6 t( r5 zfactorial(0) = 1  # Base case/ h. k" W, v) b' p7 v. I

    5 \) S+ R# s2 E4 E& H" e/ [5 uThen, the results are combined:
    9 o- Q, W7 [5 y7 E8 R, m
    & R0 B  ^; ?+ k
    0 n7 k* D1 M% X; k3 b1 Sfactorial(1) = 1 * 1 = 16 f; D' }% c% M: v+ J( |* J
    factorial(2) = 2 * 1 = 2
      h2 e0 c/ [7 J& @) x  qfactorial(3) = 3 * 2 = 6* D6 l, m& M% f
    factorial(4) = 4 * 6 = 24
    ) W  L( s) E1 `" Vfactorial(5) = 5 * 24 = 120' _+ j" }, }# ^: y% E4 G
    : J; e' O8 f- E1 D
    Advantages of Recursion
    , ~4 C6 P' K+ |& {6 T: I7 H  b
    4 K  ^4 N8 K- u3 l    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 h+ ?- m( \' y
    # W+ f: p( Z3 N, S    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    1 a  A( c/ J  Y, J! f. O
    3 }) W5 B; l+ n2 t, |! ^7 BDisadvantages of Recursion4 H4 o4 {5 {( e7 _* J3 j
    7 ?( ^2 ~, w6 B1 R& p
        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.0 |" x, i6 K7 X' r& S. X0 G* _

    ( F" n3 P: o: I# R( n    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    5 f( N& V( b% a/ J4 v6 o- r! q
    ( Z* {1 n  D' rWhen to Use Recursion8 J# Z: r( }6 A2 T, i# [  X
    ! D7 p; r" p* C2 h) r# u: r
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ' C* t& J/ P0 p5 t' |3 m, g3 G, X) Z. C# R1 [' `) C4 c+ B9 w$ w* J
        Problems with a clear base case and recursive case.: y+ Z% M2 s: N1 s$ v

    5 L/ ]7 f2 e: R+ K) l1 ?Example: Fibonacci Sequence6 @- P, m% o" R: F& r, f; K" E
    " r; x& h$ {) r
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& s; [; S0 M) F# {) k) X
    5 _& n2 `; |) L1 ^8 ^# q  F
        Base case: fib(0) = 0, fib(1) = 1
    / J7 E4 [6 R  l: W2 ~' F! {2 x. i+ F* u" j; F  F- S# e5 p" }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)* K; z" L' _4 x' Q. w2 O

    + w% E, U% q2 S  O) Y2 rpython
    % K( ?$ A) [$ R% }3 W; |6 Y/ G3 P& t0 _) H" @; Z9 R

    6 V3 |$ C) A6 m% vdef fibonacci(n):
    & ^( y0 a$ P3 F1 w) a    # Base cases+ h& J, ^1 {2 @6 f  p
        if n == 0:6 y( _8 T9 D/ p( @4 _1 V( @/ d
            return 0( D3 X% q* y9 ]) b2 ~
        elif n == 1:: W: Q! h8 M2 I: \4 Q$ _
            return 1
    5 [# @: z8 w+ Q, `; v    # Recursive case
    ! S1 _" D( {: `* Q8 p+ }& V    else:
    ; [# h8 C. J% K5 b8 d        return fibonacci(n - 1) + fibonacci(n - 2), D/ i; a; K. I
    ' G0 N- D3 U. p6 c5 ^
    # Example usage
    8 ^1 k1 n; g4 @1 n, h8 S; t6 z1 M" f+ Mprint(fibonacci(6))  # Output: 8( o) s' E0 V" B8 T

    7 t. \& c% S% W  yTail Recursion8 J) e* g5 n2 H$ d* ]
    & ~1 G9 I) P* I1 f4 n8 K
    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).2 I. T! h) X% b- L1 t
    % P; K7 Y& @, _. L* h$ i- C
    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-1-22 13:01 , Processed in 0.064421 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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