设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + X1 c" \$ W# g! e' l  Z! H2 r
    ! s3 D0 }$ ^4 d& ?7 W, q7 L8 \0 m
    解释的不错
    0 d  k. a8 o7 m' \' ^
    8 g1 T3 K& t# a) ?( e% o递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    8 G5 D9 w. x3 R& e5 H5 E+ w0 Y' ^& M4 D* p
    关键要素
    ; n  K' \+ |0 n! M' ]1. **基线条件(Base Case)**. G: ^( B" Q0 i% f2 M1 t6 L3 P
       - 递归终止的条件,防止无限循环, C1 g7 V/ X; F8 l* n. j: l
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1* g! }/ m/ l1 ~& n6 Q# B3 o

    4 G6 |+ }% q1 D' T7 L9 I& V/ |! w2. **递归条件(Recursive Case)**; w% g, ^0 V% g
       - 将原问题分解为更小的子问题7 k( O0 V: ]$ u, [( N/ p2 K
       - 例如:n! = n × (n-1)!2 ]# v' r4 m8 O6 U7 ]
    7 y3 D6 O- i* T/ ]
    经典示例:计算阶乘
    3 S  ]* J4 h- c& |) H- |* mpython
    " C0 S, @9 l5 S/ G( tdef factorial(n):! S- e& L3 B8 G# e. }, A/ y
        if n == 0:        # 基线条件
    5 m3 |; L6 ?- b+ F: L* h        return 1
    ! L+ `2 M# x/ o5 E    else:             # 递归条件
      q' Y- \$ R( L! @$ M1 H& m: C        return n * factorial(n-1)0 `7 l9 E% p4 p# m# \/ R% Y
    执行过程(以计算 3! 为例):
    7 W( i- P# X6 w2 @" E8 {factorial(3)! p9 c7 p6 t+ d5 o+ @$ B/ [
    3 * factorial(2)( a: Z. C# Y7 m& @
    3 * (2 * factorial(1))  z4 N* _: M% j
    3 * (2 * (1 * factorial(0)))7 t8 O! o/ n0 G% y+ U9 \
    3 * (2 * (1 * 1)) = 6
    # Z5 `0 V5 L' ]& ^1 ]0 m
    & C9 N; a1 Q  A% C# Z8 |6 n* u: r9 E 递归思维要点! u+ A- L1 m, M: A: x/ d3 K
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) T, r; }& x1 @! U3 x; N; }  t2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ A# J! m8 m5 W& z3 w9 B8 N* F8 `$ P, Z3. **递推过程**:不断向下分解问题(递)
    % l3 k! w) r2 S' [4. **回溯过程**:组合子问题结果返回(归)5 Y2 }1 R/ R2 z0 K0 \$ ^

    1 j6 F$ j! d8 M5 [8 T# P8 X 注意事项8 Q. ]0 P1 ^# h2 w+ Z4 M8 e5 |
    必须要有终止条件
    3 `2 W$ a5 r+ ^, o3 c2 d# _) w递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' J( H1 s1 s/ h! k某些问题用递归更直观(如树遍历),但效率可能不如迭代+ w: }7 J- `/ \: S. f8 z3 s
    尾递归优化可以提升效率(但Python不支持)* c4 c. z- y! ?& `$ p

    % `2 L" O" @5 K4 g8 c. F2 {0 S. e 递归 vs 迭代
    9 ?$ U; h- A$ ?|          | 递归                          | 迭代               |
    + ?# ^' o5 w- T+ p5 r4 x% c' f( m|----------|-----------------------------|------------------|
    - b. ^- G: V0 [' O' U2 S| 实现方式    | 函数自调用                        | 循环结构            |
    2 R( ^8 d) f  c| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    2 }+ C) p6 }* i' U, I) _6 D| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |2 K; k7 m5 V4 Y" J3 w$ ^
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" n3 w) r. ?% k; N

    2 S0 M; K2 I; j+ ~  O9 K 经典递归应用场景* B9 d/ Z% J) h, W* N3 m) f# w
    1. 文件系统遍历(目录树结构)
    9 C* z! m! m) O" h* p0 E2. 快速排序/归并排序算法& S9 A0 I5 D, ]: }: R
    3. 汉诺塔问题
    0 U" f, L# M; k  f) d6 G4. 二叉树遍历(前序/中序/后序)
    , C4 o8 P5 R! l0 x/ N( ?# _5. 生成所有可能的组合(回溯算法)( s& M5 x& J& E. |/ O$ u
    " i1 T; e" G/ F' m& G) i
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 06:08
  • 签到天数: 3293 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 _, ?0 Y% s; t4 y
    我推理机的核心算法应该是二叉树遍历的变种。5 C8 ~# |* v. [7 y. s  a- _0 E. V. [( @
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . r2 y3 l# o4 b8 O) k) BKey Idea of Recursion
    8 `4 p! Z( k7 ?8 o; R; \( x* q5 G/ C* G% z9 q. ^% [
    A recursive function solves a problem by:
    - v8 q2 Q6 K% t8 r" y: n$ O2 r: c. r
        Breaking the problem into smaller instances of the same problem.
    " \) @* g4 k/ v5 I; b8 W# J3 }
    " Y" K/ d% s. x; I' Y    Solving the smallest instance directly (base case).
    4 A8 B0 U% h! H; Z4 L" `5 y0 _, Y9 b3 ]( X
        Combining the results of smaller instances to solve the larger problem.% |( M4 n4 l  m8 U

    , Z8 H; L) E9 v( a: u6 e! }$ kComponents of a Recursive Function
    ' m: x& j$ x, m( q9 p& a
    4 P8 b- _" U; r, \, y    Base Case:
    ; n% Z1 E! F  d5 D  _) j" N8 z5 Z7 b) t
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.3 |+ j, ]) p5 `) ^8 C+ F

    2 `  E2 ~/ ^- P5 b+ G6 y# b4 N        It acts as the stopping condition to prevent infinite recursion.
    2 j6 J" ~( a0 f& L9 h! b% h) J8 d' K3 D/ @
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' W- p; g9 w& p; F  h# f: N; y

    2 X$ I" G5 t% R, B    Recursive Case:4 Z, Q- _  ]& h

    1 O2 l* X& k5 [        This is where the function calls itself with a smaller or simpler version of the problem.! O% g, \$ w" {! f* o, I1 R5 m1 T

    & I9 Y1 h% T' w: n! E5 }+ D        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ( L5 G! ^( _6 p6 D: N8 N& P3 {5 M( R; M9 f% i' m
    Example: Factorial Calculation
    ! W) S2 M& h7 z8 B$ q+ _
    7 D: I3 d' v+ n4 S& @, u, nThe 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:
    # v- _$ d  E8 s$ z6 U1 T6 C# J& i: ?# I
        Base case: 0! = 1
    - q" o5 `0 f- ]! M9 z7 I+ @4 a
    ' L+ z" y- D- W) R. Z; g    Recursive case: n! = n * (n-1)!1 W9 ?  [! h9 _- V3 ^8 \5 V' p

    4 r3 r, M0 B1 ~* h3 M- x4 L2 b+ Q7 p" V6 uHere’s how it looks in code (Python):2 v, N5 Q$ B3 G/ F
    python
    , c. L  H+ d! W5 Y" `5 B  j: B, `+ C  ~8 l* e$ I

    1 y6 S9 u  K! t2 ?( Jdef factorial(n):! y# t. a5 k! {) R
        # Base case
    * L& N' \2 M2 O0 g1 r: [    if n == 0:
      m2 F$ x* F0 I/ d& S, b        return 1
    ! }, [2 B) y% o: D3 M    # Recursive case
    " c+ `# X+ i/ ^9 ]9 A7 x. @9 O* O    else:- ?1 b; h, ^* l
            return n * factorial(n - 1)
      r8 D- C+ F# s5 x& [) X; s, f; N# e1 F/ [+ P
    # Example usage
    ; W" m: M- c, y- `# l6 \2 w: iprint(factorial(5))  # Output: 120- L( r+ K0 J1 X4 P$ o

    8 W+ J) s" J0 mHow Recursion Works, S) V: }4 l- L& R" F

    . |! i  c- \% G    The function keeps calling itself with smaller inputs until it reaches the base case.
    # ]; o, N0 `4 R, f# d1 V% q
    & x: m1 m: H- \/ \! w1 E    Once the base case is reached, the function starts returning values back up the call stack.. g( Z( M* J0 e: d2 p7 y3 z
    ! q4 K1 C( n# a/ a( s
        These returned values are combined to produce the final result.
    % z; M% m; A! s
    - @# R. h! j  q' |  z0 u2 {For factorial(5):. Z* l, x  q5 n7 k

    2 W" I1 C. y4 @+ s6 v) n& e
    / w' g6 H3 I6 t/ }. ^4 |factorial(5) = 5 * factorial(4)
    : `7 e8 S& D9 W3 l3 Efactorial(4) = 4 * factorial(3): N% s4 @+ V$ |6 P* h. o4 _5 s( g: [
    factorial(3) = 3 * factorial(2)
    ; X0 @5 E( y) j+ V( dfactorial(2) = 2 * factorial(1)  E/ s( ^4 h5 F- ^, s% f% `
    factorial(1) = 1 * factorial(0); h8 d, w6 a/ O! U. ]  L
    factorial(0) = 1  # Base case. s  c; Q* x* w. f) v, w
      f. b, n. K. x/ X; d! t  [5 O6 a
    Then, the results are combined:9 Q! S$ J2 L9 X  f# j) w0 W

    - y4 O* y4 n/ ?4 [; h! x" h' ]( T2 j
    factorial(1) = 1 * 1 = 1
    + F% ]- _, S# A% C+ V6 W8 ffactorial(2) = 2 * 1 = 2
    . `/ s4 c: r: f1 Efactorial(3) = 3 * 2 = 6
    " S! {" ?) a+ S/ Bfactorial(4) = 4 * 6 = 24
    - G" _& l. u6 S% Mfactorial(5) = 5 * 24 = 120
    - `: [3 n' E3 H9 ~) a; w5 z! z3 s- [% s0 Q
    Advantages of Recursion
    9 X5 S% u) z% _! J6 K# L
    ' L& D1 Z* o# F$ S    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).
      b. A! i, S5 A" g! U. @" ~
    # K$ k, D& C2 U5 E    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 r! i6 N4 E; n6 K
    - i2 ~9 }5 ~  H* r2 L" p$ j6 s+ JDisadvantages of Recursion0 F# x' a; U5 S# {; N

    3 |& ~) T0 E; m/ c2 {    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.
    6 r6 V. O$ H3 @  q9 k4 N9 D& s% o6 H( i8 G) ?9 o/ W" a3 V! ^
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# c! ~1 U! I$ V8 Q7 N6 Y
    8 h5 A2 P7 u; a* U  ~% i7 v7 I
    When to Use Recursion2 [7 d. z$ C" {% A" \2 L1 m

    9 }; [) P$ ?# v" _- u. [& d    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).0 j* ?9 L+ x# v1 I

    ! U( I8 ]) |; ~2 L* f    Problems with a clear base case and recursive case.
    - m: b, g7 t" }. ~$ p; U0 \, ~4 V6 u( o7 p9 _, T: G
    Example: Fibonacci Sequence' b9 J! M) @7 t2 v: C

    * f% V8 G) \6 d5 [" S8 j3 DThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:7 g4 |* S8 ~' |9 }3 ^3 T, V  S; q

    , R: l/ k$ r* C+ }3 q' i+ }8 ~    Base case: fib(0) = 0, fib(1) = 1$ I  Y% S1 i3 l) x3 s% C8 r
    5 R( w$ x) |1 V$ W% ^* r  g$ O$ G  [
        Recursive case: fib(n) = fib(n-1) + fib(n-2)2 [0 n8 ]: u! {& N7 [
    1 L$ ~" P$ S) j3 [
    python
    - F4 Y* R) P* |7 Q; [7 n
    " R4 T. C4 W1 b% h0 \4 n
    9 B  R. [; [1 hdef fibonacci(n):+ K9 o4 H. r% B
        # Base cases
    ( g9 L3 n5 R  }- l! W    if n == 0:: P4 H6 L# }% i' Z
            return 0
    ' [, j( z; ~. C" v$ x2 s7 N    elif n == 1:
    . |( g0 @0 D  m# [        return 1$ R' E2 e; ~8 s: o5 c
        # Recursive case+ B0 o/ p: g# v3 G/ G
        else:6 y& Z# U, D% v/ r
            return fibonacci(n - 1) + fibonacci(n - 2)
    $ x5 c% l" }, ~3 ?1 r4 P
    - T, y) ?0 N( F+ f; O  ?( J# Example usage
    9 D0 A: r+ ]5 C! }* e5 a4 H7 A' Yprint(fibonacci(6))  # Output: 8
    5 D& R; `" E$ t
    * Y8 ~! y; J6 x5 a3 }! J+ y2 wTail Recursion8 }$ |/ {; H- J  S4 B. @
    , O+ R( W& `4 S4 H, p* Y, a
    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).2 X% F; u0 A. K( T9 H! m' w! g

    % r1 \  S) S( N$ M2 YIn 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-7-13 04:27 , Processed in 0.059274 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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