设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 D; R; D! ]5 t: N0 x6 b
    , Q0 t5 z) w  Y; ]% Q- w$ E. y2 D8 U: U解释的不错9 ^- S' |3 h" u

    1 h, W8 E" M4 v) o4 x: R递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ f2 s( y: j" J

    " B4 q8 T# H; e% M( l2 p5 \, X& c! y 关键要素
    2 b2 N! u& I% B6 @% q5 k1. **基线条件(Base Case)**
    0 |6 H/ o* K; j% X+ l   - 递归终止的条件,防止无限循环
    - W7 `. {2 B. u: e% E, D# [7 T   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1$ _* Z2 {) `( l3 k5 i4 j$ q

    2 J# }5 T2 H6 {  [2. **递归条件(Recursive Case)**
    . k+ j' ]; K9 f# s2 F* u. g' @   - 将原问题分解为更小的子问题: s# Z- T* J) J! m% y( k* ~5 k$ F
       - 例如:n! = n × (n-1)!* h% _) U( K( W+ b' w1 s6 t
    7 J% b' X" b6 H
    经典示例:计算阶乘% @/ x: R0 {# n; J) I6 s7 w
    python- B. C3 W( U' B( j. O' J
    def factorial(n):
    ' n5 {- {( f, W    if n == 0:        # 基线条件% p* R4 @8 I' M  B- j9 }) F2 U
            return 1
    8 J' B/ n0 M: V    else:             # 递归条件
    ! s: ?: q/ M3 n$ m/ \: u5 V# X  P3 H" r        return n * factorial(n-1)
    0 `7 D1 j2 l; R( t4 z3 p执行过程(以计算 3! 为例):8 Z- z5 X9 O+ A7 U2 O
    factorial(3)
    % z* t, Q9 h" ~9 a. d3 * factorial(2)# r7 F5 [; y- H0 Z
    3 * (2 * factorial(1))
    . g+ @$ p- b+ l1 m; n3 * (2 * (1 * factorial(0)))2 h- Z0 N( {& z' W* m. z
    3 * (2 * (1 * 1)) = 6; P" i2 |3 E- i, {
    : Q: s: B+ D- P
    递归思维要点
    0 D% y( E0 N2 Z) Q$ P1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    - I9 [" b) |# z7 r8 p1 r2. **栈结构**:每次调用都会创建新的栈帧(内存空间). w5 ?2 a( h7 T
    3. **递推过程**:不断向下分解问题(递)* }# Z, M% g! v
    4. **回溯过程**:组合子问题结果返回(归)- E3 z/ Q# q7 ^0 a6 y

    " a+ W+ o0 m; l 注意事项
    + Y$ W  I: B8 w必须要有终止条件6 u; Z2 B9 m* @9 ]* N
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)/ Y5 [+ v, e/ t
    某些问题用递归更直观(如树遍历),但效率可能不如迭代: N% K7 W- E* c& y& z# {. c- x
    尾递归优化可以提升效率(但Python不支持)
    * c; e- p/ S0 ~2 A( N7 z; v* H$ P8 Y3 W% ^! P/ G' R( c
    递归 vs 迭代: K: z" g6 m& W7 j$ x
    |          | 递归                          | 迭代               |
    ' V# v/ L- W) N7 ~' T! A|----------|-----------------------------|------------------|! O: p4 v2 s1 ^6 b
    | 实现方式    | 函数自调用                        | 循环结构            |2 Q" ?; l) x; q; z0 _0 f
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |2 {* W) c4 {; ~/ n( n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |0 ~( I- ?& H. {9 i4 Q. o
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |6 p" |3 U$ _0 K2 C- P/ [3 F6 d

    - ~' ?6 n! N  F 经典递归应用场景( Z( u3 E; G- m5 V- Y$ W/ b+ N
    1. 文件系统遍历(目录树结构)% `& _. `9 O  q- {: U# i1 t$ g
    2. 快速排序/归并排序算法& R' _: A% v) S2 O5 v3 |% T
    3. 汉诺塔问题
      ]( i& f- t& z) ?% e9 e4. 二叉树遍历(前序/中序/后序)
    0 l. D* ~: K/ [& I' c- z& n9 S& I5. 生成所有可能的组合(回溯算法)1 k( L9 y* W- p7 d( `7 p; `

    6 M& u; |' K0 ]0 C6 r* X# M试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 07:17
  • 签到天数: 3120 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) S, J( a- F) K, N1 |
    我推理机的核心算法应该是二叉树遍历的变种。# I1 X& R# t8 s6 ~' u. G
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    8 `4 f1 F- Z- _; E8 _# m5 l* F+ TKey Idea of Recursion
    4 i1 o% _4 |( \+ {9 S5 F% G% a% J& f
    A recursive function solves a problem by:2 x: Y& }- t9 }" x
      J! ]# L5 M$ A0 f! U" U5 T
        Breaking the problem into smaller instances of the same problem.2 u# H$ e  W9 L; k
    $ n! y0 V3 {0 l( n  \
        Solving the smallest instance directly (base case).+ {8 P# w  Y* T- J+ g5 J+ p

    : X6 \" x6 i5 H8 p8 \7 b    Combining the results of smaller instances to solve the larger problem.
    1 }( `7 f$ V' b1 E8 i& l* F) c$ }& L. j
    Components of a Recursive Function2 ?  a4 Z# p3 s  A% w
    3 b: l( U4 P7 ]6 g
        Base Case:
    1 c1 k# m! p+ E7 V6 d% `7 l4 d$ d8 O/ e7 U/ \) }7 Y& f
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.0 L: Z% E; c5 A/ S8 y& m
    3 m1 ~: }" P! z, ~3 n8 o% _
            It acts as the stopping condition to prevent infinite recursion.
    ' t. x  o7 x; R- h# U" S3 _) I5 T9 X7 C! u! p+ [, p( a2 G
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    " s- B  l( ?4 Y& U  D- K$ x: _! e: P
        Recursive Case:
    7 l0 M' J7 ]7 V! L
    1 Q  p' h* P# e9 Y& b( Z/ W0 J        This is where the function calls itself with a smaller or simpler version of the problem.
    $ L) `9 x# C. A$ r2 W. Q0 R* Z7 Y/ u& `% C) H- R0 V: V
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! E5 z* }7 q& M3 e
    1 U$ @& d) F' y% z  }
    Example: Factorial Calculation' q, P* V( Y$ a$ X* o8 f! ^- b

    ; H4 ~6 T4 v& X7 Z( v+ Q+ oThe 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:
    1 E" J/ \6 K: ^" ^4 w/ {6 `9 w5 Q* h( `7 h
        Base case: 0! = 13 [. p2 Q5 g" s* R: k
    : [- Y' U9 D3 U. [3 L
        Recursive case: n! = n * (n-1)!
    9 s- k! {/ L  B7 t4 `; O: J" L: _% J! X" h
    Here’s how it looks in code (Python):
    6 O( K; B% d: N* F0 Bpython+ L* J4 D" O' j5 N3 E

    / n8 Y) B) R& z: Y3 ~( P9 R) M: x" U. a. l) Y0 }8 |  ^0 J; W
    def factorial(n):/ M  G/ r+ U$ S' }
        # Base case" ?3 P/ D9 ~$ U0 r$ D# W6 R/ K
        if n == 0:; J( o5 }* t& v: Y; J2 ?) ]7 y8 p6 P
            return 1
    / z$ L7 A+ O! \8 M6 A    # Recursive case
    & Q; x' m: E: F    else:
    ( ^2 A5 \! r! D' f& o- ~        return n * factorial(n - 1)
    2 l. N. B) U7 F4 \/ f: K
    + y; e2 r" ]( h/ l; c; d# Example usage6 K7 C+ j* H, G/ r, b9 @
    print(factorial(5))  # Output: 120% {) m+ b. ~  n' ?: w% i& J! _5 |

    ' T$ [8 O9 ~0 ~$ w, qHow Recursion Works, U  K* j* \+ w6 @# c; H0 j
    ) @( I; Y; i* c$ u" r* L2 }3 Y& p
        The function keeps calling itself with smaller inputs until it reaches the base case.5 {9 _' {& D+ j0 Q$ F

    2 b9 P, H* ~3 U# e, i- `    Once the base case is reached, the function starts returning values back up the call stack.
    + k1 d3 K; h/ L% [8 C
    8 z% v% F% m2 `5 a$ E    These returned values are combined to produce the final result.
    1 E1 k8 j7 ^. U; i) @9 \; v! o, F  E0 o. L
    For factorial(5):
    , G2 }; F; Q7 q% B/ u; ]: J) V5 a% ~* P4 u$ G
    & O2 o% W2 h! z3 R
    factorial(5) = 5 * factorial(4)  D6 ~: h  y; q% ?. j* {# e6 R
    factorial(4) = 4 * factorial(3)% S* d" t# I' i' E
    factorial(3) = 3 * factorial(2)# A8 c$ S3 c: x5 g
    factorial(2) = 2 * factorial(1)4 ]% Y3 q1 N; j
    factorial(1) = 1 * factorial(0); y- L* Y" Z) Y! i  ^
    factorial(0) = 1  # Base case
    9 E& o, s5 l! q
    ; x  H1 N- B' \% B" s6 F/ HThen, the results are combined:( |0 T5 @$ ?# }* `) Y" `8 x$ \

    0 r$ E* q- g! b1 G4 X9 b' |& t7 d2 c* ]1 N
    factorial(1) = 1 * 1 = 1
    & _: {! R5 O5 L. Z( hfactorial(2) = 2 * 1 = 2
    * @5 ]) r$ M* o; w0 k8 Nfactorial(3) = 3 * 2 = 6
    % z" X5 @* r4 H3 B2 ~factorial(4) = 4 * 6 = 24
    ! [; A/ M" h% u& g, q5 u( v& V2 Efactorial(5) = 5 * 24 = 120
    + X4 \: v( a4 {! S
    $ y$ G9 N4 }4 y9 G/ d) u9 e1 m+ HAdvantages of Recursion$ P+ G# I5 Q$ D% ?& z$ \+ O
    ; E1 q4 O& D' L( T0 f, w0 v$ 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 {' G1 T- \" k5 c, @! V' L5 x! ^& _$ ?
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    5 H% a9 k+ ~( g2 ~
    - D8 X$ z' T6 o: T* Y  D2 hDisadvantages of Recursion
    ! h. h3 U9 Y& z, M2 S- U3 m! C3 |
        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 ^' p# ~4 m& {* H9 i# K  n$ K
    ( e. ^2 _7 V. ~9 u8 `: T& _( y
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).* x" o' n  h4 ~
    4 {1 [3 [# R1 v' c
    When to Use Recursion% h) X9 x& ]0 M5 }* G# r

    5 x7 c8 s  G1 P% O5 u7 O    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ' D" S& s. ~3 c# ~8 ?6 D3 |( F! u7 Q) E) k
        Problems with a clear base case and recursive case.
    4 g9 K% S7 G- x, |0 Y( B! @. U7 y. b2 p* ^% k1 E& m; B
    Example: Fibonacci Sequence
    0 m$ m5 U! a. @# o
    $ ~4 |" y6 j/ q7 z7 S. h, F9 TThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:! e4 c* c8 i- f

    # u7 l& R2 X$ d# o    Base case: fib(0) = 0, fib(1) = 1
    8 Q6 ~* ]8 o2 d* N/ t  n" j
    8 ~% P& p* r; ^+ t% o3 y% v3 w+ X    Recursive case: fib(n) = fib(n-1) + fib(n-2): a8 u, T0 T* o% D
    ; s) N9 D* @1 e- K9 b% y: d, q1 G
    python3 k% h2 b0 Z* e' m2 ^9 P5 `

    + ~. ~* Q, u& Z) w$ M' l2 K9 g  M$ X" F
    def fibonacci(n):
    $ O# [4 r, I% ~1 n7 D    # Base cases0 B" u9 u5 D$ C9 Y! f( k7 Y
        if n == 0:
    $ O1 y+ u" u6 A% m: M, d8 O        return 0" o4 f8 L* h& `' C3 `
        elif n == 1:1 S0 c* j: X, Z  k( t
            return 1- {7 a9 G8 V) [! i2 M) `
        # Recursive case
    9 d) x( I% k/ y    else:3 n7 {2 W# N% a! }
            return fibonacci(n - 1) + fibonacci(n - 2)
    ' [6 ]- x) B7 p5 R8 f" U) a) i0 x/ a; n7 |" c# k
    # Example usage1 C6 r& H* F: X/ C- H* c
    print(fibonacci(6))  # Output: 8: p' d: e3 ~0 b& b& q" t9 f

    * l6 r; u. g& M8 uTail Recursion
    ' X: H/ F- O7 D$ }" P+ D; N
    & @) T% C  u" N6 z6 W  R1 PTail 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).* Z! [9 W% {4 }/ Z( N$ V
    ' k( z) f( q' G4 w
    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, 2025-12-18 06:06 , Processed in 0.031018 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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