设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    . g# J8 `9 E" s- T8 |  k) a2 c, r) J( {2 {6 ]2 p) N& ^
    解释的不错
    2 |& s( |- V; `, a
    / s5 v; v+ [' S, S! R6 J递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, |& {' c- l9 K3 o! G, M9 M

    . g8 n2 H$ C' a# ~1 b 关键要素
    % X0 j6 s: L2 e7 J; f  o1. **基线条件(Base Case)**6 `6 n" w% u; `) T
       - 递归终止的条件,防止无限循环. f1 D" F. H$ G8 {2 m
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 c& b( b) f1 n- C
    " q* {* p: `& G* S  m: K
    2. **递归条件(Recursive Case)**
    . i5 F# A9 x+ X' }" [" k   - 将原问题分解为更小的子问题
    / ~6 p% ^* C$ Y& {5 r4 ^   - 例如:n! = n × (n-1)!. `0 F0 \. D% S% ]+ j5 Q
    1 ~: r) c7 r: @/ |! h' _1 ?
    经典示例:计算阶乘* r4 N1 h5 y" N! l" r1 Y. T! x
    python8 N6 F2 U3 B" @4 l. J
    def factorial(n):
    4 h1 P6 ~. `5 e! E% w/ K    if n == 0:        # 基线条件2 y% U2 [1 o# s4 c6 \) |3 Y
            return 1: v! I5 L$ @" f' E: a& ]
        else:             # 递归条件
    ' O( z7 K3 o1 q  h2 }        return n * factorial(n-1)
    ' r, H1 C& t9 T( S6 z执行过程(以计算 3! 为例):
    4 d- d6 p# Z3 _* |factorial(3)/ [8 N& {. A* B! P5 U% f5 ]- l
    3 * factorial(2)4 [  A6 S3 x4 }* D, `: P" r; {+ n
    3 * (2 * factorial(1))
    / o- B, n: Q# m8 d" L3 * (2 * (1 * factorial(0)))# ^' \: ?0 F: X7 P# }4 G
    3 * (2 * (1 * 1)) = 6
    " J! a: x$ h; q/ U( `. o) G# V% J- Z2 o3 f$ z0 `! ?3 Y
    递归思维要点2 Z$ t( I2 s  s9 u2 W
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : t3 ^/ D# j% n$ L5 O  j3 I" J2. **栈结构**:每次调用都会创建新的栈帧(内存空间)2 D" W8 K1 d. D  @
    3. **递推过程**:不断向下分解问题(递)
    " ]  M/ s6 t2 b/ C0 w2 `4. **回溯过程**:组合子问题结果返回(归)
    8 F1 D8 K# K; S6 h
    7 ~$ N: V6 |: K! |: U7 i; j% } 注意事项
    9 J+ k% f. I7 e: D) U8 q必须要有终止条件
    ' [/ I, t, J+ L# @& a$ J递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 r& L. P1 d( y& g6 ?某些问题用递归更直观(如树遍历),但效率可能不如迭代6 |/ v2 C" t- \4 i
    尾递归优化可以提升效率(但Python不支持)
    ) z: X/ y% n8 u7 J* P: N( o% }" c/ z7 w. r% y2 Y2 m7 ]# E7 _$ l
    递归 vs 迭代
    ( E7 H0 o1 t' A* G3 Z# P|          | 递归                          | 迭代               |
    & e( d# r; ?3 ?- p  m|----------|-----------------------------|------------------|
    0 E' }/ |& X, K0 l/ g4 H| 实现方式    | 函数自调用                        | 循环结构            |4 p$ d6 l2 b* w* b% M9 T; o
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 |+ |3 [7 Q4 N: n) o6 r
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 h/ h$ Y, m0 m( m
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% `. o! `  w% f0 `
    ) S& s. t4 m) V# W5 X" O! L% ?# |
    经典递归应用场景& e8 R% f% [9 U) s* f5 I$ T" t0 ]
    1. 文件系统遍历(目录树结构)
    - ~9 |+ `! q  m( A: r* f) m2. 快速排序/归并排序算法
    + G8 j4 H. b" [+ s3. 汉诺塔问题
    0 U# O2 F# f' G" i' \4. 二叉树遍历(前序/中序/后序)
    + k1 |* S5 Z8 V+ q/ e5. 生成所有可能的组合(回溯算法)7 n$ s' q' {3 ~( J

    ) q9 X7 j# l0 z+ `1 v试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 20:11
  • 签到天数: 3220 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,8 `% Z/ r; A3 Q/ M6 ~' S0 z# _
    我推理机的核心算法应该是二叉树遍历的变种。
    2 B9 H+ |, C2 H( s& C+ J1 R另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    8 i/ j9 }" c) ?- SKey Idea of Recursion
    " H# \# W% k* e5 }  l. Z5 H7 d5 C/ P& t. Q( _- z
    A recursive function solves a problem by:
    0 \& x& x  \3 H* c4 e# Q; G: \8 P! A* Z* d: H0 u+ n; i
        Breaking the problem into smaller instances of the same problem.1 n" R, h6 T, Z+ X) G0 z

    0 T; a$ b" W/ }% y    Solving the smallest instance directly (base case).
    * {1 \( _* F+ i" c7 F) N% u) z# |1 Q0 }2 D& Y- H
        Combining the results of smaller instances to solve the larger problem.' ]) ]# w) h9 c9 `( E, x8 \4 Z
    ) Y) L2 o1 Z' g7 z* \4 [6 F
    Components of a Recursive Function
    * ?( g9 t3 E8 R0 u
    ! e: i% d2 [8 U9 ^1 U% N    Base Case:
    5 k8 Z- `! q9 s7 W2 D+ r
    $ X. ^* ], r: x- _6 Z5 |8 B3 u        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 P8 Y$ z2 a8 x+ A2 b% O" s# k* q
    8 P% n$ }7 C+ J        It acts as the stopping condition to prevent infinite recursion.
    # j( X; f6 r6 H0 o2 U
    6 i% \5 H6 ]& k& o        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    , F0 f, |- [" Q" `9 ~1 Q2 k% E. z( y- c' t8 n
        Recursive Case:# Q0 Q( _! x- J
    ) e( E4 y9 B# P" h1 w& y
            This is where the function calls itself with a smaller or simpler version of the problem.
    4 r- x8 R/ ^+ Z
    * ]5 q. q' e& ^5 `& x. h0 Z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    / E; V' s! U9 o4 Y! j
    9 f' w' N& k5 A4 vExample: Factorial Calculation
    , [3 X5 ]  u5 P2 E8 ?; G% y2 s
    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:
    0 O# b9 h  H  |1 [. b9 v7 x0 r: e) [1 E2 P: k+ ^0 E
        Base case: 0! = 1& Q7 c" Y$ \) H0 W. X3 Y5 a  z
    8 J! U/ |; {0 s# y1 Q% p7 N+ C1 E
        Recursive case: n! = n * (n-1)!
    6 ^$ s/ d+ r/ Q& O% V8 Y! m3 c& {7 q$ |4 P  M
    Here’s how it looks in code (Python):- ^6 L# B  E# _$ `$ Y# a
    python0 a1 @4 C5 _  x1 C8 l+ R1 b

    + @; J/ q9 Z+ h$ f: S8 T# u9 {; o& S! F
    def factorial(n):
    7 w; G& S0 F! [+ @' F8 O/ P    # Base case
    ( W5 ?& u- ^1 J! r' `    if n == 0:
    8 h" o# e; _5 J9 A$ Q4 B2 I        return 1! P& S5 J7 v4 Y6 r6 E% m- s; H
        # Recursive case- c8 f; Q7 Q& P$ V9 t+ S  j
        else:
    6 p/ q7 ~! j9 l' y+ n# H6 Q; @; `) Q0 T        return n * factorial(n - 1)
    : M" u$ l8 X; b2 J* ]7 y" |
      o# L) X: ?* G! F% k( i# Example usage
    & G- a. v) G/ Y" vprint(factorial(5))  # Output: 120$ ?" a/ n, s7 O  l9 S) ]/ K

    ; F" A6 q8 t4 j  I" gHow Recursion Works  C4 k+ t4 r( T0 y( j% V9 ~. K( ~9 {" o
    ) [  T$ r2 j2 \5 O: Q/ v1 N3 L) N
        The function keeps calling itself with smaller inputs until it reaches the base case.
    9 V8 C1 ]# I/ |4 r: Q& R( m9 j: I( T! I# i
        Once the base case is reached, the function starts returning values back up the call stack.
    " k, W" x# z5 j" ]7 t. A5 _. x  y) h- w) a5 ?, q: \5 o
        These returned values are combined to produce the final result.
    ! X) ]* F$ `! ]% h# f8 Z& {' U
      X8 d( t. n' @+ L7 \. S# m( gFor factorial(5):* S+ r) l+ P4 Q% P  J  ?9 D- X
    4 j4 _7 a! A' ^0 m2 F/ [

    8 e' Q. t/ T# y1 d6 M4 T2 \+ Hfactorial(5) = 5 * factorial(4)4 s' f/ |6 M' g% C& S, R
    factorial(4) = 4 * factorial(3)2 N& ?% g3 a3 i0 T/ Y. x
    factorial(3) = 3 * factorial(2)* k" f. @5 m: _% w* `( J7 a
    factorial(2) = 2 * factorial(1)" Z$ e: X( w3 h& }4 C7 E
    factorial(1) = 1 * factorial(0)) @, K: o4 |, _' i
    factorial(0) = 1  # Base case: h' j6 U; Y: Y% W8 }# b

    7 Q3 }& J1 D# JThen, the results are combined:
    9 q8 H: Q( W0 g* U2 Z9 N) Q& m( A0 S8 X" \# v8 B- ]" ]
    + E% i& K; J7 R! q4 _& b5 z
    factorial(1) = 1 * 1 = 1, `6 G' i# }# S
    factorial(2) = 2 * 1 = 28 g! }3 p. }, e. F7 l
    factorial(3) = 3 * 2 = 6* a/ @8 m1 o, u0 u7 [
    factorial(4) = 4 * 6 = 24: }$ H3 g, m1 A( N
    factorial(5) = 5 * 24 = 1202 }! {( [$ [. L) ~& X1 m; x
    ( Y, [1 B: r' [$ O; c6 ]; w5 c
    Advantages of Recursion2 p% Y- [- M4 Z9 {/ }
    5 }$ D) v- T2 q/ X4 x. t! I* r) ?) q
        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).
    - n, t" h( k! X) P9 f7 }2 z( J
    0 T. D% D; d! t* Y* n    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ! P( q. ?9 s* i8 e1 W7 v1 @( P, @8 X' b+ a2 u, t
    Disadvantages of Recursion
    # ?3 P& v( V# {5 l$ n
    $ D* U: V/ O9 Y' t  X7 ^- `5 _" i$ _    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.
    1 w( \/ n: x; l: K; m/ a9 `+ |" h+ ~% T
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).' T/ ?2 E8 ], ~) b' X. E
    8 |3 m8 Y6 O6 Z! ]% N
    When to Use Recursion3 `% Y* f4 `8 [
    8 p: h) h9 ]( ?7 j! {1 Q3 O5 Z" s
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # K0 f, `1 n* n: f9 N# c% n! d6 y! A) w% w/ W' X) L
        Problems with a clear base case and recursive case.
    & z5 T& |8 T5 n
    7 \8 p. p) q& E, E- T! WExample: Fibonacci Sequence, \4 l* O+ s2 D7 K$ V' T9 U

    * p7 _4 p+ |' [- B+ MThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 n( r4 l6 I. t9 T" f4 E9 p% ?. [1 R2 P" Q$ j
        Base case: fib(0) = 0, fib(1) = 1. \- d) R" |6 ^5 G% ~) v
    / d: q" l+ a2 d' {
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' f% X3 [$ w! |, M" L) \1 x, x/ V. T8 u
    python
    ! N4 N) ~: I3 b8 Y& Q( X' \, |/ Q; F" l3 `# ~, a

      H' r9 u8 X% ~1 [, _def fibonacci(n):
    + Z4 v( `' p& p; i* ~    # Base cases
    7 b8 _  o  u; O1 k    if n == 0:1 D: _4 k' s  ~2 n  i
            return 0
    4 V4 o# @* ?9 @: |    elif n == 1:( [, B  U: d, a) C' _! f0 W! O) I! S
            return 19 S( ?* J/ W" [5 H0 {
        # Recursive case
    ! |' g: G& J) E  ^6 V' Y    else:
    ( s# w6 s- l( V! ~8 K& r        return fibonacci(n - 1) + fibonacci(n - 2)
    : ^- Z; e% q0 G8 ^3 Z2 {6 E2 b- b' h2 e- c1 Q8 H4 [3 o& M, O" w
    # Example usage" u, N9 g' s; R; T5 G
    print(fibonacci(6))  # Output: 8
    ' Z; y- R/ z" `' T) V
    3 P7 r/ M  W( _! P7 w2 R5 DTail Recursion/ J1 O( ~4 ~# c9 `0 ?2 |$ C
    7 R+ D5 r' ^  h6 l# _% E
    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).: O8 E5 m; F% {/ [0 v9 \2 c) s1 g
      R& D5 V9 o' o3 ?
    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-4-22 01:27 , Processed in 0.058121 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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