设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % Z, `7 Q  n6 C3 P2 A) U
    . N& m: i& X* a/ {8 T/ _1 X
    解释的不错3 k* j6 U6 C/ `! B; a
    / Y3 r# X" V9 E4 \' v  {
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & V4 l- \, f  X! q5 M6 z/ O2 y; ]  q* G' B' r" P
    关键要素# V! B  ]: P) B  M- J* h! d8 A
    1. **基线条件(Base Case)**; f( p: D1 N8 U
       - 递归终止的条件,防止无限循环
    # m' s9 N7 \( d# l   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1: w) b" i3 x4 Z$ v5 d  c
    . {, H& v: @6 v% J( t
    2. **递归条件(Recursive Case)**  q; A3 p/ n. x) k; v. U
       - 将原问题分解为更小的子问题! Y8 I# z  ?" |. q; n9 k
       - 例如:n! = n × (n-1)!
    0 T- \# L' F5 J
    - ^& p9 n1 {' N# A% F; S 经典示例:计算阶乘
    : {( ?; K5 P! g% Spython0 t# o9 |0 I& T& n( a4 R- W9 u
    def factorial(n):
    + v! V4 H2 L0 B) T7 [7 m    if n == 0:        # 基线条件
    6 u* \4 A; O+ e' q7 ~        return 1
    & |! t. [6 j9 ]( I( W/ y! U8 H- H    else:             # 递归条件
    2 m" a! r3 Q- G, d4 q        return n * factorial(n-1)
    2 t" z  M8 h; L& g. Y- ]执行过程(以计算 3! 为例):" v5 g" l7 g$ Z* B6 M& j3 Y
    factorial(3)& ~5 P6 `0 i. w+ A2 S4 T" v0 \
    3 * factorial(2)
    6 A* O# P( o; {7 Z3 C% e3 D; z3 * (2 * factorial(1))" U% G! W* m6 C& e4 n
    3 * (2 * (1 * factorial(0)))
    " K2 j- p+ f+ k3 M0 i; Z3 * (2 * (1 * 1)) = 6
    # h# X: K' U; {& s
      r! K4 A5 W6 a0 x- G0 |( H* E 递归思维要点
    % e) [" R; R9 l6 _  u' U. S1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    4 E4 e, ]. D- N2 X6 ^8 E: ?2. **栈结构**:每次调用都会创建新的栈帧(内存空间)9 ]' _. v, ~/ ]! g  K6 y$ M/ O9 S
    3. **递推过程**:不断向下分解问题(递)
    3 R0 `/ x$ D& q- B) l* ~1 X5 E4. **回溯过程**:组合子问题结果返回(归): H+ v, [* `6 V5 R% a

    . Z, @1 e8 B" @2 A2 o 注意事项
    5 g; e$ i- W6 ~7 K+ c必须要有终止条件
    & G' d4 Y0 {8 z; D% O+ e6 o* O! l递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' U" Z& v5 j1 Y+ B4 s) \+ \某些问题用递归更直观(如树遍历),但效率可能不如迭代1 Q0 C6 C% p; ?& E8 `! v. `0 _0 K( W8 L7 \
    尾递归优化可以提升效率(但Python不支持)& a% ?0 B- Q- B% ?

    % G; s- |3 q1 {, P' D 递归 vs 迭代
    / i: k5 u3 o$ J+ |3 V  t/ j: {( I3 C7 t|          | 递归                          | 迭代               |( y' F* N) q* e2 S( `3 a- h3 _: C
    |----------|-----------------------------|------------------|* r3 h- Q- q5 [$ _" q7 ?6 `- ]8 n
    | 实现方式    | 函数自调用                        | 循环结构            |
    2 Q% H. ^, y* V# g2 t( V| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ `2 K2 b$ B0 F( p0 w' F+ p8 f
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |! [" Y: l; L- q, h8 V9 F
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 K0 N5 f! L" Z4 n1 T! c0 y' b
    ' {- j* z2 c8 D8 i  l6 j% s 经典递归应用场景8 J) |; l4 c/ }' o% S0 l# K1 }8 R8 |
    1. 文件系统遍历(目录树结构)4 I* M3 ^' V5 Z  G
    2. 快速排序/归并排序算法- z) h4 X% ^9 c  y- m5 ^% ]; O) u' ~! L
    3. 汉诺塔问题
    . O! ]7 t/ i% M! o4. 二叉树遍历(前序/中序/后序)/ y( U- g4 H, p# v
    5. 生成所有可能的组合(回溯算法)& w0 h2 c" k  C& _4 |
    * [/ K' S2 p; |1 ~, ^
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    5 小时前
  • 签到天数: 3229 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% i. k4 d; ]7 w/ |  W
    我推理机的核心算法应该是二叉树遍历的变种。9 S  m3 x, P; q) [0 k7 y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:/ w% d# P7 F$ r& S
    Key Idea of Recursion
    ! d9 _, F6 r( i1 u/ ~/ v4 ?2 k& A1 Q# C3 |3 f' ?# [( q
    A recursive function solves a problem by:
    / z6 E) e! P" ~! t/ w5 X; ~
    6 o4 c! h. _) `8 ?: z    Breaking the problem into smaller instances of the same problem.
    0 p, h2 J0 t0 O) s0 P: \, L2 X$ e+ I. I- B) l8 I/ t
        Solving the smallest instance directly (base case)., B. B2 M1 Y& `4 K/ K. s0 G$ {
    ) U# j( w, D4 F- L; i
        Combining the results of smaller instances to solve the larger problem.
    4 V$ |7 N# f. h0 k: K% }+ Z' B7 ^5 }& V' {7 g
    Components of a Recursive Function, b& _3 X( c2 u* [) M& j" @$ d
    $ P1 z( z2 {& {
        Base Case:
    - [$ G7 F. N) ?; E, ]; K' O- C. l# D; H5 }# U0 X
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    . a% S- z( G& z: V2 A) k% E/ I
            It acts as the stopping condition to prevent infinite recursion.
    2 D8 E; T8 }% F- Q; _* A: ^
    9 k! Q* L% j, H1 _& h        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.6 |3 P; l! [4 D0 T) r% A
    ! z* p: B: _! ~
        Recursive Case:  T8 J# o! _, H  o9 }

      P" w' Y+ Y9 Z+ d" G, P' I        This is where the function calls itself with a smaller or simpler version of the problem.  A! L5 P2 O' a$ ^& Q

    5 w5 |) D0 ~% M$ R! p        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' S0 X$ Q0 y7 s9 K" P; S, S

    1 f2 O, i7 O. hExample: Factorial Calculation6 @: l/ `) v' \6 o' J) `. \8 t

    : |$ E) D; R) CThe 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 |1 M  \* g: S2 r* u- `, v

    " j1 c2 G; ~" c  \$ D0 }# V6 l    Base case: 0! = 1
      i6 V/ L% L/ U) Y0 {' I  K5 J& T
        Recursive case: n! = n * (n-1)!! }/ l& p: T! h0 W& Y
    + x7 }- l6 b+ ^9 G& n
    Here’s how it looks in code (Python):
    , M6 }9 \- o" H4 W3 F1 spython* B6 N: Z3 E4 V  r+ k/ Z9 n

    ( M4 {" H6 b. O
    9 Z( K  u7 w( o) Z$ P' vdef factorial(n):
    8 V8 M9 x( [9 F/ }/ ?    # Base case% e( U5 m/ `/ Z/ z( N; g; @
        if n == 0:, |8 S1 h8 G1 C3 d' w4 `
            return 1
    1 A/ y2 [, X/ Y& R2 k( i- h! V    # Recursive case
    2 V/ @+ c( C9 E+ G    else:
    6 A. Y9 m$ p* ?7 X9 |" M/ Q4 c3 d        return n * factorial(n - 1)
    * _- v! J* c+ k; Y3 `) m' a/ p1 a2 P& i* X) g% Q3 m  D$ P* k
    # Example usage
    ! F- v3 W1 @+ Fprint(factorial(5))  # Output: 120
    ) L, n& j( Z9 p* F8 E8 ~8 R6 r, V: H  h1 Y$ c/ I* g) r$ l
    How Recursion Works: b8 s( }2 G# |1 z
    2 X2 I; m4 K% _: r+ P3 b
        The function keeps calling itself with smaller inputs until it reaches the base case.
      I7 l' s' ~+ O% L4 k5 A$ ], I
    % h# M- x4 M3 ~  O    Once the base case is reached, the function starts returning values back up the call stack.9 a# ]: T6 v; I% g5 I

      h6 A  ?( u( F; I; }    These returned values are combined to produce the final result.
    1 j0 `& |% Y* c2 Y$ I9 `/ \: l0 r+ u. U
    For factorial(5):+ P2 g& l& D% {: s
    % A/ T# f5 k5 c" F/ S
      z! X8 v; K( p) K: W
    factorial(5) = 5 * factorial(4)# `0 W- I! G9 d; N9 K/ s& A
    factorial(4) = 4 * factorial(3)) T, r9 ?" N! x, a/ r* t  o
    factorial(3) = 3 * factorial(2)4 ^3 [9 z" o" x, {' A$ N- t$ s
    factorial(2) = 2 * factorial(1)
    2 G; u! N7 V  O0 Lfactorial(1) = 1 * factorial(0)
      U8 b! f# I8 R2 Efactorial(0) = 1  # Base case
    6 W' d( Y: C1 H, m) h4 ]1 E+ K4 J
    8 t0 d. x1 N- }3 o- HThen, the results are combined:
    9 k0 e7 S% V" l% [% @0 B% F7 }1 m& o3 R) {4 r+ c/ L

    - I" ]% D9 U6 W/ kfactorial(1) = 1 * 1 = 1" q; w9 y5 j% i$ ]
    factorial(2) = 2 * 1 = 2' X" c: w, V3 c  l" [2 o8 x! T
    factorial(3) = 3 * 2 = 6
    ( _3 K# G- K- E. wfactorial(4) = 4 * 6 = 24
    + x% K! s, C* [  J  o6 X0 j, wfactorial(5) = 5 * 24 = 1200 f/ ^, J) i8 I

    6 M% e4 z- y4 EAdvantages of Recursion
    - P, M  b4 c: O1 }3 b% z; }2 V3 e" Z+ r% t6 t% k% F
        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).
    : l2 h2 Z2 Q, n9 x, I2 \8 B
    3 x# A- {3 t. _3 j    Readability: Recursive code can be more readable and concise compared to iterative solutions./ s! t; N' i- k6 F
    8 M8 u0 {. U( T
    Disadvantages of Recursion
    : j+ o0 P( p& V: d3 W9 w1 r
    - v* D  E% j4 o) `3 {    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.
      `: w! }6 z+ \$ r" E; |
    4 L( M6 m! A' E7 v8 Z) @& ~4 k% f    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).- V) R0 y  ^" z( V

    3 {9 c  S+ {5 c3 J3 p1 }When to Use Recursion4 e) H/ ~; b! `3 C  E* K/ @
    $ Q% C: H/ ]& p2 I" o( m4 A
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- B6 a, y* Q, q; P7 G2 `
    % H' @9 k1 u6 I% [% O
        Problems with a clear base case and recursive case.
    4 ~: ~* N( M+ Y8 G" c2 I8 F& Z- {, u5 ~3 y
    Example: Fibonacci Sequence" @5 c+ ]& x8 \

    & U7 I, y; j$ l& A) ?: vThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ) @0 n5 C2 b& h- U5 h
    ; ]2 S$ }  O/ f3 }    Base case: fib(0) = 0, fib(1) = 1
    + v& X2 w! I& e2 ^; l" C5 `) Y/ j  m: \- g
        Recursive case: fib(n) = fib(n-1) + fib(n-2)) T! C# X* T  l3 p1 T9 V1 e, y
    + r; }3 N1 M1 L# g( P
    python
    $ Q+ X: L5 L. t8 t" W( T# h: G. ^2 A, t
    ( b: ^' M1 v. f
    def fibonacci(n):' R) o; m/ n2 G9 S) [; m; c% i
        # Base cases
    $ b0 D3 C/ {8 F# y7 \$ E' }    if n == 0:# L+ Z: A2 p3 g% z
            return 08 A3 I! d7 b7 K5 W1 E" M
        elif n == 1:4 f: I" `: u3 q' P: }7 W  L
            return 1; Y- `0 k# m' x7 R; c  E6 S, k
        # Recursive case
    : T. _; H* Y$ E% c' w$ w( {    else:
    9 H, O; D3 w, v4 G8 \5 f        return fibonacci(n - 1) + fibonacci(n - 2)
    9 q, l, f9 O* O3 w! @: h
    9 n6 F9 I( e& B. q6 Q- e# Example usage% Z3 l) }+ d. d: q" w
    print(fibonacci(6))  # Output: 84 v9 X7 x% o7 d+ g+ Z3 x
    " `  p( q8 @2 P3 x8 y% c; H1 q
    Tail Recursion
      S) T) ^/ F9 K8 i- W; }* o# ?4 W
    3 s( r# l7 V7 m7 t5 J3 ^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' M- `2 O$ N1 g% k
    & P! r- A1 b: Z3 z3 Q" _; ^7 b9 O
    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-5-2 23:26 , Processed in 0.055905 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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