设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 9 S- @) ^8 d0 ~7 l4 X

    " L, C" L* x) b解释的不错
    8 R7 b: O0 C: ~# T  @2 ^' R4 \& O. m
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ k( O' ?4 e: c# |  X% R( \9 y) x) B' E8 P3 Q7 g$ W
    关键要素) ^4 v: W. i' m3 {
    1. **基线条件(Base Case)**
    * ]% C. W: @% p   - 递归终止的条件,防止无限循环- H$ ^* W( S4 S9 ~- N. C3 ~
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% ^, X% E* n! B2 W9 e! h! b

    4 O2 N9 [* j7 {' O+ Q& O2. **递归条件(Recursive Case)**
    ( X+ v2 G( m. D   - 将原问题分解为更小的子问题) ~# ?  c+ u9 a8 ?
       - 例如:n! = n × (n-1)!; }& H% O- Y3 S3 {7 c

    + A3 ]% y& X- @$ f. ~7 ~ 经典示例:计算阶乘
    " T+ h. y  }* u4 U, }( hpython; `4 G9 \4 Y/ V1 K+ ^: `
    def factorial(n):$ |8 e6 e8 T5 O$ O6 D0 j' S
        if n == 0:        # 基线条件
    5 R* R) S$ J& k        return 1
    # O% d1 y  x( N, h5 ]    else:             # 递归条件- ~, p4 f: u2 C1 `
            return n * factorial(n-1)5 M3 \) L! b% s: ]# p
    执行过程(以计算 3! 为例):8 l5 K, C/ D+ W: V
    factorial(3); J9 o8 G; m2 ~0 f
    3 * factorial(2)1 p" ^. J7 v2 Y  I8 S: W
    3 * (2 * factorial(1))1 U3 i8 V' p% D- c# N! v3 }! Q/ P
    3 * (2 * (1 * factorial(0)))
    / T8 S. ]" r% H  e3 * (2 * (1 * 1)) = 62 _# b; g' Y  G8 ]0 I5 V2 C- M
    ' [" H$ t6 f5 t; j* D7 m
    递归思维要点
    1 C1 d* |' {5 W1 T* Y( a& m1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / v4 a8 \  z6 f" J, B2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    2 o( w( ^% G4 m3. **递推过程**:不断向下分解问题(递)
    9 @! K* Y3 B9 ^' u0 d+ ~6 {3 d4. **回溯过程**:组合子问题结果返回(归), F  O; Z7 w/ l: `2 \8 r

    6 J$ c* Q9 O" S 注意事项. l9 s# d" b" G+ y# k2 c
    必须要有终止条件+ w  }7 M7 j7 M6 Z9 T* I9 i8 `, j/ o5 a
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)# e. Q) R) P; f7 P
    某些问题用递归更直观(如树遍历),但效率可能不如迭代2 O$ S2 ]+ H$ [% J! p0 w/ w
    尾递归优化可以提升效率(但Python不支持)
    5 Y+ O: `/ r1 F  u4 u4 B8 O' O: ?3 Y( O% M- i  h
    递归 vs 迭代
    3 B) E7 N5 ?6 @' k0 A|          | 递归                          | 迭代               |1 P! Z8 R' E7 K7 U: t) k% {$ H! `7 ]
    |----------|-----------------------------|------------------|1 Y2 z) a) `; f9 t7 O7 G4 r" t
    | 实现方式    | 函数自调用                        | 循环结构            |
    ; c' x' e. {3 u- C| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 ], m* {$ E7 |+ U7 r, m
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |. e. [. e( v  w6 q- S
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) ?8 P" Z' o; L( N4 I) ~

    1 B) f  |" K8 y 经典递归应用场景
    & H! C( a3 E; T6 b, V1. 文件系统遍历(目录树结构)" k4 N& }1 l' n- s5 {/ b
    2. 快速排序/归并排序算法+ Q: i2 B' x  G
    3. 汉诺塔问题
    4 Z/ a- V. @+ y; k6 k" D$ `/ B8 F/ J4. 二叉树遍历(前序/中序/后序)
    0 w6 j( v( j$ O0 N& v( `3 C5. 生成所有可能的组合(回溯算法). j5 L) N, h" |5 E5 a

    9 H+ g9 I  O7 o! V7 b试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    16 小时前
  • 签到天数: 3285 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,; {" y5 B6 G$ ?5 W2 b" L2 J, u( f; k. z; d
    我推理机的核心算法应该是二叉树遍历的变种。6 V9 O% R0 U* ?- @+ r8 J
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:7 r% z) K: k& v7 h: V% w
    Key Idea of Recursion6 n% v! _; x: w

    7 H& z$ G, u6 S% IA recursive function solves a problem by:$ `& I" e5 T) S7 b
    & H/ }; K: h3 h. ^
        Breaking the problem into smaller instances of the same problem.+ R! ~/ R3 t1 M+ Z, ?6 {& `
    1 m7 E  g; q" T* v
        Solving the smallest instance directly (base case).
    5 S0 s% V" [' L7 \( e. t' [# d' ?7 @/ ?
        Combining the results of smaller instances to solve the larger problem.
    ' J1 k  ]) ]8 o! b
    ) D9 [( i9 T# Q' t- KComponents of a Recursive Function
    % _! T/ J* D0 n$ W$ v8 \8 t
    4 U. m+ ?& a( v: t& I    Base Case:
    4 T& B( k/ M' {# M$ c
    2 |7 h5 P7 x/ K" `9 F$ u! m        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.+ ?0 Z; c" e- [! i" {3 f: u" Y
    7 ^# C0 z# v& R0 a
            It acts as the stopping condition to prevent infinite recursion.6 D' R' O3 i7 i( d. P
    ' f$ a, W, z: C; t) G. w
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.4 Z  S% ]8 i# }" z# ]) ~
    + ]7 R4 E5 p. l) b2 e; p. I6 T
        Recursive Case:
    2 u3 p- D4 `4 g
    / e" t* h. z  ~% J' S' c        This is where the function calls itself with a smaller or simpler version of the problem./ m: V! [5 I; W, ?' G$ R- I

    # X9 b0 s; V: d# D: l$ C$ L        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " }. s! b8 Q/ M. P8 i7 W# z" B) z3 r8 f3 Y9 G+ |
    Example: Factorial Calculation
    ( B: p4 y- G* j6 g9 k4 l$ G, r/ K8 |9 I0 F
    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:
    ' M' t6 r' F, D# F1 T, v6 P, O$ T3 f! p2 ~$ _+ ~
        Base case: 0! = 1
    0 C8 h2 O6 [6 |/ R1 s) p( P0 m4 s: ]/ R3 S2 X- L5 W' n
        Recursive case: n! = n * (n-1)!7 V# x! V  T; K* U: I$ a
      K! G) k2 m4 L0 D! ^; \
    Here’s how it looks in code (Python):
    - T8 b8 z8 t5 S& _/ b1 |1 `7 \python( E8 p2 B* u. t7 D% s# U6 h6 ^9 u

    1 J6 M) A' p: {" j, w2 q% {8 v( _5 K# d0 Q. p/ ?
    def factorial(n):
    * C  s- d' S# l! |  N  h' q: l8 q    # Base case4 l" }3 d, O5 j7 s
        if n == 0:
      |% |' O5 I( |9 S        return 17 z( ]8 H+ H3 Q$ o5 p/ j  e3 q
        # Recursive case3 u( j; ~* _) {& {! L) f- h
        else:
    ( {/ e9 e  H- u1 p+ H4 V1 i( A        return n * factorial(n - 1)
    7 d- |( `5 Q, \* T. k) ?: q4 M
    * F9 t7 l/ d( b# Example usage8 c" B+ d. {% e: G
    print(factorial(5))  # Output: 120# A  K) y! v- T4 b+ v

    ) ^$ w5 E/ \* H. W% M, aHow Recursion Works* D5 ?) ^/ R+ M, V/ v2 j

    1 U2 e+ }  u8 k* @! r# F    The function keeps calling itself with smaller inputs until it reaches the base case.9 v, u& ^- |2 G( f" Q

    & o, t7 K0 _% ]% H" w  ^    Once the base case is reached, the function starts returning values back up the call stack.
    8 b8 q' n6 b* J& F0 X& P8 [% e, e* u; n/ f3 Z5 Z
        These returned values are combined to produce the final result.
    ) e5 {: ~7 D3 }' ?4 f( G* K( t
    6 E6 M2 y% y1 o) ]8 nFor factorial(5):9 A' X9 P' K% V. W6 w0 F( _

    ' r3 M) z; l- M2 f" k; A3 c* Y3 ?- m$ b7 G7 z4 T
    factorial(5) = 5 * factorial(4)
    1 T& f  y1 }1 v1 Sfactorial(4) = 4 * factorial(3)
    0 f* D3 l- E. `  T/ E7 M! jfactorial(3) = 3 * factorial(2)! `* x1 a% m  p: H: G/ c1 Y3 f
    factorial(2) = 2 * factorial(1)
    1 t; ]* |7 {7 r3 Zfactorial(1) = 1 * factorial(0)
    " p4 x) t2 `- m* |factorial(0) = 1  # Base case; V  F$ z; S; o

    1 V' x/ B$ q3 FThen, the results are combined:
    % u9 y3 I: e  K2 c1 B. a2 c8 u/ k/ b* I% Z( V# f
    ; k; J& d5 Z' c- E2 m3 W3 n+ s
    factorial(1) = 1 * 1 = 1/ R' w0 R' k% P4 ?
    factorial(2) = 2 * 1 = 2$ N7 Z, X! ^" l5 L/ m9 c  e3 v
    factorial(3) = 3 * 2 = 6
    . u% g- m  d- a& [6 W+ h# Lfactorial(4) = 4 * 6 = 24
    ' ^' D) }; C; N% Y+ ?factorial(5) = 5 * 24 = 1205 R5 D" _, W/ i& E/ _
    4 g# c2 R" C6 P$ m. h
    Advantages of Recursion
    0 M' \4 O  Y! Z" x! ^$ q1 C0 I! ^: B1 S! {' W5 |
        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 v3 `3 p& I  m$ y# }
    ; G  @9 U  }9 X- r& s  B1 b, s
        Readability: Recursive code can be more readable and concise compared to iterative solutions.) Q: K/ R1 M' F- i) o! e+ ^* Q
    ! Q; c: _; n) m. a: p0 X
    Disadvantages of Recursion
    . k0 h7 M3 W* b% a4 o2 h9 @7 v* u
    . a4 r/ {' f" E( m    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- V2 Z2 @6 [9 K. m6 O! W" k2 F9 A5 ?1 |# \4 v" ?3 J: X
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    3 e9 w# w" }( m; a0 `7 O
    / C9 s' `9 M# |3 T, KWhen to Use Recursion4 U5 Y5 O1 H5 s! u

    " v+ `4 |! Y; a+ \2 O    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 ~7 y, e9 h, d: i" `  L% m9 H
    5 \% K. t' d3 E5 ~9 C% g) f" U7 y7 Q
        Problems with a clear base case and recursive case.% o) {/ z$ A6 K
    6 [3 O/ x0 Q; w* M+ u
    Example: Fibonacci Sequence
    % m$ R6 |6 V5 i* O1 a$ C; B- T9 O% ~7 C3 D! W- }4 c
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ( Z+ E' n. Z/ W: Y
    / K' n9 E5 h! s1 n" }3 c+ e7 t. Y    Base case: fib(0) = 0, fib(1) = 1! b. J+ c% m7 M5 I, |
    / M$ }$ j: N- ~1 d, P4 g& |6 G1 R
        Recursive case: fib(n) = fib(n-1) + fib(n-2)8 h2 F' r6 K7 Y& u" a! X8 ^2 [

    : w( c% f* f9 D7 Wpython
    8 T: B! N  b, X- j$ ?9 a- }* D3 i+ b; S, \% X& _2 _' W
    + s$ ?1 G- w8 w% {/ {6 s+ B
    def fibonacci(n):
    / p5 v& a' Z1 m( c. }6 t8 a) g    # Base cases
    ( L0 S6 j/ s+ s7 g5 L7 l" w    if n == 0:
      N# ^3 F( Q' H& m/ H9 f/ C' d' l6 a+ a        return 0( S0 A/ x3 V( I; i+ v1 Y; K
        elif n == 1:
    , u- E( ]0 o4 H, S; ~! O' G        return 10 Y$ [1 L' R) O) V
        # Recursive case
    0 j; i) _. f: m3 g  I' K    else:  O) M3 L( z' U' j: R; l
            return fibonacci(n - 1) + fibonacci(n - 2)1 X7 T6 I3 Z* q3 q$ U! g8 }
    . ?, a) j& R3 a1 p! ~
    # Example usage' I: u0 y) c; [
    print(fibonacci(6))  # Output: 89 \" T: D8 `4 h% ~% F6 O$ c* X
    % U5 l. F" c; x5 ^2 O: ^9 k# [
    Tail Recursion, x* L. \$ X9 Y/ a' H

    ! i/ C6 n$ J8 uTail 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).
      T  U( U- D: x" c$ d. u4 {- b
    0 F" c  {' U# h, U; a$ ]3 X+ j; TIn 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-1 22:26 , Processed in 0.063089 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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