设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + v- I5 x( ~1 u$ Y$ W3 n
    ! r) w" {% Q7 W4 r0 k
    解释的不错
    0 ?2 u+ G: S0 ^0 P) Q* |$ L) K- i9 U( d3 b# y' S( K
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ Y. k# ^8 U* R5 u! t' D+ W+ P8 c. T9 n0 e4 g& O
    关键要素
    & L; d+ E) `7 x. W1 N4 H1. **基线条件(Base Case)**) n( X& w( u0 t
       - 递归终止的条件,防止无限循环! e. c, o( V& {* L* X4 V9 X
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    0 @- S- C4 J* j6 L. y; ]; [7 S) ?& Y/ A
    2. **递归条件(Recursive Case)**$ V8 @. Y% i$ Z# _5 A
       - 将原问题分解为更小的子问题! Q+ C8 ]. B7 \  I) C4 s
       - 例如:n! = n × (n-1)!. x% j8 Z- M' X4 U
    * h# e( k7 z! R4 f. j5 }/ F7 u4 h# B
    经典示例:计算阶乘
    1 H$ H* @/ W6 Gpython
    6 q5 _2 y" C6 w& K, A# ]' }def factorial(n):
    0 ?6 J: k/ ?; R) I$ X* ?9 v    if n == 0:        # 基线条件& M6 G! r  J. [7 o+ A4 o$ i- q
            return 1
      M# D  l- _" O8 _7 t1 n    else:             # 递归条件$ e+ l% k  k- N. ~' u1 j8 q: M4 g
            return n * factorial(n-1)3 T0 Y+ g  p! a: s5 ^# q
    执行过程(以计算 3! 为例):
    % B# D% s% E3 d1 y) P5 [( jfactorial(3)% _4 y* d1 X3 ]! t/ C- [% _( Z9 R
    3 * factorial(2)
    ' L4 \# V0 M  _$ U. J3 * (2 * factorial(1))1 v: U9 \( b1 c% J- C" @( p
    3 * (2 * (1 * factorial(0))): s, I, l* J3 ^
    3 * (2 * (1 * 1)) = 6
    ( d7 n% {! j, E) v( u# @5 t5 A8 T
    9 K2 W$ H0 i' f! ~3 r# A 递归思维要点
    4 u3 w7 v5 w, z2 W4 C& b1. **信任递归**:假设子问题已经解决,专注当前层逻辑* U, _' l0 g* G  P
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    , }( d+ n& M7 U( L4 ?- f3. **递推过程**:不断向下分解问题(递)* V" U! l8 v& i, Y/ m3 Q/ c
    4. **回溯过程**:组合子问题结果返回(归)
    $ m7 k  z3 _; c. k' z' f. ^% g* J, K8 v: G2 A
    注意事项
    # x) n# l% q+ e! _必须要有终止条件
      w' @- C( d6 {8 o递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! S9 v2 n8 d: \' k某些问题用递归更直观(如树遍历),但效率可能不如迭代
    7 }& {/ ~; }# w' h2 q尾递归优化可以提升效率(但Python不支持)# v7 h  q* X! F' b! g7 P- H

    # r2 N2 n2 ~/ y5 ]) c& T& q 递归 vs 迭代  p9 U) l6 {3 z  A) o" Q$ T& T
    |          | 递归                          | 迭代               |
    ; i2 C1 o( V, S3 ?& `|----------|-----------------------------|------------------|/ L1 w4 Y, @' [: P: z5 _4 b
    | 实现方式    | 函数自调用                        | 循环结构            |! t( O- I1 Z8 X% z! I
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) K- _) N: D" O1 S3 K
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      p2 U0 f- Y) N+ Z+ x. k  u9 C- w| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 o5 @1 c; S/ f: I9 D, L
    ' d2 `/ \" ?' }% t 经典递归应用场景4 O7 P& B! u6 i' h, S5 C& z# Q# Z
    1. 文件系统遍历(目录树结构)& q9 j/ \* M# ^& q# o3 `  ?
    2. 快速排序/归并排序算法
    ( y; B. ~8 h3 {; U. h+ O3. 汉诺塔问题
    ! w2 h8 C. Z+ K) E& J* X; ]7 r4. 二叉树遍历(前序/中序/后序)# A& t2 l9 Q2 q
    5. 生成所有可能的组合(回溯算法)
    3 O& S  i: c* X' g$ W# C8 N  o0 \+ D; b  E
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 07:45
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    5 q5 U& T6 ^! E9 _) I9 c我推理机的核心算法应该是二叉树遍历的变种。' n' ^9 w/ Q; N! I
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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" ^- u, X9 ]; N" a) g+ r
    Key Idea of Recursion! E* Z* v; ~) c/ Q

    ; h5 w; z6 b4 \% ]$ lA recursive function solves a problem by:
    # X3 e+ {- c& j; z! T5 C
      B0 O4 y2 l: y0 B1 ^( m! _% b    Breaking the problem into smaller instances of the same problem.
    - v+ z1 O4 r* c% I9 `. X0 E4 O# o+ p" N2 L% d" u, `! f
        Solving the smallest instance directly (base case).
    ) w6 u% W# A. J" \: j( B. A! Z
    + p: D) o# w; ~2 R9 \    Combining the results of smaller instances to solve the larger problem.# o: V  c( Y8 [; T: x7 v

    3 |) o1 u, z- I6 R1 L( d; ^Components of a Recursive Function1 }4 j% u% W8 V; x/ K- a4 r

    : @2 o) @  R: g2 S1 `& C/ j    Base Case:3 O3 f, h' K/ I7 N, D

    ( |3 o. z; y5 n( i" F3 A: j  @- d/ z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.$ [* p& Y: V& i6 z5 B/ @' q
    3 Q8 g* E0 _9 B: C$ ^0 V0 W
            It acts as the stopping condition to prevent infinite recursion.& H8 e- }. d$ O* t6 @" v' a
      Y. h3 G4 |; E/ H' `6 Z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 l# A5 H, N0 T$ l; f8 i& T$ Z% A

    ' k  Z$ E  q- V# I/ ~    Recursive Case:
    1 {& I  P$ x# ^. O$ S: G. U; H+ O# h+ D: m
            This is where the function calls itself with a smaller or simpler version of the problem.7 `. y; c  t: y' I& H7 k
    6 J3 K# \% y2 V' P# f" J9 W
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 ]( J: V. O0 f4 F  n% ~9 g9 U" ]7 h
    Example: Factorial Calculation. u5 P" |5 w5 d. v# x* |

    " n/ Q, @& [7 QThe 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 c; X( D6 o  d+ i9 N. B; k/ p2 b) e5 X- x, f4 A
        Base case: 0! = 1
    3 q* g; A' M7 D& p7 }, R
    1 d) C4 t$ @. J( i0 U    Recursive case: n! = n * (n-1)!
    ! }/ [$ a9 ^7 y* k7 O1 ^
    % s1 M2 w9 X6 R% R( U# l3 h8 dHere’s how it looks in code (Python):# q* j$ C2 ?1 ^
    python, D2 y0 T/ o  c
    - Z1 v. Z# e% R- ~

    0 d* `3 Y/ a( q. o, q" W+ Hdef factorial(n):2 o7 M& ]; O# T( j$ m, Y
        # Base case
    / z% A5 s8 G3 P- F3 y+ C% Y    if n == 0:+ |6 O. D3 q; H0 r+ u- ?  l4 q
            return 17 A: g0 f, a( e) |
        # Recursive case2 X* Y7 z' L- q
        else:
    3 U/ S5 Z5 ~6 p4 H- {# P, a        return n * factorial(n - 1)
    % E" Z# V. n3 W0 ]! `, q1 X! V% x* a" T0 ^
    # Example usage
    ' n  H) n0 m' c( M% H# cprint(factorial(5))  # Output: 120
    & S% z( ^) @4 v9 N0 ?! A
    5 L; i) [: R3 S) f1 Q6 LHow Recursion Works6 U' H3 _( ^0 h0 n/ Y2 \

    % s" Q  I$ A- r    The function keeps calling itself with smaller inputs until it reaches the base case.+ K% f) o! g; V  t* U

    1 ~- x+ c3 x. F    Once the base case is reached, the function starts returning values back up the call stack.5 [" k& F9 M; `; c
    2 c* m$ U! z# X1 S* n
        These returned values are combined to produce the final result.: R. j9 f& `* a$ l) I* D! {

    - Z5 q7 D  Y' I' J% R2 sFor factorial(5):( c7 }, ?) r( H* Q$ r3 P

    ; ~6 p! B1 m& i+ T$ w! ~3 g& j8 q9 g# Z# f/ f7 `
    factorial(5) = 5 * factorial(4)/ X( N# J1 d3 P) ^
    factorial(4) = 4 * factorial(3)
    8 [, y# _1 A$ \! \  X) G8 O) ufactorial(3) = 3 * factorial(2)
    $ Z' b* b. d( ^: bfactorial(2) = 2 * factorial(1)* T$ y# [5 `3 B1 Z: {/ Q+ @
    factorial(1) = 1 * factorial(0)
    5 z! A( P: i; L" n+ E3 g/ |factorial(0) = 1  # Base case: E& {) \2 ^: v0 @& U: x
    - I4 ?2 t8 Y; d& j' d6 A
    Then, the results are combined:+ A# f2 U% _, j8 x' o5 O: G

    " _" n8 P/ ~' M
    4 L! Y0 e9 ?3 Kfactorial(1) = 1 * 1 = 1$ b1 w, h. b# I3 {) P! j
    factorial(2) = 2 * 1 = 2
    4 o7 i2 u, |& rfactorial(3) = 3 * 2 = 6# m8 Q" a/ {7 r/ i1 n" ^
    factorial(4) = 4 * 6 = 24
    . u" k$ U7 u0 f. Q- Vfactorial(5) = 5 * 24 = 120
    % Y8 D1 w; Z3 J: R7 S
    # A# U  `# V+ a6 b0 g" g, c1 A% ZAdvantages of Recursion; Y, ]' d' J2 X/ k$ ?4 r7 \$ v
    # y" d3 C' i$ x% E( r+ T
        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).
    + r% h( v& w! w5 W3 K$ E, k7 I# `/ h2 d5 }* _% t) r0 D
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / U* h, J. g6 b" O$ n& j: d" R! j8 X' F; ?& v
    Disadvantages of Recursion% ]% N7 x! {4 _6 A& V$ {7 \
    * i! R: A0 N4 `+ E
        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., W7 F  A' [: F7 m. j
    1 A1 ^0 b" c  i6 }% T% I7 }2 B; V
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' K( [) X+ B, ^/ b  {* ^/ o( w
    ! M. V4 \. F% y: i# @( q& ?When to Use Recursion
    1 ~" b; w3 V) r. ~' \( m9 w
    " R1 k, x1 i( d! l    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 V: R3 h) Z8 [( M- f

    7 E7 t) M# }5 u) Q    Problems with a clear base case and recursive case.
      G2 Y0 I2 G( O- o) _6 y
    4 c& c' C, r/ v) Y4 x: UExample: Fibonacci Sequence
      T5 V/ K0 k" `' Q' r  l) F; [0 \, I* \$ x( p
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# w! x0 x7 ?' l3 f" M* p
    * d# u7 E6 x2 J
        Base case: fib(0) = 0, fib(1) = 11 B* l  R# c' H# m6 [  R- r

    - p5 i3 o; G0 W6 E    Recursive case: fib(n) = fib(n-1) + fib(n-2)! C. a/ q9 K: ]% g
    7 g  R4 m' ?3 \, B3 A5 n6 L
    python7 S7 `. H0 S' _+ o9 C/ r

    + W  }: w4 H9 c* P6 b6 G3 L2 L2 @' U5 ?& f2 k9 \8 J
    def fibonacci(n):
    8 E" \0 f# Q% ^, ~" ~! j! d    # Base cases
    0 s3 H7 u9 _, B) t    if n == 0:
    " J3 r/ i& D4 L4 }) }3 E: n- D        return 09 E+ ^5 v5 o; _* p# Y  a! N5 `/ z
        elif n == 1:- I8 {  a! ?0 Q3 I/ X2 N
            return 1
    * B& |+ D; s& ]$ ^( X- v    # Recursive case
    % [( Q  ~2 A+ I  k4 P; y+ Q( F    else:# e7 V0 ~8 W% V' F2 ~( S
            return fibonacci(n - 1) + fibonacci(n - 2)
    - ^3 ~) O5 D! m# A+ l
    1 ]6 y& e, v8 t9 w5 e& c# Example usage4 `/ m/ Q1 k8 u( p0 E, U/ m0 G
    print(fibonacci(6))  # Output: 8
      J- _( `+ M2 M3 Z% A$ h( ~4 J2 A; o% t+ E; g
    Tail Recursion
    2 G/ h% e' n8 Q/ Q% m" p; s4 f1 j3 G
    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).+ Y' I/ B+ [" ?3 [

    ' c' }" x% i5 f4 sIn 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-4-10 14:34 , Processed in 0.062492 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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