设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ) }  Y: K4 i$ X. n/ b3 n7 v$ S2 G
    - n# G& s! C0 }解释的不错- _. H1 P7 U+ F2 p4 u! V2 ~

    0 [! V8 i- E/ F递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ \5 h8 D7 C# {5 @% s; K

    % v& y$ Z+ y; m# v0 a 关键要素
      U, J6 o% b# R( Q5 Z6 i6 p6 ]5 \1. **基线条件(Base Case)**" ~2 X1 n# g- N' U
       - 递归终止的条件,防止无限循环& o" O$ l) ^, f" p, U( Y; p
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    & G: _. m$ A/ B, p$ g8 a, ^. k" v& `; h
    2. **递归条件(Recursive Case)**
    , O7 Y3 a& B" v/ b6 c   - 将原问题分解为更小的子问题
    + e. x' Z# _. W* E2 |$ r   - 例如:n! = n × (n-1)!% {  w$ J! n6 W3 C; ~! u& c! x5 _

    8 x$ T8 y/ x, O) Y5 A" Q6 ^ 经典示例:计算阶乘, C: n" e+ A/ _0 f) a
    python/ p, Z4 t. e% T( `, B0 v' h  L/ f; B
    def factorial(n):- m/ v& D( t; p: ~9 K' t* m
        if n == 0:        # 基线条件9 T+ ?! t+ `& J
            return 1
    , G6 n  Y' a8 P2 ~& P; Z    else:             # 递归条件/ n! D6 i8 z; m/ @
            return n * factorial(n-1)
    ! t0 L& W. q6 S+ C- L" [1 t执行过程(以计算 3! 为例):
    ) i# N+ m# O# I1 j2 y/ [& qfactorial(3)7 a; d# [, K# w7 y) C8 a% u
    3 * factorial(2)# v4 \: d$ d8 q# v/ m* m
    3 * (2 * factorial(1))# N' u- [' m, f8 o0 k, c
    3 * (2 * (1 * factorial(0)))
    3 K8 e$ G3 T  ^9 e: h3 * (2 * (1 * 1)) = 6
    , b* J( y7 ]: J7 ]. C, j
    / {) W, e" ]( f, G 递归思维要点
    2 C# c! X. u! ^- y' M; a1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    5 J( `7 w: n& _/ C3 R2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 M/ f* j; }) \# u4 D3. **递推过程**:不断向下分解问题(递)
    , p6 S- |$ M/ g0 @4 g4 T4. **回溯过程**:组合子问题结果返回(归)+ ]) W% p' N$ s2 z( b+ e4 C

    7 P7 R0 q3 e3 E" i 注意事项
    ! J$ O) N7 @# F! D. S3 N. p必须要有终止条件
    2 t# J$ ]* M2 }5 f递归深度过大可能导致栈溢出(Python默认递归深度约1000层)8 e5 v% {7 Q5 I0 n5 Q
    某些问题用递归更直观(如树遍历),但效率可能不如迭代4 V& D5 b8 H3 a! O8 H) f' ]1 G, |
    尾递归优化可以提升效率(但Python不支持)
    ( m3 b1 \- E) P, t! ], l  W9 [5 S
    递归 vs 迭代
    4 B, i1 [6 Z6 l$ ]* @7 S# t|          | 递归                          | 迭代               |0 m# |5 `3 ]1 T! p; u
    |----------|-----------------------------|------------------|
    ! D" H" g2 ^4 u" f1 K| 实现方式    | 函数自调用                        | 循环结构            |
    , ^' y$ s' K6 C2 y5 F% S| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |: o) c) e( {, {( \8 H( z3 k  ^9 X" W
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |. s5 ~% r% {2 t
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    . M7 v6 @& V( {) @7 V# l; {& j/ _- g0 g4 C4 t1 V0 }
    经典递归应用场景
    & g. q9 q8 [% |8 U1. 文件系统遍历(目录树结构)- c3 l& ^1 h, ]! b& b6 E
    2. 快速排序/归并排序算法
    0 @( g( W7 ~0 H7 v3. 汉诺塔问题
    8 r) y/ F. l/ I7 @* L- _; S4. 二叉树遍历(前序/中序/后序)) K7 {. A7 t" l5 ?+ D* G2 I
    5. 生成所有可能的组合(回溯算法)1 m. x: Z3 M8 s7 H  O+ Y% t$ s& K
    ) _1 [" D/ g& F
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    前天 07:04
  • 签到天数: 3377 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 ^) k6 H9 D, ]0 {% u1 C
    我推理机的核心算法应该是二叉树遍历的变种。
    + m$ V* s+ L( V2 L1 `另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 j! o5 n% J( J+ XKey Idea of Recursion5 m7 o1 @; a3 X, e

    7 C4 Y" O/ w) `* C+ P; d7 `! R: @A recursive function solves a problem by:
    * ]6 n4 }0 e0 |, E  x" T  X  Q( O4 f' W6 j0 }* D/ }  A$ \0 D8 n% P
        Breaking the problem into smaller instances of the same problem.
    * P: ~, T: a) p! H
      y6 c4 P* V4 u8 t    Solving the smallest instance directly (base case).2 J. }: d4 ?" j% {% j0 W5 S

    " o& F# W' P* M1 I" ?: B; L" r    Combining the results of smaller instances to solve the larger problem.; d' k) ~- _) D; n

    ) g2 n% @( `4 j% T- ]Components of a Recursive Function6 t$ C- X) o! C  c& }) d0 L

    0 ^% H/ ^+ s! ^; W0 ~3 X, r    Base Case:. y% L3 ^  \& S" u8 W& ~7 e
    # K  @0 u. l/ I; u- \4 o
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # n! ]% o) M+ a; c
    ) D/ F, S1 e2 ?( O% e        It acts as the stopping condition to prevent infinite recursion., ?9 f7 G  f& x& @3 C

    ) ~/ u- w9 B* D1 S3 ], Q        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 f8 o. L  z0 W/ p9 P4 z: F6 Z7 M5 Y8 o7 I
        Recursive Case:& ^1 s% t2 M5 f$ H/ P9 f: z$ Y

    ( H" i+ T4 j* n1 @9 n        This is where the function calls itself with a smaller or simpler version of the problem.
    ) h$ q- Y& Q- }! e+ `
    ' J8 C: a" b% R9 \. y        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    . T3 Y! j0 Y6 G% d5 V1 i" x" d6 u
    9 ~/ b4 \0 ]" V# ^6 h. W3 wExample: Factorial Calculation
    / @: U' D( w- m" i8 X( t* O2 i, Y
    3 r/ j. ^$ a7 ]+ V2 }4 tThe 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:
    , M- N8 _/ A2 o: M/ ?6 R! D
    ; z+ e. O6 z1 o/ B. T% H    Base case: 0! = 1( S1 k6 E# b8 R0 a9 n% z
    3 O: x! X1 N& S& _3 d
        Recursive case: n! = n * (n-1)!
    * I# q% l: i! P% L- M
    : I% Y. W, W2 ~" r7 o; [Here’s how it looks in code (Python):
    5 x* g7 e) k  r# d9 D. _$ ]python
    " H* r+ j+ b4 U
    # q5 K& V  B, ?! l. u% J1 c0 t/ P; c! B3 W6 a; Q6 X6 \: ^
    def factorial(n):( J; Y& N& W: t! Q  l
        # Base case
    1 J3 @$ Y! q6 d- Z' A    if n == 0:5 F" }3 j0 U( \  d2 t# X
            return 1$ j! ?* z9 I) s  }
        # Recursive case
    / r) C2 _. ]3 x/ q    else:
    # x  ?# A% Y/ ]' \$ g  j        return n * factorial(n - 1)
    / Y$ E1 F- `) z6 y8 S3 f7 ]! T6 ]& i
    # Example usage
    9 {; @" L! H- K  Z# n% Yprint(factorial(5))  # Output: 120! C4 b/ Y1 P- Z- O: }  v3 M

    & b. e2 ]& Z+ OHow Recursion Works( s3 I# f0 X- A$ _2 N6 r3 G
    ) m0 }8 d5 s! `) G- C
        The function keeps calling itself with smaller inputs until it reaches the base case.
      s- ^# ]0 z# }) @
    ! O3 i( d2 G* K    Once the base case is reached, the function starts returning values back up the call stack.
    3 H1 Q1 K% h1 }* A9 s/ f2 V& z$ Z2 m7 _4 x) k
        These returned values are combined to produce the final result.- K* G# m9 [3 p. U
    $ d" z1 J! K3 d/ I2 O; x
    For factorial(5):3 H: r. u1 d. O# n& _* v: d

    & q4 B* x' Q7 c4 T
    4 _5 P+ p+ B! nfactorial(5) = 5 * factorial(4)
    % A6 W! f; {. V% k: ?( n% t0 ^factorial(4) = 4 * factorial(3): o# \4 u% u, q9 I
    factorial(3) = 3 * factorial(2)/ v9 i( m8 }1 G4 N0 R
    factorial(2) = 2 * factorial(1)
    # x) @: ^2 K" G6 `factorial(1) = 1 * factorial(0)
    6 ?; z2 ^: k7 c& D2 E. B9 kfactorial(0) = 1  # Base case* u6 k5 n( |* I, L
    " M4 q" h3 l* [; `1 h; ]7 X
    Then, the results are combined:
    - T' o5 \+ S- r* H: S! ], c/ [; r
    $ O% A5 \. K- @) w5 M, ?0 m! i0 S2 w) d0 z$ ~2 r
    factorial(1) = 1 * 1 = 1$ I2 R( ~  }% _1 L8 G
    factorial(2) = 2 * 1 = 2
    , C$ d1 m! Q: zfactorial(3) = 3 * 2 = 6( O4 _) n9 R6 k
    factorial(4) = 4 * 6 = 246 y% z! k  P( A% E: z8 d
    factorial(5) = 5 * 24 = 120
    , j/ K- F: z7 u
    ( T* {# t: J: a, }8 U' y4 |Advantages of Recursion
    : o( r6 J, X$ y5 t6 _- a5 w/ W, i- n8 D( p9 c, {6 ?9 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).
    2 H0 y0 y7 k' U8 Q4 x* @. T  c: B/ i9 Z
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - J1 p" o9 |. e7 K" W& z1 E  J; A9 [0 Q" N! |, V
    Disadvantages of Recursion' |# \% U4 q) j4 R  u3 G5 c! l

    3 C; Y( L9 ]) K- F, y) l    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.; K6 {$ H+ }& @
    4 \$ M3 W3 e; S5 @6 h' s
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# o8 t/ r0 s* j3 T. e! R. P7 b

      T* i2 p7 W" T2 ^4 r, Q( T3 GWhen to Use Recursion! `# b  [3 P- D! h
    3 T3 z9 K3 Q: f/ \% Z& P1 T0 b8 m
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + G% k3 z, l6 Q0 c, i- W! v2 Z* G4 }, i
        Problems with a clear base case and recursive case.
    " R# o  t. _, ^3 a  _7 O3 @
    9 e8 C: r4 Y: b) f+ @5 k, m  FExample: Fibonacci Sequence! D4 h$ C" ~. u! X
    4 N  }6 P6 F3 T
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    2 F5 D' M  i3 `. q8 b$ p, r
    % G8 P+ Y! `! p# Q0 V    Base case: fib(0) = 0, fib(1) = 14 B) y4 o2 y" U' z9 @& f  {  P

    ! g. h0 _! d& t; i+ X& O    Recursive case: fib(n) = fib(n-1) + fib(n-2). F  ~" N* A# {! m. X1 ]! Q) W

    - N; E6 r: f7 A8 X7 kpython
    + }1 [) m/ f' B5 k* u& W# V9 K
    % n# T; i" s$ n* j
    % J. P5 J7 O" F% y$ v" O/ Jdef fibonacci(n):
    5 R  A" B8 C9 g$ I) m' s. E    # Base cases
    - \4 m/ q% q0 c# l* u, J    if n == 0:
    1 t% q8 X6 M7 D  u7 v        return 0* f, e$ s, P1 N) R5 W% v3 b% C
        elif n == 1:
    # [6 `9 {" |* h2 ~1 T7 \        return 12 }% s6 M1 S3 Q; w
        # Recursive case
    $ b$ W# p' Y3 R4 S    else:
    1 n- S. U) `! j. B, T9 m3 a        return fibonacci(n - 1) + fibonacci(n - 2)" b* L( [! J; |" {" I) ]

    8 G4 w! y) z$ f# w; @# Example usage2 `; j& N" r2 w) ?* r
    print(fibonacci(6))  # Output: 88 j6 `: S- X/ I* ^

    * C6 s+ X  h$ s( `/ d, eTail Recursion
    3 c" T% `0 z( _1 c
      I8 d7 W2 h- b% zTail 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).4 \) X& K7 \- e. ^

    7 r4 J5 N4 T) u& |" o0 n, jIn 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-10-14 00:12 , Processed in 0.065932 second(s), 23 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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