设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , m2 G: N  N9 i- T. f3 P5 T( K1 O

    5 q  Z: f9 X: {' z% n解释的不错
    2 u8 K$ e* n: h4 _+ b( X2 Z5 L/ t/ X3 y
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& n9 d" u" h' h0 l& s* u2 `0 g" A

    & e3 z+ N; s3 I, W2 Y5 Q% m 关键要素
    + k: e- Y  }/ u4 c- K8 t8 j' X+ o- N1. **基线条件(Base Case)**' f' k; Y2 ?% C' Y* p3 e
       - 递归终止的条件,防止无限循环
    0 p7 S7 h+ |4 G  C# J' O1 i8 |   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1( M$ {& e3 w% H9 a5 X

    $ n9 Q# v( u, D* |2. **递归条件(Recursive Case)**7 A4 n) p7 D6 w, d
       - 将原问题分解为更小的子问题
    0 W! E% G: Q2 {5 M9 N9 [   - 例如:n! = n × (n-1)!6 N( ]  H1 \9 Z& `6 v9 E

    2 c" }& ]/ l: n& o- E( M8 n! `" n: o 经典示例:计算阶乘
    8 D; R5 D% t" u" |9 D( t8 e' dpython7 H" [; t% M! R- e" ?
    def factorial(n):) s8 Z7 A% V, ~) ~# B+ a: h1 n
        if n == 0:        # 基线条件8 {& u. E6 Q1 z+ J% u- Y/ H' a
            return 1
    * v/ ~4 r3 P! I; u/ Y' W) l2 N6 H    else:             # 递归条件
    7 s* Z1 |$ r! x9 p& j        return n * factorial(n-1)
    ( x# q/ C* u- x/ w1 b执行过程(以计算 3! 为例):
    # ]* k( h& P3 g+ P$ U' G3 E" cfactorial(3)9 k! @' J" ?2 O
    3 * factorial(2)
    ' w, B' J) d0 H+ [! B* H) Z. x3 * (2 * factorial(1))5 K$ w& S  L! A- o
    3 * (2 * (1 * factorial(0)))
    / i8 H. z8 t. h4 H# j3 * (2 * (1 * 1)) = 6( M5 N* j" V5 l$ B" T

    ! b3 G2 V5 ~( [. k; \" X, j 递归思维要点: E. [$ Q* K  R$ P8 U. ~
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 M3 }( j6 g/ p* w
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    % L2 B2 A. W* ~9 ]3. **递推过程**:不断向下分解问题(递)2 ]! l6 \' z) e5 \; b7 F
    4. **回溯过程**:组合子问题结果返回(归)
    ) |8 J( ~5 U. \* j8 [
    % y1 ~% ^4 W6 M' ^4 K 注意事项! T# V  X: k# g4 _
    必须要有终止条件, c3 G, p$ U8 V- W- a
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 Q% {0 e* q6 N. ^4 q" N3 l
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , m2 l$ O  P% G" I& o7 ~尾递归优化可以提升效率(但Python不支持)
    # I9 c8 e# O, W+ e$ r. B8 P$ W8 M4 w0 ?
    递归 vs 迭代
    1 x, Z- z* V. p' \|          | 递归                          | 迭代               |
    ! _6 d, y7 O8 @2 o/ G2 O|----------|-----------------------------|------------------|; W' J, H# I9 K1 b: m
    | 实现方式    | 函数自调用                        | 循环结构            |5 I5 N/ L0 \1 ]/ A# T! ?
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |' L, |) {: Q  _" r: n  i
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 V" w3 U/ N5 Z/ u2 z% X% ]* `| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 I6 Z( b1 A* z6 N
    6 F; I) _  A/ O& Q+ W
    经典递归应用场景
    . P0 }. D1 [1 {: u' ^1. 文件系统遍历(目录树结构)
    + P; J8 t8 Q, j+ S1 f/ G, ~2. 快速排序/归并排序算法
    " Z8 r( l, Y. j0 Q, C) ^3 M3. 汉诺塔问题% j1 i" _' ~2 [
    4. 二叉树遍历(前序/中序/后序)( u  [) g6 `8 m4 V
    5. 生成所有可能的组合(回溯算法)
    ( ?# P2 Z4 ?) Z4 y' M( P+ t
    - I3 \5 d: z& i+ i2 u3 n9 x试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    5 T# F$ [8 R6 q我推理机的核心算法应该是二叉树遍历的变种。  c! j9 i: E6 Y' f
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
      U  j4 \4 U  IKey Idea of Recursion1 i$ k! f8 o* _2 B! B1 l
    ; V5 Z( ]& D" c) o
    A recursive function solves a problem by:
      U+ ]2 r& u3 c2 x8 c
    $ l: q) E" E' K; X6 l8 ]    Breaking the problem into smaller instances of the same problem.
      J" l9 _: w0 @( z7 [/ i1 Y$ U8 X( s* A1 u8 U8 J
        Solving the smallest instance directly (base case).
    ' Z$ l/ h% U2 c8 M- K' R
    ) k/ ~; K' P9 f" ^% B    Combining the results of smaller instances to solve the larger problem.
    + \; s' {/ W8 b7 ?6 M/ o, R4 |9 Q
    Components of a Recursive Function3 y3 b, D  t2 O" }9 m
    . F2 x, k6 F7 L- n! {: \' ~8 H
        Base Case:  y& Q3 x7 E( x/ B

    / D# l% X) i" N) M: E+ m        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.) r2 h" V& y' D% i) x! m& Z

    ( g5 L0 d: B* K, b5 g" i: P        It acts as the stopping condition to prevent infinite recursion.
    1 I. K3 F# G& D$ U* [0 S
    - ^# x2 P7 m2 z3 [9 Z        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 U  r  w( a) Q5 L# D5 [

    ! R( v' u) S) X    Recursive Case:6 x! V7 q, ~1 J: r7 k

    ! f: U2 }9 p' P7 s) ^4 R" x* [        This is where the function calls itself with a smaller or simpler version of the problem.
    : q4 c' B, h* F! i9 Z5 V% A6 B2 _. v
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: V7 G# D4 D' u! j

    , ?* H% H# b$ P+ yExample: Factorial Calculation& R( t, k# n6 b2 y% Q6 m( e- V: c
    * u; E' M4 \) ~
    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:+ e; [* B0 \9 Q3 B

    : x- ?) D0 f5 C    Base case: 0! = 19 a8 p+ c9 g/ n
    , z: b3 }$ {2 Q& b5 H
        Recursive case: n! = n * (n-1)!
    7 I: A( T) K% G. Y2 {( P0 s* A  F  C0 |9 Z/ W0 k6 I; f
    Here’s how it looks in code (Python):
    " @0 \/ U# C3 _- |6 ppython
    $ P/ a8 s+ i5 Z$ y  J2 d1 R$ `4 E2 |! K: A7 w$ s
      }9 b! o! d' @6 x% E7 s, ]
    def factorial(n):
    5 L0 b. U) Q. S/ l* d    # Base case
    8 i7 Y- L$ ^" q& K" p: h    if n == 0:8 T3 w2 S# x- M8 y5 v/ s# v% Y, Z
            return 1
    % ^; Z. v- s* _( M0 I+ S6 @- R; L. v    # Recursive case! C* Z# x! q% G) d: }0 \% j2 J* j/ d
        else:
      R8 ~' U3 Q' }6 C3 \4 t        return n * factorial(n - 1)4 h9 B/ r+ |1 H5 C
    1 K+ p3 ]# @5 B+ z- x3 N3 a, `* w
    # Example usage1 N7 M. @  m- A& A* L8 C
    print(factorial(5))  # Output: 120
    % I" Y- Y! Y( A$ ?5 z( P, @9 A# ?) U- P
    ) ]5 w4 g( K( P( y7 ]( uHow Recursion Works
    9 b6 k7 G) y6 h$ |3 {
    * O' w4 O# L- `    The function keeps calling itself with smaller inputs until it reaches the base case.: P( l- N2 A+ L
    ; }1 e$ @7 e: u8 j5 X) |
        Once the base case is reached, the function starts returning values back up the call stack." b! _  P, W9 Z% b% x

    " p8 H6 Z& s& Z0 p3 z5 o/ L. X    These returned values are combined to produce the final result.! c% A% V- a7 j
    % K& {; O! p" n0 c) g! V
    For factorial(5):& q1 a$ y5 H# z" V( a/ v' Z

    0 q( B/ b7 ^  a, u0 t7 j- `8 f5 M& M  z) p( ~
    factorial(5) = 5 * factorial(4)1 ]4 ~2 R* p3 D: ]6 w. G& L
    factorial(4) = 4 * factorial(3)
    - R; M. F5 a$ E8 U  y' Z6 S3 Wfactorial(3) = 3 * factorial(2)$ P6 j7 Q5 z1 n' c) y
    factorial(2) = 2 * factorial(1)
    " a: K+ g! Q6 [factorial(1) = 1 * factorial(0)
    2 x0 H* N# x3 x; |  g# H; Sfactorial(0) = 1  # Base case$ U% y8 T: T2 e; @! j7 W' G
    / A  P& g0 `2 X( E: v' E6 q& W' P6 B" A  i
    Then, the results are combined:# m4 m( s" c! Z
    + Y1 k  P0 \0 g( ~, \
    7 b* o# j& ?$ f" r. e- q' s0 Z4 z
    factorial(1) = 1 * 1 = 1% T0 l( @9 G, L" M  i$ G0 T- m7 b
    factorial(2) = 2 * 1 = 2; X' z2 h3 U0 z. `
    factorial(3) = 3 * 2 = 6* d9 p) ]- ?/ {0 Z
    factorial(4) = 4 * 6 = 24
    0 n( B' r" {* `4 K9 e" q2 tfactorial(5) = 5 * 24 = 120
    7 ]' ]! {$ L+ @. g5 Q  u. O! X+ ]1 C# _' ?
    Advantages of Recursion
    3 n/ X. i, f$ h: o; L$ a1 ]2 n+ z) i. I- k8 B& ~* X
        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 ^3 d  G* ?- p5 m2 b: G
    4 I! U4 h6 b' `  F2 R    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , j% Q; D) L* |) w* ~. M
    * A3 r9 z3 R& X) |( U; vDisadvantages of Recursion% ~' v. |9 U7 w8 f  k

    $ d' y+ S5 a& ]2 m7 A) j  c1 O9 ?    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.
    2 H* a8 ^: {7 D8 y+ S* c6 v; K# P! V6 f; N3 q2 l
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: V( d' ^  }9 T: A* I7 A; ^- F

    ( c1 N$ p6 C+ C6 d, I! k$ BWhen to Use Recursion
    3 N( C2 `4 ~" k6 n  K5 b8 T$ k
    5 @" e& X" ]) \1 [3 _8 E1 w    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    3 s* Q' z' |4 H: C" [% g4 L. G& k
    : ~4 K0 |/ p9 W! P: _* Z    Problems with a clear base case and recursive case.' h( o  r# T. q  D7 O. m5 {

    ' ?& w6 q: ]( T8 p9 ?Example: Fibonacci Sequence
    . u3 l5 |3 u% }( ^7 T4 d/ Y( ]6 B7 q( X5 {9 |# v
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:- O. t+ l! b' P8 C8 {4 b: u
    8 _6 U) h  W; Q7 _- }: P/ `
        Base case: fib(0) = 0, fib(1) = 1$ a- j6 U9 Z+ J  x9 O9 E

      N1 Z3 s. z  F( M, {    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    0 s4 r/ c$ i- g& Q1 A, Q2 V/ D9 O+ r( F- M+ H7 r
    python% ?# R( j6 i( L: i2 J/ p

    % s: K+ e1 N" _- b8 ~. U2 g7 U* }6 O1 x+ k$ ?- Q  ~
    def fibonacci(n):. c. U" K: G% D3 t! m7 Z% a* o* r
        # Base cases
    ! _8 v( y8 a; ^) `2 W! [    if n == 0:7 t3 i& F5 b/ I. m5 x8 r
            return 0
    & l: m5 S, d( T, e( ?9 j9 s    elif n == 1:& e8 ]# b# |: A$ F5 x
            return 1: k. W4 S+ A# G5 }7 d6 e; u
        # Recursive case$ V3 F' Q2 |6 N# k! J' i4 K
        else:8 w5 c( J* I9 [2 T9 h3 A  C
            return fibonacci(n - 1) + fibonacci(n - 2)
    3 Q  t9 f; l: ]. \6 f  A& [& [  a* x7 @% e1 j6 T3 F
    # Example usage
    * n+ Y! r) f# N2 B8 @' fprint(fibonacci(6))  # Output: 8( G; e# s- \1 o& c; y- r# s' @; `
    - n5 \/ ?! L# a8 f1 g
    Tail Recursion
    . y: y: u: g/ C9 U: h3 K+ P2 _! \' @
    / i$ W" i/ `1 T4 }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).
    7 p# w, c& t/ ]4 h3 a# g9 ~6 r4 u
    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-2-28 05:24 , Processed in 0.059862 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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