设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ' F6 q1 t2 K$ O" P2 q' q6 w" N

    1 P) |* S; T  o3 b* n解释的不错
    . U: j3 }3 K. H0 J7 w( d5 d) o/ @; n. e) p' G
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    % x8 ?; b8 D8 H' T2 c2 j8 f  v5 K6 j1 x' S8 L! y, ^9 g) M6 P
    关键要素
    1 L! M4 v' o0 e* k& r  u1. **基线条件(Base Case)**$ Y/ y& d) V/ f+ r3 f! ^7 I/ }3 b
       - 递归终止的条件,防止无限循环
    / |- x1 [! |2 B9 }% s   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    , n; U5 L) p, l8 B
    8 f: e% c9 \$ Y2. **递归条件(Recursive Case)**
    / s1 L. h) B+ N& ^! f   - 将原问题分解为更小的子问题* J4 Q0 D) f+ _, t
       - 例如:n! = n × (n-1)!
    9 @. s4 x0 @, U# Y: ]
      |/ c0 l  N) h1 i/ ]" c 经典示例:计算阶乘# I+ B. z$ s1 Z" v
    python
    $ D6 i7 I9 d  F" K3 U  ?5 rdef factorial(n):
    / q; \8 R, _+ ^5 k; C) E& L    if n == 0:        # 基线条件& q4 K) ~7 o# j
            return 1; c$ Q8 s5 e+ I/ ^) |; o
        else:             # 递归条件# O( z. A. A: H5 c4 E; e+ W  n
            return n * factorial(n-1)- e. C7 S7 h* W- A+ l8 f7 p+ F, u
    执行过程(以计算 3! 为例):
    / y7 [, c9 r: H" w6 l* ifactorial(3)
    % s. [8 S* _3 Q, B$ J2 E; m  L3 * factorial(2)" J; L- X* Y8 l
    3 * (2 * factorial(1))) e- ?, q5 k& p! b, S- B
    3 * (2 * (1 * factorial(0)))% l) u3 g7 _6 O7 A; x+ z% g
    3 * (2 * (1 * 1)) = 6
    + `/ a& z. o/ d% R$ A$ M1 W  O' S( S+ H' [2 G9 G* [* ^% S6 F
    递归思维要点5 R0 T/ ^# K& b3 Z0 b: J
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    * p. L  }* {1 M5 C2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - p0 y( D9 }! \. w- f3. **递推过程**:不断向下分解问题(递)
    6 {" b3 x; B+ c$ M2 ^8 J4. **回溯过程**:组合子问题结果返回(归)8 C3 f" V9 }2 L( o+ g
    + _+ P6 h$ M' V
    注意事项
    6 L1 m  C1 r4 k- c% @必须要有终止条件; m1 o( j$ Y0 w1 A7 j
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)! }. {' Z+ }  ?6 r4 C8 i5 s
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    # z/ W+ l/ j: F/ y3 C尾递归优化可以提升效率(但Python不支持); S) R% Y, ^# j5 d
    7 W2 _! v$ o# e
    递归 vs 迭代0 ~) T( B! e: ~3 `" Z% X
    |          | 递归                          | 迭代               |
    ! i( M: R- |3 a% x. d2 T! C|----------|-----------------------------|------------------|
    . ^5 F. v. C2 E, S5 r; c! T| 实现方式    | 函数自调用                        | 循环结构            |
    * j7 x" C, Y$ u, E| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / I. ^3 L3 y7 ^+ k( W1 M| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    : H& \% ^. |* u! R/ {| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    & d( c7 k/ B# F6 J# R4 t- @7 _* e  w; ~: [6 u- Y
    经典递归应用场景
    7 c7 ^+ _6 W% t& l1. 文件系统遍历(目录树结构)8 Y3 D& h/ H. B1 W
    2. 快速排序/归并排序算法
    ; e) a$ l0 c/ c2 J' k5 d7 P3. 汉诺塔问题
    - f1 {2 ~5 p- Y  [- O4. 二叉树遍历(前序/中序/后序), R' [, h, e. R; M* l2 M; z$ D4 C' g
    5. 生成所有可能的组合(回溯算法)
    1 }( X6 Q, |, R1 o8 N# [0 t
    2 C, H# t8 _' Z+ J2 o4 N试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    16 小时前
  • 签到天数: 3145 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ F  f0 `1 [0 \, p  i
    我推理机的核心算法应该是二叉树遍历的变种。7 F: r% a6 B, _! m
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:' d1 ?/ \* T2 n
    Key Idea of Recursion
    " N9 L  @, L9 \
    2 ^0 I1 B: {1 E! s5 n9 gA recursive function solves a problem by:
    ( h" W1 W2 q7 w$ x! C/ O6 y* V- _$ F
        Breaking the problem into smaller instances of the same problem.
    ; e3 U3 B: \, t% p  V% H7 z9 X" {, I, x5 i
        Solving the smallest instance directly (base case).
    3 W1 \6 B0 [, w. R- m
    ) `% P- d0 C" G    Combining the results of smaller instances to solve the larger problem.. {. U8 f; H4 c  {: s0 Y" j
    6 b, {6 n- L  E5 r7 A, n
    Components of a Recursive Function
    0 v6 s0 F2 Y' w( [) |0 f. f3 E0 n2 j( |6 N2 v+ O
        Base Case:8 k  S# Z' D. `' Q6 _

    ; q6 E! b2 l& b3 q        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 f0 z( q) A( X# A* ?; h+ u0 H
    " Q9 v4 W2 Q0 S9 q1 x4 T( k5 ^        It acts as the stopping condition to prevent infinite recursion." [9 @6 b, @* i5 D" `/ [
    $ N( {' q" K" V$ B8 L
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.0 B3 a8 y4 z  O% ~

    0 ^5 Q* @9 |( ^# {    Recursive Case:5 ^. T% a9 ^8 h" W6 F$ D

    ; A; M9 q; A! E7 W. N! i: _! Y        This is where the function calls itself with a smaller or simpler version of the problem.( S1 `0 G1 x! Y7 [* U
    4 J3 ^) D1 E+ I7 N
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 t8 f  l1 a& J) L4 ^
    & U/ d0 \6 A/ J1 A9 M
    Example: Factorial Calculation
    ! t) \5 {9 R, n/ i! i- R, u7 c: R5 i# K/ \3 z
    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:
    6 h, M' V! h8 c8 e4 M
    3 x( ~4 W5 _3 Y" {( X    Base case: 0! = 1
    $ z4 [% q6 a, S1 n$ f
    # M  {$ {5 F% w- i, \8 @" t* {    Recursive case: n! = n * (n-1)!
    9 h' H/ b  m' H, @# s" w! x6 c) o7 c( o% c. [1 X6 J+ ?
    Here’s how it looks in code (Python):
    $ A4 o/ J2 C1 s/ J' cpython
    7 Z" o+ F9 s/ X0 N8 O# ^
    8 m5 O! w: Q; y8 U  Y
    ( X# j; P" v' x/ e, @def factorial(n):
    3 ]& s! }5 T3 h* \. G    # Base case. ^7 V4 Q: M4 F0 w0 X
        if n == 0:% d3 z& q2 w/ d* @
            return 1( h8 a: R  R* A0 M  t
        # Recursive case/ L; @+ p" x+ h4 z$ I' {
        else:
    8 R/ x# ]9 u4 H        return n * factorial(n - 1)9 f) m  `# X3 n5 U' j
    7 l) N8 o, a& a3 Y+ S9 o+ Z  J
    # Example usage
    7 s/ K! x# h" {print(factorial(5))  # Output: 120& w8 i3 X- {* P& Z1 U7 ?

    , b; a5 [2 Q7 r' K. x4 jHow Recursion Works" t8 P8 h, c2 x7 U& N% T

    8 b+ N% B! J  E    The function keeps calling itself with smaller inputs until it reaches the base case.
    & H  d' f$ T: I% C4 w$ w' F9 g, ~" J# R3 L
        Once the base case is reached, the function starts returning values back up the call stack.6 D  y3 d' P1 L% z* b
    7 \/ d" H: @9 k; Y
        These returned values are combined to produce the final result.! h) ~) @5 Q4 L% I, Y. w* z  N; i4 g$ Y
    0 T) z! p+ C; a# X. |2 @2 d
    For factorial(5):
    , Y7 g* }8 O; q+ B5 s( Z* v. X2 F2 q2 q# d" q
    8 W' v; {9 n5 W6 ^
    factorial(5) = 5 * factorial(4)
    . N$ e- E( c! P. I# efactorial(4) = 4 * factorial(3)
    . W7 M. s* `; L9 lfactorial(3) = 3 * factorial(2)
    8 n" l7 p9 _$ M/ E, Vfactorial(2) = 2 * factorial(1)$ @( A! s+ H6 a# ~& T9 ?
    factorial(1) = 1 * factorial(0)! ~0 W" `7 a% {; A9 g
    factorial(0) = 1  # Base case( b* S' f9 w- Q& ?/ U

    5 O/ ]! s! `8 LThen, the results are combined:
    * {5 u7 [1 d) s" a( n' F6 _/ f4 {) Z
    $ h" s1 q& X  H/ U/ x4 Z
    factorial(1) = 1 * 1 = 1* D1 C% N5 j& M& p0 z7 O" x
    factorial(2) = 2 * 1 = 2
    5 q8 j7 e$ ^' }/ Lfactorial(3) = 3 * 2 = 68 U. M! h& m" x/ J5 y  {; N
    factorial(4) = 4 * 6 = 24
    / N/ g* e7 T& ~+ g' Bfactorial(5) = 5 * 24 = 120
    - \1 e, f) w5 t. {- U1 k; H
    ; `! p9 }; H  f2 \Advantages of Recursion: |( v# Y1 K" e- d+ ]8 @

    1 G$ R0 r; x0 k    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).
    9 h4 j% n8 p9 F# y/ V0 D
    ! U2 ^5 ~1 X  I" [# t' d' B; y" L0 P    Readability: Recursive code can be more readable and concise compared to iterative solutions.% V' l- F' L; l4 O& e9 A' q
    % l. N) G  y  h) c( @; L
    Disadvantages of Recursion# H) o9 E4 b- W* b, A

    " I/ E" F& C& @+ V    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.5 u, @" r8 t% B4 i4 g; _

    3 J4 m8 q$ y8 v* y; S    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 a9 ^9 h, R" l2 f
    7 P: o- K8 j( l, W# fWhen to Use Recursion
    2 k% J& F5 |  `/ c& T8 s5 ~+ T( E" |9 X" Y9 Q9 |8 x
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + h# ^1 Y2 c" ~# E' E6 e4 g" V) A
    4 @& F$ X& A. S: N    Problems with a clear base case and recursive case.
    8 @8 z0 s0 F  p  ~$ E
    ' w( \- S& Z7 D$ pExample: Fibonacci Sequence
    ' R7 l/ G" m' K6 W* k! |: ?
    $ p# Q5 N- ?" x6 U* [$ R5 E+ YThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    + R( I5 M4 q) {- s+ i* L! Y
    1 k8 u2 ^1 x6 T' w0 F# a9 s    Base case: fib(0) = 0, fib(1) = 1# v) N6 m' @4 l; \( n$ T: h

    . g5 B! k& s" A; H1 i# l  a" ?6 O    Recursive case: fib(n) = fib(n-1) + fib(n-2)2 {) v6 M# X0 \0 I7 m

    6 c9 ?/ ?( q, Ipython
    0 M6 L# P$ d5 b
    9 Q; h% U/ d3 r' r# ~; j7 k& r% X7 ~- d9 _! \. `8 n
    def fibonacci(n):! P- a9 @  g  G5 {' k& S; w- ]* V
        # Base cases
    / b% h2 F, ^% `8 W- C8 `: {. H    if n == 0:
    , K% V; K( P1 a& G8 J0 e! G        return 0
    % s9 t" p0 a* W    elif n == 1:
    # v3 V1 _8 ^) B( L" K( a( m- S7 G        return 1
    ! k4 q/ s8 ]# F; C' R  r/ j2 o    # Recursive case$ |& s" m/ x( a) W' i; T
        else:
    # A9 u; F7 I& ?& r  j8 Y7 G% ?' E  e        return fibonacci(n - 1) + fibonacci(n - 2)
    + l5 K& U9 n0 r3 D2 U; Z0 a! X
    ( m5 S) T1 f: C# G1 I, D0 Z) d. w# Example usage0 T- Q6 N; |. p* h  R5 x
    print(fibonacci(6))  # Output: 8  f& M( K  X- t  w& H  w% z1 ]  l
    * J7 ^1 C% m) J5 T- n( u" o
    Tail Recursion- l6 M8 L% }6 a1 j& D; U

    9 q0 p7 S1 I2 \( s" sTail 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).5 _# U9 a/ f  \/ R. W. H$ @
    - H: l5 l. M) ^9 ~9 h/ S# @! k7 X! 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, 2026-1-13 23:27 , Processed in 0.035660 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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