设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! U  X0 D- w  j, g( S- a
    ! F" C1 Z: n, D/ h. ]; Q. E解释的不错
    3 V+ H* }& G% y4 |/ `( U6 F7 F4 q- I+ Q0 z( l
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& d) I" o) D& A
    & K" x( F. i9 s
    关键要素3 i0 r- n1 k8 ^4 I
    1. **基线条件(Base Case)**' X3 m; z% M& M' \4 p3 S
       - 递归终止的条件,防止无限循环6 m( V( o- U8 @3 Q' G+ Y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " X6 n: Z+ k3 n$ A+ S5 v8 F7 S4 V" O
    . f' Q+ e. b4 g9 t, @( ~2. **递归条件(Recursive Case)**
    , v4 W1 w3 b* a% ^! V. N: B   - 将原问题分解为更小的子问题
    ' w0 e7 ^! i& N+ D   - 例如:n! = n × (n-1)!
    0 z; @: y1 o; z5 C+ c& z& j& h& ^9 p' V; }2 j
    经典示例:计算阶乘
    ( ]! d7 A. {9 b( Apython
    : w+ ^9 R0 f' b. I. Odef factorial(n):6 D2 U  q% H/ F: u: }4 w
        if n == 0:        # 基线条件1 K# N. n( A& q; @0 ~, r5 H
            return 1
    8 K) S# X. t2 [1 |: K: I    else:             # 递归条件8 |: Z+ W  ?5 t, L3 I
            return n * factorial(n-1)
    % r! j# @% j2 u: G$ D执行过程(以计算 3! 为例):
    ! l7 ?( L, G3 @8 Y8 N  p/ N1 ^# Afactorial(3)
    ) n0 F% F! x$ x8 Z: W4 s% E3 * factorial(2)
    ) ?  f: @* d" @: P5 S* _3 * (2 * factorial(1))" Y7 @: h& F* p+ I  [( e
    3 * (2 * (1 * factorial(0)))9 u$ m+ F& L. r8 j& ~7 ~3 E
    3 * (2 * (1 * 1)) = 6
    * l& Q# F& S! \" I& P% V
    0 U4 n) p; U) o9 ?/ k6 k  g5 t2 O 递归思维要点- c0 L- B5 x7 q7 W* g
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : a. ~3 S+ Y5 `' r1 y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)3 t: y$ Q7 d" _
    3. **递推过程**:不断向下分解问题(递)
    " K+ ^% R. o1 Q5 h1 Y+ \. `. G9 s4. **回溯过程**:组合子问题结果返回(归)
    ) R4 ~1 C2 U$ L8 S+ z) v8 q1 B% E
    ( I0 M7 I8 H1 R. G; V 注意事项! ^; P0 |# N3 M: J, ^
    必须要有终止条件: R% b. d+ E9 v4 F& F/ x. @
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ V2 H* r  i: }! S/ N- c1 z
    某些问题用递归更直观(如树遍历),但效率可能不如迭代) Z) n4 I( c: N( B7 H. f, H9 Y
    尾递归优化可以提升效率(但Python不支持)
    . T7 Z2 A0 l+ a, ~4 J2 J4 ?8 W4 U5 K! P) |) @# z. L
    递归 vs 迭代
    & S4 J% ]' z) ~/ ]/ j" ]|          | 递归                          | 迭代               |9 v. l% ]0 s& o; u
    |----------|-----------------------------|------------------|
    3 N) W0 g' L1 y* H2 S4 {( L( || 实现方式    | 函数自调用                        | 循环结构            |4 t' J9 ^& p" x" H
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    + h. t9 @' g' \0 X8 j( w: Y" w; R| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    9 I' q0 S( t; g| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % d! S2 Q# H5 z+ w1 j9 U
    : }1 x  J. r8 Y4 ]0 } 经典递归应用场景
    # C0 X4 r: b" J: r" B1. 文件系统遍历(目录树结构)1 ]* t$ X% W) ?+ ?5 |& G
    2. 快速排序/归并排序算法
    ; @% M/ ?& f0 u! w* n3. 汉诺塔问题
    ) r, h6 R  y4 p% _, c. {4. 二叉树遍历(前序/中序/后序). G8 s; h. y4 o1 @) \
    5. 生成所有可能的组合(回溯算法)
    # `+ c$ a1 P' R% G9 ?0 C
    - D# D, C* T7 J  d# _* B& c试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    10 小时前
  • 签到天数: 3105 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    . Y3 l% n, }; [% J我推理机的核心算法应该是二叉树遍历的变种。
    1 a* I0 W! u/ j5 t' G, B% _. t另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    6 n8 H, e  J# `/ T* P4 A% X; D/ {9 ?Key Idea of Recursion$ D# c- ?9 E1 u

    7 H( e- j. n" c" K; d: o- oA recursive function solves a problem by:  Y; R1 H2 m3 w- v
    " U6 e& @2 L0 W& p# \* k' i
        Breaking the problem into smaller instances of the same problem.
    : ?- {( g2 b" U; C2 ?3 L- `0 ~3 B. z4 X
        Solving the smallest instance directly (base case).* l- d+ `* Y# {# r
    $ x3 ?& T/ o9 g' c$ P! `
        Combining the results of smaller instances to solve the larger problem.
    4 k  W2 M! l: Z- \. p. `
    ( a0 [. x6 n) L) z% kComponents of a Recursive Function
    , e5 J2 p& J0 U2 a4 S
    ) L5 W4 _+ X: @: q6 n    Base Case:! Z9 D8 X8 M" O6 m
    ' d- v: r  H! s& n  c9 q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    1 k  S+ U0 H5 V0 d# g" h! T4 U" A! o) y2 x* s5 \. e) i
            It acts as the stopping condition to prevent infinite recursion.1 B6 v9 q; [( w+ b1 o4 P; g6 V4 Y
      m8 }( q. j9 r
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( P. ]- t0 Z, h7 i" A9 r/ k' J

    : Y" U: P) v) C! z    Recursive Case:" J: ~1 U. O2 A$ y
    ) v2 d, k+ r( L2 D6 d
            This is where the function calls itself with a smaller or simpler version of the problem.
    7 A& k8 }3 _. z( B, o
    & C2 e$ ?" x& ~" ?+ V* \        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 B+ P- n. W  z+ Q, E
    4 b% r- [$ J( q7 W0 V, \5 ~3 ~- J+ }* e
    Example: Factorial Calculation
    0 B% l) e6 g/ X$ I7 s. V4 w5 \! H& s) A( i$ A; S3 f. o
    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 O* m/ q4 D& e6 W; {5 T

    0 f3 R% A2 A1 ]6 W# E( b/ Q    Base case: 0! = 1
    0 j* @/ V% B: [* X
    / r4 F# b7 r' j+ y" l& `    Recursive case: n! = n * (n-1)!
      b) Y$ i3 c4 F: C6 {: b3 T, \, l  m5 M
    Here’s how it looks in code (Python):
    0 v# m2 U9 j6 a4 U) W; m( i4 spython. B* ]; P& h( ^: u
    . t8 Z3 y  {4 X8 _
    2 P6 A+ \/ |$ e# a5 N7 r( f
    def factorial(n):3 l& ?0 c, Q  P1 d
        # Base case( s7 u! M7 k% o# w  `. D
        if n == 0:
    6 D  T5 H, R* ]/ z! }. }- A        return 1
    , `! E1 N( N( f* ~    # Recursive case  W. v" k. i7 s1 Z9 g
        else:
    0 S* Z+ {/ Y1 s# p* p        return n * factorial(n - 1)) Y+ }) d  h" p2 @0 Q, n
    9 d+ K* g9 z8 @7 n* E# U
    # Example usage$ |& P' {" p, {( X* t0 R
    print(factorial(5))  # Output: 120
    ) z! I! v  G* _- D2 y" H2 e( y
    ' J4 t  u$ o, V8 o& [5 Q3 y0 gHow Recursion Works
    4 A' \' c: C0 U9 x  b0 F7 M0 F( K. t. `1 y% X, t$ ^) ~* R
        The function keeps calling itself with smaller inputs until it reaches the base case.( y6 C  y4 F  H8 H9 M! {
    7 g/ Z, Z  `0 p1 v# w' \4 i
        Once the base case is reached, the function starts returning values back up the call stack.8 _( n0 M, J+ ?* m8 f. H0 g0 U
    ' f+ r! T  g" U" r4 O( N: ]' ^
        These returned values are combined to produce the final result.% p5 n# M. k0 Z& L$ I
    ) t, B. B: [3 `, z: F
    For factorial(5):# G" V# ]4 o& Q1 j% B2 {
    3 m+ T+ |$ S: x
    * [( B! ]# L$ \/ j% Q9 y
    factorial(5) = 5 * factorial(4). ]% a  d  R7 q3 n) d) h. N
    factorial(4) = 4 * factorial(3), H" c* B1 N+ ^; B5 ?8 a/ A
    factorial(3) = 3 * factorial(2)
    ' @4 V, G0 j1 V# L$ i  bfactorial(2) = 2 * factorial(1)
    1 E, ~3 j+ T+ O  }4 O3 I- d5 k2 @% ]factorial(1) = 1 * factorial(0)& }7 g" X6 I5 U) G
    factorial(0) = 1  # Base case
    9 Y- Q) F6 I  r( D0 S: b* ~: l* i
    Then, the results are combined:8 o, l9 Z% e! m5 x- @, K

    ( I% M8 s/ l9 y
    ( [; |4 `2 T' J: t5 ifactorial(1) = 1 * 1 = 1/ Y5 Q# ^9 t( o- C$ q2 C6 n1 I, |  k
    factorial(2) = 2 * 1 = 2
    ( A1 b: I" z8 k: b2 B/ Bfactorial(3) = 3 * 2 = 6
    $ v1 \6 N& A, ?, S/ @factorial(4) = 4 * 6 = 247 H. d$ i, f. Z3 X
    factorial(5) = 5 * 24 = 120- K8 f& v9 ^5 h4 X9 ?0 H- i: [
    . I+ e+ b( R1 B4 N/ ~5 @- ~2 q
    Advantages of Recursion
    & N& Z) k" O0 }% ]! q! E2 {; Q! b( |" s. _7 q" ?3 z
        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)., k  z# K0 _! W

    + g1 a, S1 m  @" V9 `% K% t    Readability: Recursive code can be more readable and concise compared to iterative solutions.! q) d4 p* a; Y
    : }( K9 ^( c2 a; P
    Disadvantages of Recursion
    6 Y# n5 W; a6 q, S2 W# ~
    * F* I; s" B9 n. V9 {    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.
    . F3 i7 C1 w, w$ Z; E1 g8 u- J7 Z5 c, t& j  Q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).4 _. |; b" M" `( [0 d

    ) O8 S6 Y: G4 ~: T, ?When to Use Recursion
    7 p! O& B0 u2 f. N, e  m4 e0 H% _# t# c4 f2 D
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    7 x! I" V' B5 i, i) t; b# _" d' b# v( B; ~
        Problems with a clear base case and recursive case.4 M" ?8 H# P+ Z' C  ~

    & _% m+ B$ |6 {+ m0 J3 o4 K  sExample: Fibonacci Sequence
    6 h5 H- J7 Z9 ^5 I, f0 ]& U, b' t1 q/ Q6 Q1 j
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ ?) f9 p) ]. R

      `* F/ [) o! {9 h  d9 c6 A) t4 D    Base case: fib(0) = 0, fib(1) = 1
    9 e" J, b& W5 f" W+ q, p9 _0 Z9 H, t: B- ^
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . o7 h' l/ I+ g$ @" Z1 e$ ~- h
    & j  u7 i# H4 V/ }python
    . L, L$ W0 d* `3 W7 F- f' G8 X3 R! k) {, S& C1 k' N% j1 b' V# e% _
    9 b! N- u. s6 w' w3 I& Q
    def fibonacci(n):. T3 a: j/ N1 s# E3 a/ X
        # Base cases
    8 u) {7 g0 N& Y! O+ v    if n == 0:" f# Y% ]; [7 e* a' m# k2 J
            return 0  {/ L) d) z# |3 C5 S
        elif n == 1:0 _* L" v3 L/ B; @
            return 19 R& a/ t8 s1 n
        # Recursive case# X- c: W3 }! e8 E* G  b2 J
        else:
    : |6 b5 z" @$ N, D. s        return fibonacci(n - 1) + fibonacci(n - 2)
    * V! i4 P+ z& l5 R% }' t
    0 Q: T4 }- q' _, H+ e2 i- M8 F0 A# Example usage7 ?. u- y, R: i- |
    print(fibonacci(6))  # Output: 83 C) m1 v: j) g( E& w  Q# o. W! Q

    0 f  B2 U% [9 TTail Recursion
      u- ^2 _- k: f" r$ F( b6 F( l; v, e5 `* w# ^
    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).
    ) h  ^8 i9 f  l
    2 S5 e, ]# |/ W$ z& jIn 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, 2025-11-30 17:20 , Processed in 0.031634 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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