设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 1 F6 L7 {5 j, u, O/ {

    # ]& q  W8 ?6 @2 E( A$ _& f解释的不错
    - s8 V9 C9 U. Y8 r1 C) d( M7 ]& v9 y/ h3 c. k1 |$ o2 I
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    % b3 t7 v2 S. z! y6 `' ]1 |5 ]8 H3 o1 n* r% O
    关键要素0 E. V9 R7 E% r* V1 n8 }; l7 L
    1. **基线条件(Base Case)**
    + f& g8 Q# V; r' \1 O& b   - 递归终止的条件,防止无限循环
    ( w5 x) y' ~# W8 C& V   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    / U8 o0 e/ j# \9 K
    8 u; o! L0 t% i, R2. **递归条件(Recursive Case)**
    2 _/ Y" J/ b. Q+ D   - 将原问题分解为更小的子问题
    ! n5 ]! w* D, z; R2 q   - 例如:n! = n × (n-1)!
    . K5 \9 x/ t" Q0 F& \
      |! H$ F, L0 l9 [/ f- q- A 经典示例:计算阶乘
    ! Q7 K0 T' q; D. }" X) ^' y( jpython
    # M4 s  w9 k) N4 F. x$ Ddef factorial(n):
    0 U- {+ [3 O6 r4 ^8 j) y    if n == 0:        # 基线条件
    - V- V: C5 i1 Y7 O6 W+ [- T" \        return 13 |4 }! _, ~/ w- H8 r: o
        else:             # 递归条件. S  E2 O; g  o. z$ b" y1 o
            return n * factorial(n-1)7 h1 S+ U# w' E. U7 e( U
    执行过程(以计算 3! 为例):
    1 {' F6 m# o. {8 v3 \factorial(3)/ C' `  ?1 S. }2 z
    3 * factorial(2)
    , P0 S/ ?- [8 ^  `' S+ M0 {1 O3 * (2 * factorial(1)), `2 S: t. s$ n' ?; J
    3 * (2 * (1 * factorial(0)))8 n0 z$ F( H) }; Z. C  Y2 y" G2 b
    3 * (2 * (1 * 1)) = 68 G2 f! B' z( b' ^9 \/ d* J$ z
    # B- z9 T/ _  s# S7 P) v# w/ v, L
    递归思维要点
    / K4 R4 Y. s5 G  k# m1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & ~9 [9 S- B0 \- d! I: G2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 q2 C; M4 \% U( z3. **递推过程**:不断向下分解问题(递)
    6 F% e2 M/ N5 a$ t0 S$ U4. **回溯过程**:组合子问题结果返回(归)1 X$ n2 U: K9 K4 Y6 N2 ~
    8 `. ~/ d/ G5 `* M& r
    注意事项
    4 X+ R- O$ Y+ }+ y: n( a) I必须要有终止条件0 E- n* s  A8 _8 r8 C- K9 G' k2 @
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( V8 _& N4 r: F) S, v$ f) B某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * r; ?' S) Z) _* D: N# h- _尾递归优化可以提升效率(但Python不支持), O4 X' w: ^) Q1 s- S0 q" w

    / n2 \/ Y6 t4 X1 V& s' j0 l 递归 vs 迭代4 m: [5 v. U* S
    |          | 递归                          | 迭代               |
    2 J: Q0 q1 [! L. [) l|----------|-----------------------------|------------------|
    + y, S$ o+ s9 j0 n  b' P' C| 实现方式    | 函数自调用                        | 循环结构            |
    7 U% C. E; Q. {( v* @6 I6 Q5 Q| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |7 w/ v! {  {" [( q9 I6 K
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 z1 ?' [$ _4 x: k: Q- Z( ?
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) D3 K! d: a/ L: J( M! o8 G

    / [+ k! L, R$ u1 m* |" F) Z7 } 经典递归应用场景
    * ^' M9 H' d: }8 S, ]1. 文件系统遍历(目录树结构)6 P& p# ]) z' u  z
    2. 快速排序/归并排序算法
    9 |5 l8 H. x6 i" {3. 汉诺塔问题% k' m( d7 p' |+ I" D
    4. 二叉树遍历(前序/中序/后序)
    : a% f2 ]8 {+ M3 q. q& K& t( P5. 生成所有可能的组合(回溯算法)
    9 O! `: v- U7 P! s: D' i
    1 W3 K4 ^6 u+ `+ Z试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    3 天前
  • 签到天数: 3210 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,9 l0 w% s- j8 n, f
    我推理机的核心算法应该是二叉树遍历的变种。! m) b8 x3 X7 Q# C
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 C. t- h+ F1 }! W8 p0 wKey Idea of Recursion
    ( Z( [6 h/ H  \! K
    % I4 a# n# y: @4 b1 k/ m2 B1 SA recursive function solves a problem by:
    : |+ @8 u6 W1 Y; F$ M( x
    9 Y  U2 f. r) U2 I* X4 M/ [% `    Breaking the problem into smaller instances of the same problem.5 Q4 ?& ^+ T1 H' X" F
    : I$ z4 Q2 J" i6 f, o0 v+ s; d
        Solving the smallest instance directly (base case).  W5 s/ Q' V# j
    ; t& `* n3 l2 F
        Combining the results of smaller instances to solve the larger problem.3 ^( ~' j9 \7 n; A- w9 w4 _( y
    4 }9 g/ _% T2 s; v" s7 R* z
    Components of a Recursive Function
    * Y5 H9 e8 g, z# u% x& r2 s0 t- T/ W: b  A6 Y7 C
        Base Case:' t) s$ {1 j8 T" f) i
    3 k: W! W* P( ?; r* F9 J
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / O/ s6 q1 s! Q0 \# v9 H8 S0 B) k) o" F" r# Y' o$ l
            It acts as the stopping condition to prevent infinite recursion.5 u  p+ u, e4 q+ L5 z" Z  n5 A
    5 p3 T+ E4 M7 E& @
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    & B. ~% ?1 X' z, Y9 k  {/ V
    % s$ E. K9 U6 Q6 |    Recursive Case:+ e# u0 d1 l# u; m: H
    3 L, Z2 {- j) y$ q9 E" N) a
            This is where the function calls itself with a smaller or simpler version of the problem.
    3 Q' D0 b) M2 Y) a) v) J1 U6 a
    - O& ?# o1 @) J1 q  d( W1 j        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).2 Z8 H3 ~9 t8 `

    4 \. `# I) {: `; C( X$ a" \! tExample: Factorial Calculation
    - }% Y: m8 c1 {" P; I$ O# v# v% B: d8 h2 }
    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:& _/ ]$ C) h: ^# ?) m5 }; F& A# ^

    # O4 @9 |) `9 {9 d  r$ `    Base case: 0! = 1
    0 t. ]' _# z8 c7 M
    + e3 ]& k4 _9 }* |" z    Recursive case: n! = n * (n-1)!
    . M/ k! ?" t6 C( r9 Y* i( G/ u' ?3 T: ?# a6 f+ P
    Here’s how it looks in code (Python):3 h9 e% L3 P( I. J8 p2 Z8 Y
    python) r. K9 T* v* Y0 U& v

    ) h/ L  v# I+ _# [$ V
    $ g0 {4 O4 ~- l6 Z, C- K6 l% ?def factorial(n):4 d" ]: h1 r& t7 H. ~# q
        # Base case
    1 o0 S5 L( I) ^: D  i2 ^    if n == 0:( [$ u# w7 x) n5 @# V! Q1 Y9 W
            return 1
    7 j  J0 d: ~& U( C2 v- @6 ~* @    # Recursive case
    5 i  {! _' z5 H/ P6 D7 g" v    else:
    " W8 L) d$ K3 j5 h/ F/ I" }        return n * factorial(n - 1)- Q! O$ c, B9 T

    + a1 [3 P; I. a( k" I2 w: B- F# Example usage
    6 V; ]9 j3 Y' a4 w4 X0 Xprint(factorial(5))  # Output: 120* U: F" i* k$ P8 C& y4 S! I
    . l$ Y! I9 |& O+ {* y9 s, i% F
    How Recursion Works9 |& L8 A, v/ t2 l' `1 n
    4 X9 P/ e: n/ Q, ?7 O
        The function keeps calling itself with smaller inputs until it reaches the base case.
    2 g: Y  H6 V& [5 k+ P; e& _5 a" [
    ; D# Y% u* n: Y: V) K    Once the base case is reached, the function starts returning values back up the call stack.
    $ \' b6 j* r7 \% Q
    7 v/ N; I! @2 O8 i$ @& D5 A' k    These returned values are combined to produce the final result., S. {+ A2 m$ n! `7 e  {7 D
    , O4 w  A' B6 D: E& m1 \- {2 [) D
    For factorial(5):
    % J+ u! d5 D2 Z* ^2 W$ r7 A5 A9 T
    - q2 b1 z% T& y! K7 L; S7 D
    factorial(5) = 5 * factorial(4)2 B% G! j. i: O
    factorial(4) = 4 * factorial(3)
    ) V$ \( R- [; B+ V8 lfactorial(3) = 3 * factorial(2)
    1 r) C6 n5 F3 f5 L7 d, {factorial(2) = 2 * factorial(1)
    & I$ }' y, Q0 U4 Vfactorial(1) = 1 * factorial(0)
    - w# v: L* r3 B0 ?( }factorial(0) = 1  # Base case
    ' m" K: }3 x+ d! H" B: t1 k( f2 r  X
    Then, the results are combined:: Q$ {  F/ m6 d- I5 n

    + e  C) U. C/ i2 l3 u- [" {8 [8 w8 V' _: \0 x
    factorial(1) = 1 * 1 = 1- ?2 {9 K( w1 c* G0 F
    factorial(2) = 2 * 1 = 2
    0 H7 e& J  C& J8 @, v& vfactorial(3) = 3 * 2 = 6
    # E$ B  I! v( b0 d/ [/ E( \' S! \0 wfactorial(4) = 4 * 6 = 243 Q9 }# A/ F7 W4 A& f
    factorial(5) = 5 * 24 = 120
    4 l$ D" K7 \1 e, f" p
    1 t0 X. z5 W" g' g' r" EAdvantages of Recursion
    3 E. m3 Z4 k; g3 P* c: x- i( A, H. G) \$ E
        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).
    1 Y- V4 k- x; i3 i/ y. d/ @9 |6 g/ a: v5 z& M* W8 A
        Readability: Recursive code can be more readable and concise compared to iterative solutions.5 [2 a$ `$ v$ _* r7 y0 }) N
    ( r/ ~" W; O+ d1 R2 h7 T+ ~4 p: a
    Disadvantages of Recursion! P# x$ X' j( N4 F; R2 w8 [) ]
    " d! Z6 P0 ]. A4 x
        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.
    : j/ E# D# g6 F7 ?
    ) y4 f- C) k) z1 R4 K    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 c7 Y- N4 \5 H- ]5 @2 v. O

    9 ]/ T0 s6 ~( x/ Z. xWhen to Use Recursion! B* e- c6 Q: N! D4 C6 ]

    4 m& t7 X1 R# x+ O    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' d, g; n$ o# V8 l

    ; U. E5 y2 A7 l* K; j    Problems with a clear base case and recursive case.) F" F% K1 ?* G, x8 @# z: l5 O4 ?
    5 Z7 t9 L' I! v2 `
    Example: Fibonacci Sequence& {% W: f3 [$ D; ^/ g' J1 L% ?
    ; [) J4 T- Q0 O+ V
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:3 M; N/ k' E* Z0 ~
    ( u5 z8 G. T5 }" J7 m$ ^+ ?
        Base case: fib(0) = 0, fib(1) = 1! J( Q- R: D4 i) l

    % @( q; o0 G7 G    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ( b& `' I  j3 Z4 E
    # r1 x4 p2 L9 z1 Bpython# c9 P8 K4 k- G
    ! j* _" \; K# ~% X1 e
    . x+ @6 d- s0 g; I
    def fibonacci(n):; ]" l6 h, C1 U1 J/ ~& j2 P
        # Base cases
    / ^4 M+ p+ I( s6 y* B    if n == 0:4 u8 J" s$ R& ~( O7 y* m
            return 0  R7 G6 ]3 r8 k. F4 o$ P
        elif n == 1:
    : ~( H  f& x8 q        return 1
    : V/ b! D% l4 \    # Recursive case' [+ T# c" h) u, }% W
        else:
    8 K5 u- D: ^& J$ g        return fibonacci(n - 1) + fibonacci(n - 2)
    ( r1 N! \8 Q5 v, d7 `4 B1 S' _, Q5 w2 a! {; e
    # Example usage
    ! I% O  Z  Q" f, A( F' P& ]* Sprint(fibonacci(6))  # Output: 8
      D9 C( H3 k2 ~" ]
    * [* v, N3 @+ J5 ]+ ^: L0 lTail Recursion
    4 E& n/ k" G: `0 T" X! G8 J& E+ |. e  O0 {) d2 a3 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).) e$ j) N* c( L: p
    7 }2 U$ d5 h( @- Z3 B" R
    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-4-3 02:38 , Processed in 0.055968 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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