设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , w8 R- V. U$ E; V' [2 D" b2 R

    ; {1 ]4 K+ D+ l7 |5 H  C解释的不错
    ; Y# A1 c# S( C* P) V6 d' V9 {, |4 u! l! ~# F/ {1 _
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  i8 Y% o- U" D0 f. e

    ! Y& ?; b1 B( s3 z" s 关键要素  c! R7 E" I# r6 C* a* q# B0 t
    1. **基线条件(Base Case)**0 A  A# X4 `; L; o# j; ]4 h7 W2 L- g, C
       - 递归终止的条件,防止无限循环
    - f/ t8 `4 R; {- T( r+ o8 A& P   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    , C4 n. Z  ]0 t. b9 ~
    , ~9 x  A/ i6 ^: j  U8 M2. **递归条件(Recursive Case)**) O# o; j% k; n
       - 将原问题分解为更小的子问题5 e' k8 B0 g- [' I
       - 例如:n! = n × (n-1)!7 r8 E+ O7 k' d- c3 C& Z6 W
    1 S$ Z+ J: p3 M* y2 g+ h) W
    经典示例:计算阶乘
    ! W* e4 }) @4 w2 C8 F# fpython
    4 K& e( _/ E- rdef factorial(n):3 \4 l. [$ p# U2 }% Y) u2 C( u
        if n == 0:        # 基线条件
    + _2 B5 u7 a: c4 _9 E1 t8 `        return 1
      @, ], h' |# t* N' H    else:             # 递归条件8 e+ v/ t0 r6 L; Y
            return n * factorial(n-1)5 w" B+ e" s7 q8 b  S1 i3 Z7 q
    执行过程(以计算 3! 为例):7 @& O* D% Y/ y
    factorial(3)) i  |2 p& O0 d' B' }
    3 * factorial(2). Y, }, E9 F/ L, t
    3 * (2 * factorial(1))* h: U" r9 q0 v  X' o
    3 * (2 * (1 * factorial(0)))8 l- \" Y4 B9 z
    3 * (2 * (1 * 1)) = 6' d; t  B' L1 i2 S
    5 e7 W& R0 X0 R9 Q8 b' w
    递归思维要点$ D* R- ^4 N+ U) W8 E
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* O0 \# i1 U4 H( {; N( J
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ( S% X1 `+ W: ?$ h. j3. **递推过程**:不断向下分解问题(递)
    ' }# W6 K# m4 }: x9 Z1 }4. **回溯过程**:组合子问题结果返回(归)* L! V+ m* l- @5 V5 I
    # E5 E5 s3 H; n$ ~4 w9 u) Z" I
    注意事项
    + D# C9 P, ~, G5 Y0 A% E必须要有终止条件* u& ^1 o) g" ^. u# E; G
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层), C( I$ U/ G0 A& M+ A7 j
    某些问题用递归更直观(如树遍历),但效率可能不如迭代  _. P. f: g8 `, c# |& _
    尾递归优化可以提升效率(但Python不支持)
    & W" U+ x  W+ E4 ]- @
    . P6 J+ a9 k% E5 k7 [ 递归 vs 迭代7 y2 f# R9 g" m
    |          | 递归                          | 迭代               |
    * D) E- B4 M. H! G6 v, ?|----------|-----------------------------|------------------|
    % N3 D& R) \% L% p8 q' x| 实现方式    | 函数自调用                        | 循环结构            |9 |  @  E' ~. ]( R
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |* M$ J8 s6 y% H
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |) P. n$ x7 C6 J
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |3 Q; J# A; |) @  D9 W# |/ W

    - x) F" @( G8 Z2 M  t2 [, G  J; L 经典递归应用场景
    7 t' B6 h" ^- M- s6 x" L0 d1. 文件系统遍历(目录树结构)
    * D5 H7 Z& ]. H/ f2. 快速排序/归并排序算法
    6 ^3 P; ~+ q9 {1 C. M) H5 z3. 汉诺塔问题
    * t7 e1 ^; w+ V" w0 H4. 二叉树遍历(前序/中序/后序)
    ! d7 o& y. `$ a# @+ i! U5. 生成所有可能的组合(回溯算法)6 v  f2 H; E: f
    , @0 s6 q7 d  d0 j" D
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    5 小时前
  • 签到天数: 3152 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( b# g, n1 d1 p5 v; A
    我推理机的核心算法应该是二叉树遍历的变种。
    , r- g9 t4 C( r/ B! {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    , G9 k3 q; s* Y$ d. K) S* x3 N  VKey Idea of Recursion6 i: M* E. y0 f0 l" s" l- {; g" T
    2 U& q' B9 J9 ^
    A recursive function solves a problem by:
    / V; S. K" r! r; U+ i% N$ B
    " r/ \  \1 c% \) T) `    Breaking the problem into smaller instances of the same problem.# f( Q& D) t; I% b! m
    1 g( r; ?3 l0 o0 r4 I
        Solving the smallest instance directly (base case).
    9 ^% U  h6 r5 \/ E* O2 z; m3 [
    7 L0 @7 ~; Y3 _' @    Combining the results of smaller instances to solve the larger problem.
    # u4 O% }, N, W! ~+ O8 q, M$ j3 b2 X  h8 ?7 Z. p+ `  z" q: e0 b
    Components of a Recursive Function
    9 Y6 @" y6 x* n8 D' o
    1 s4 i  n, i( n: }! f& `    Base Case:
    , S' [; g# P) ]; t  m+ |# ?! b6 g' P: C, e  S
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ' d. D9 _7 a; O& _% @5 E; J( }9 t5 a
            It acts as the stopping condition to prevent infinite recursion.  Q& o1 ~. ~; Z/ @
    4 r9 u. B$ x3 S
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    3 E* R6 ~  B( y/ x# w6 Y7 f4 Q. |- b$ \3 [! ^' u/ O
        Recursive Case:
    ( ~# d* ^+ T1 o% k9 [- U6 m. L' q3 z! u9 F/ ]/ e8 a2 a" N( m
            This is where the function calls itself with a smaller or simpler version of the problem.
    ; F6 z$ i9 h% R. Z$ [! F7 `) I9 q  ]6 d) S9 S/ U7 i, \
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 r# e; k( a5 r* J2 h

    % d' D7 W. z$ [" G4 d3 z. bExample: Factorial Calculation
    / J+ |& N2 X! J5 ^0 Y8 d# r( _2 y- D. p& w$ J$ K: S7 c+ T7 h* I3 H
    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:2 {& @) ]2 g. U& O- H# ^# b

    ) @/ R; e* M9 A, g% M7 I8 g7 I    Base case: 0! = 1
    8 U& p4 ^2 \$ T: Z# d6 p
    ; M& W' c/ c" D8 W5 u* ~; J    Recursive case: n! = n * (n-1)!
    4 U3 R8 J$ [0 r/ O  l0 p
    , W: L: Z% C7 o5 q+ ~) H6 ~Here’s how it looks in code (Python):
    , ]7 K3 b8 |$ o) g2 h$ W! Dpython
    ! u0 V: P" Z" p2 _& }) y3 }7 }4 M5 O3 T( z$ F3 \7 V  H4 W
    ! H! _/ a9 a( X4 \' r
    def factorial(n):% Q8 Q& L# B* P( F; D: m
        # Base case
    * p; Y4 W  C) d* E    if n == 0:8 F* \- O* z$ {3 ~
            return 1
    2 V2 O2 ^9 Z$ ?( I  T+ B' |5 i    # Recursive case$ z8 U/ i+ y7 O5 e; j
        else:
    5 w+ a2 Y8 C0 j        return n * factorial(n - 1)
    , a5 P/ c, P2 g1 s) _5 f6 P
    , p. r. {! ~: }# D' }# Example usage
    6 k" w4 n) N1 T; n, \( Qprint(factorial(5))  # Output: 120
    % Z1 ]% ?$ X3 |# w) V
    # S! W4 D/ |# o  Z) d6 r- sHow Recursion Works
    ' e) H2 D8 X9 q1 U- P: s( o) ?+ D  u- r
    - F/ \- r! P5 v( P) ?# W    The function keeps calling itself with smaller inputs until it reaches the base case.& ~3 U% [6 f- M# U/ y- s* \' B5 D9 l
    % T2 C* m- B! e1 B5 `
        Once the base case is reached, the function starts returning values back up the call stack.
    $ x7 F1 ^; H0 \* F1 u
    , H7 _. u% a: q7 |& p' E' c    These returned values are combined to produce the final result.* d, |  O; V( J

    4 C: W2 E* q; F" O  _" R2 E" aFor factorial(5):
    % l: N0 G. N* P9 {% O* [2 ?
      l$ s0 n  W4 [1 ^1 f5 s8 A) z3 u- {% ]8 p
    factorial(5) = 5 * factorial(4)- ]% W2 c2 ^: C. D+ i. d+ O
    factorial(4) = 4 * factorial(3)+ t7 F3 B; o  H4 d& O
    factorial(3) = 3 * factorial(2)% w+ |  R1 j& L/ J/ y
    factorial(2) = 2 * factorial(1)
    5 f. |, S+ u+ H  V; n/ \$ s2 zfactorial(1) = 1 * factorial(0)
    $ x& }" g) a: g) I& yfactorial(0) = 1  # Base case
    - p- G; H* `- ]6 N8 f9 E3 }
    . k! A, y7 [% U1 eThen, the results are combined:
    : T8 \' Z6 i# L2 k3 V# ?, q1 `) n8 K7 e2 r
    9 G% C+ r4 C! S$ a2 c
    factorial(1) = 1 * 1 = 17 h8 ?# M6 l7 e  V5 W) u6 ]/ M
    factorial(2) = 2 * 1 = 22 \1 @" m  \9 ^/ @9 C, U
    factorial(3) = 3 * 2 = 62 M1 ^, ]3 N" F: {: w
    factorial(4) = 4 * 6 = 24
    ' Z8 j6 a8 z$ h6 `factorial(5) = 5 * 24 = 120
    & ]* A$ h4 L* Y! h& d3 s
    / \2 T6 K& X( i2 _# @4 `  v2 `& @Advantages of Recursion3 a% X# I$ W( q- {! F4 v9 f

    2 F# H& A% j0 `) ?3 ?0 g. k    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).
    3 Z9 ?: ^1 |. V4 t1 h
    ' v& y" j- }3 L3 h, N    Readability: Recursive code can be more readable and concise compared to iterative solutions./ n  i7 I9 T% H

    : T" l4 Q; A) l3 u5 I: T' SDisadvantages of Recursion, \: Q# E/ @3 t& U# P( l; U
    : D2 {0 }; j* ]
        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.
    ; O/ l- N. `( K0 `. w( V
    % L5 H% L, h& O( q; Z1 `& N    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ) y( u" B* {7 s0 Q# t- ?  ^! Q, ]2 e. j9 U
    When to Use Recursion
    : R5 [. F: H: h
    ( ?6 m& n" j* k7 g. t    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    3 M7 J2 ^0 S( `2 b2 |. X  _8 z/ D4 p2 U# c8 w. P
        Problems with a clear base case and recursive case.+ d# T- ^: m8 k4 O
    ! V! C* F7 Y3 O( ^, o6 P
    Example: Fibonacci Sequence
    # @* P4 |2 G5 |' s. }& k, v+ V) z# p, s: c, B5 _& [8 Y3 P0 c
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 y% {: Y7 j" L9 t0 n3 J* v6 E# s! p0 H/ r: X& ]" C
        Base case: fib(0) = 0, fib(1) = 1
    0 _9 k+ y. P. V: S
    7 C  `5 j: M* a' Q; Z* X    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    / B( @2 M+ o7 @: j5 j+ D! i$ F
    / k! [8 W* v/ q  Ppython* w+ E# u4 s2 b  }+ N
    " e- B9 [% z. g& B. ~% [2 ~

    $ |& r9 V, l. I7 C1 l( r3 ~% @def fibonacci(n):1 P5 c: \8 K0 N+ a" g
        # Base cases
    ' b& f2 V9 u' H    if n == 0:$ S* R5 n- f; P" U
            return 0
    / I- B! E, f, F7 h5 `$ t3 q    elif n == 1:  j* ^; E! X7 B/ {( X* t
            return 15 X8 O4 ]) V# M1 X
        # Recursive case
    9 i0 \" [4 W0 g    else:
    % I/ S% |3 `! z2 H* B  N6 H        return fibonacci(n - 1) + fibonacci(n - 2)
    6 q* C' `9 X6 P/ @- X" {3 p: D; h1 P3 c4 s+ N9 O0 w- D4 t
    # Example usage
    + w3 e& X1 K$ t8 J( X0 zprint(fibonacci(6))  # Output: 8
    . i2 ]: ]/ g# S& L- m/ W/ x
      j1 v4 ]) O4 ?8 \: K" XTail Recursion
    ( z) ~5 M  \8 L, H1 o, {% u' ^6 M& u
    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).: f4 o" S) F. o2 m

    $ G8 z+ @6 A5 `+ B4 tIn 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-23 12:29 , Processed in 0.056755 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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