设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 o1 p- E6 b7 t

    $ E5 m- E! L& \: q0 N# l解释的不错
    7 U1 ~( |0 s; N: S3 x: z% e" w4 ^+ I1 _- j! I* f; t1 R8 h  Z2 ]/ l4 [! v
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : e; I7 W% _8 {0 v
    / ]% M0 V& S/ y8 v, b& ]5 V: _" b6 z 关键要素
    , O7 J9 @1 P- y: }+ v1. **基线条件(Base Case)**
    # E! {% L" R. @# K; C; |$ \   - 递归终止的条件,防止无限循环9 b& S" E3 k0 ^7 f  L; K9 c( z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1, n$ o% \# Q; V
    . s  q6 v4 P& s8 O7 c# H% v% b+ ~
    2. **递归条件(Recursive Case)**, [* H9 @, s. P2 ~2 L7 E
       - 将原问题分解为更小的子问题
    6 h# x8 d7 ]$ q# P" ]   - 例如:n! = n × (n-1)!1 z9 ]9 r, p! t7 I- P9 n. x% n
    # R: u% {# d$ z
    经典示例:计算阶乘
      U) b5 M. |! R8 Epython5 o! F  Y( n% f( m. y: [
    def factorial(n):+ z3 ?+ Q! K, K8 \/ k
        if n == 0:        # 基线条件
    7 }/ G. }: d! ^( Q* L        return 1
    ' R( g$ p* N- _    else:             # 递归条件
    + a4 j! G* P; o/ P( J8 g        return n * factorial(n-1)  [- z2 `3 D* ^8 N
    执行过程(以计算 3! 为例):. b0 |4 Z# J8 \* X
    factorial(3)1 M) W8 j, Z' Z# C8 \+ k9 c
    3 * factorial(2)
    ! q3 x# f7 G4 h7 c5 ]. g7 e3 * (2 * factorial(1))
    6 b: g4 ]1 P& X  }) M- f0 p' [" t) O9 y3 * (2 * (1 * factorial(0)))- m/ K4 r1 k9 J/ V+ @! |
    3 * (2 * (1 * 1)) = 6& n* y* y' _: [1 _2 v4 p
    - ]$ w# e/ b  C" O4 W* \
    递归思维要点& _+ T( m; r$ n* O8 d4 q1 B
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑1 X' a9 A4 W. z# W6 H; p+ u
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间): b3 L: Y: b$ l3 y' i1 ^7 y
    3. **递推过程**:不断向下分解问题(递)
    9 r5 R, w2 X# N& W/ w. \/ Z# Q9 _4. **回溯过程**:组合子问题结果返回(归)7 N0 R' @& r! W8 i  ?2 s# _3 g
    - c3 T" p5 e" ^5 N, P7 C
    注意事项2 ]7 K9 y/ s; s( S. x& {
    必须要有终止条件
    6 I8 T: @" y2 a递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    : I3 D& V: N  T* I# U. K某些问题用递归更直观(如树遍历),但效率可能不如迭代# f  }1 b  @# j" [! I5 X3 l
    尾递归优化可以提升效率(但Python不支持)
    ; f4 \1 C! P: {0 }
    5 S  w7 |2 ^2 z% l! v. X 递归 vs 迭代
    ' g8 C- ?' |% I* c) r/ b|          | 递归                          | 迭代               |
    7 s+ m+ k% b4 Z8 P' m+ {|----------|-----------------------------|------------------|
    9 A8 L* p/ O/ A8 q  D1 d| 实现方式    | 函数自调用                        | 循环结构            |: j# z9 z! F1 ~! X. `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) B% ~# L3 P0 q* S2 S$ H0 k  G9 D
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |2 }( S0 q& u' P0 Z* K4 S
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
      `  `* _: t$ E( _9 c9 |6 ?
    : F4 V. U6 u+ N 经典递归应用场景; q1 C/ y9 h* G& v8 n! x
    1. 文件系统遍历(目录树结构)
    , c) M. A; z6 |$ ]2. 快速排序/归并排序算法
    : s  n8 e( n  |" K7 ^$ D3. 汉诺塔问题
    3 X' h" c5 Q5 A. T8 M  }4. 二叉树遍历(前序/中序/后序)
    - {! n: q! E3 _% W  K5. 生成所有可能的组合(回溯算法)/ n/ e# _) N* H; w
    ; Z6 @5 i0 {. K0 R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. |$ J- ^- s  Z$ e
    我推理机的核心算法应该是二叉树遍历的变种。: }, G& [2 v' ~+ h- U! `" Y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ; v6 s9 U$ r  ]# k1 \Key Idea of Recursion
    & _( e6 t, l, m/ q% t2 T1 Z- r% Y( V) l* t3 ]/ K" D/ c1 S' _
    A recursive function solves a problem by:3 _  ?- U2 j. y) X2 A: F4 }8 G

    " t" ?" r$ f8 j, }: ^    Breaking the problem into smaller instances of the same problem.( J- e3 J& V3 [0 L) M1 `
    / n9 @8 q+ D- F: x
        Solving the smallest instance directly (base case).
    4 k4 Z6 O/ \1 V& h1 W+ _$ L4 y
    # v( z, X) ^) l3 Y0 Z    Combining the results of smaller instances to solve the larger problem.+ S1 ]3 @2 H) ]" h
    % i/ n( p/ e+ `  a7 J
    Components of a Recursive Function. h9 Q( K2 M8 x% B& T8 r
    1 E* L! X4 ]5 M0 p0 ~
        Base Case:! r# i6 ~% i+ k9 L) A
    5 @3 e5 N9 A1 V+ z( S& }
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.- N) k3 z; N; ~6 T# _
    ) w. g# F$ p" c% V3 c
            It acts as the stopping condition to prevent infinite recursion.2 U# t5 O  M" A. t4 l
    * x9 A/ }, N6 T( v4 L& _5 O4 \/ n
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.) V% n$ _! k1 X0 m. C) |
    - U* c: A+ J% c
        Recursive Case:
    5 w. v( v  p2 _( x
    " D  R7 ?) g# w6 l+ F" b1 l        This is where the function calls itself with a smaller or simpler version of the problem.
    " n: Y. J- N3 U$ d+ Z
    # H% k( b5 k( x7 y. J% w$ q- j        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 A' P4 Z* J7 P+ @: Z
    6 D( U6 W; w% D+ A7 GExample: Factorial Calculation/ u, E# R+ b0 k

    2 M7 Q0 x$ M9 E1 O* I7 gThe 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:' g; u$ z# H( ~' G' D# V# `. M
    + E7 i! ?' Z: x1 x( C+ v
        Base case: 0! = 19 S, V, Q# n! Y6 c; |* d5 Y

    * a. D" W- f; A* T. t4 C    Recursive case: n! = n * (n-1)!& B+ v, v$ h! h9 B8 D; p9 Y

    ) |* d+ m( Y& q. j! {8 S( U& NHere’s how it looks in code (Python):
    8 J* f& l0 f% _  Lpython! q, |- Y: P! y) r, N
    * O( n: E# _/ n9 Q4 B# v

    * j7 [( E! x# G& udef factorial(n):
    6 o) N/ k% |- z7 J- F, b3 q    # Base case
    + `9 f" @% y" U    if n == 0:
      g' C6 |) b* g! I; R        return 1
    ; u6 j% B5 D' a- y8 E3 {    # Recursive case% A0 @, W: i, x5 X' _. d8 A" Y3 K; D
        else:" O! H7 a8 Y! z* {6 V% E. |3 X
            return n * factorial(n - 1)" H$ }/ d' c5 X7 v

    $ |) {1 w0 `' G, O: m, x" z# Example usage+ ?$ e. `3 S# Z4 C0 ]
    print(factorial(5))  # Output: 120% j8 J8 S- P: _0 c7 {2 @

    ( [; e! `! N  S! G' |How Recursion Works
    3 i! o) K7 S" l' L: M6 \  r
    $ n2 v+ P5 s8 r( b' j    The function keeps calling itself with smaller inputs until it reaches the base case.
    : {7 j* N8 `0 y3 p& _1 }& N& q% b3 k- P# w* M- i# k6 i7 b
        Once the base case is reached, the function starts returning values back up the call stack.; L* D) ?' C; `* a6 l' w! A
    + K1 r8 X. T' F: R2 f% \. ~
        These returned values are combined to produce the final result.
    2 u+ V: ]( w9 l* n/ Y  s- i* X3 ?+ Z+ I
    For factorial(5):* W/ E" D* \2 ~  c0 W
    ! N/ g/ x" y* K+ i

    9 W. P' P% j# f8 ?$ x+ T) Afactorial(5) = 5 * factorial(4)
    ; c) q/ c/ Z5 Zfactorial(4) = 4 * factorial(3)2 `  u- H+ B- W1 b
    factorial(3) = 3 * factorial(2)9 u) @/ q+ l0 t/ J4 ?$ g& H. N
    factorial(2) = 2 * factorial(1)- X: T/ |3 E4 u4 c
    factorial(1) = 1 * factorial(0)
    . e2 q' X$ i0 Y+ ]$ rfactorial(0) = 1  # Base case
    : v; A# g6 W! E2 P% B0 C2 t' }$ q! D, r2 ]
    Then, the results are combined:( H6 {! Z9 H+ k  q+ U. Q+ H, K

    5 C3 a' r2 p' J; B1 J
    / x1 C5 ^; u% q3 F$ w. lfactorial(1) = 1 * 1 = 1
    2 m& i1 _3 j3 \6 jfactorial(2) = 2 * 1 = 2
    3 t, l  I- H: b6 Lfactorial(3) = 3 * 2 = 6
    . A0 g1 T- M8 {6 P( D1 jfactorial(4) = 4 * 6 = 245 y7 z1 M0 d6 r
    factorial(5) = 5 * 24 = 120
    6 E' W- d. O' U
    ; W2 `- d: {' ~1 l( L( dAdvantages of Recursion% h3 d+ U  Y; B0 u( h

    $ f2 d' ~1 `1 j( T    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).! ~! ?& a7 F9 x. B
    1 g4 S( i8 i% C  b' W% Z7 y3 i( e
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 ~' q9 \9 [' s$ a  o3 _/ e5 k) Y: F: \' N$ M
    Disadvantages of Recursion
    5 P" C. R7 i/ H/ h3 K; S( [
    + ^# i$ J' R) e    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 \; S9 w9 }
    4 ?7 z) E& f6 j  D: c3 U    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).; W. x  ?$ I7 n+ {7 J+ ^
    0 E) S) I! Z( Q* z
    When to Use Recursion
    ; @) {7 C1 n4 L( i
    * E. H! H& ~' ?: j2 q2 h3 y    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).% o2 W$ i% q  l1 |7 N6 r" D

    / j- f) P* K7 b- f* a% M    Problems with a clear base case and recursive case./ G  ?4 j9 L- c8 |  W% {  K: E5 i7 H

    % `% Y6 S. H9 D6 cExample: Fibonacci Sequence( e2 C# f# Z% I# }5 D

    / v; s9 P, m/ W! }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:" A9 b( L- l9 g' j7 v5 x) [
    7 J& J6 U* G$ [2 E/ Z
        Base case: fib(0) = 0, fib(1) = 1
    9 S* C/ P7 Q" H' `/ ~9 a
    ! Z) G% T8 u, [: [% {+ p+ n6 }. }    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    5 W! ^* [0 R; `; h' b/ |
    9 T, A- ~+ A6 a* g# `python, s" {1 d/ x; W+ I

      Q! x+ }  O& e; `5 t8 E' H$ U% l. Z* i3 T9 Q$ d7 w
    def fibonacci(n):5 K, q- x3 C  Z! E' o
        # Base cases! E# ~% I5 g+ b2 R) n; l
        if n == 0:$ w% w( \1 z% h+ g
            return 00 W8 M& d7 X& z' B* X3 A8 \
        elif n == 1:
    " p6 f; w: r) B9 J' y4 |0 W7 j3 s/ b3 _        return 1
    + W( o: _8 C6 s2 k# p+ _. R    # Recursive case
    , N2 z2 G0 Z3 S8 }9 i( x    else:4 j9 N$ X& e3 n$ {, t2 Q1 a
            return fibonacci(n - 1) + fibonacci(n - 2)
    2 e4 ?$ D& f# f* L: @
    3 `5 n/ b' p( r; w9 c0 w# Example usage& |/ D# A0 k" E  S9 o  ]
    print(fibonacci(6))  # Output: 8
    6 P2 R* m; Y$ E" b( v; d2 e
    # ^" @/ Z# P7 f: W8 TTail Recursion0 T5 ~8 \# `4 e" W6 }
    : x' P8 G  T3 t" T' m3 X5 f" C" S# 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).
    & w3 N$ ^# P' ?+ n& u+ K/ m( Y5 B& E+ j. T2 J9 E
    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, 2025-12-7 15:28 , Processed in 0.032576 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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