设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ' X1 j4 Z. j1 L$ C5 T

    3 V9 e! T& B5 s# w1 f: j2 c解释的不错. y2 ~: x0 b5 Q7 @
    " l, U$ S4 @( @2 ?2 A" O) i7 f
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " c$ E; j0 Q# A3 ^. ^; r- w9 N
    7 X  E( v( _  M 关键要素' ^; i/ c3 n5 l% a( G. U- N  K
    1. **基线条件(Base Case)**$ s1 p/ P0 p, `8 ^! ?" r* W
       - 递归终止的条件,防止无限循环6 H! H1 Z, \2 q/ T6 T9 L) T# `
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " I* K9 r8 o2 H6 l$ C
    + D# Z& F  G8 Z+ p2. **递归条件(Recursive Case)**
    6 E6 H" B4 ^) f0 n, B& C/ F* h6 z   - 将原问题分解为更小的子问题# g5 ]. v% m" w  [
       - 例如:n! = n × (n-1)!4 o3 X5 u& N* r6 ^$ @& E
    - |0 p7 R+ }2 n3 \4 [" x  U# X8 |. P
    经典示例:计算阶乘0 V/ V* p" O" r! G9 Y5 b( L# f8 L
    python
    1 p7 i  ?: S! j$ [2 i/ j) S) Udef factorial(n):
    ! o9 V/ e$ b4 F    if n == 0:        # 基线条件! P' l/ {. C9 ]! g3 L
            return 1
    7 ~7 S, N' x2 B: `' Y3 i    else:             # 递归条件9 S& s, M: z6 Y# \$ f5 q
            return n * factorial(n-1)
    4 Q+ w7 k. K6 }/ P执行过程(以计算 3! 为例):
    % {7 b; }' J& m, Mfactorial(3)
    ! B& y3 z* n! {" b, Y* q3 * factorial(2)8 d- e+ B- N9 [6 O& l( k
    3 * (2 * factorial(1))
    , I0 B+ X. ?3 T) u+ a6 A3 * (2 * (1 * factorial(0)))* S+ O6 o" z* A
    3 * (2 * (1 * 1)) = 6
    $ o$ \' j2 N8 [. ?* J( P  i7 [. `
    , j" n; m$ N0 _+ r" I 递归思维要点
    ' Y2 P7 ]* l. V6 k' l& J1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . A! Y7 N6 b, ]* Z2. **栈结构**:每次调用都会创建新的栈帧(内存空间)% S9 x5 k# x, j( v
    3. **递推过程**:不断向下分解问题(递)
    , v0 q+ s6 N: @4. **回溯过程**:组合子问题结果返回(归)
    6 C) `9 B9 i% A. |9 V; C# B/ H; P9 Y2 q
    注意事项8 q* @6 }! J0 A' M' m$ L. g
    必须要有终止条件
    - f; M3 m" E$ r) f! \. J递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 W6 `7 v& Q, `) ]9 a- k: m某些问题用递归更直观(如树遍历),但效率可能不如迭代
    # d. g0 O6 ?* Y( Y$ s/ W2 H尾递归优化可以提升效率(但Python不支持)/ R- i  e4 {7 ?

    $ Q: o, Z+ U& z* R% M 递归 vs 迭代
    ) C8 f' J; [; ^3 x5 O9 C|          | 递归                          | 迭代               |3 {0 b( c5 o6 X9 Z
    |----------|-----------------------------|------------------|
    - Z; C4 I! {+ x1 i, g- G5 c| 实现方式    | 函数自调用                        | 循环结构            |' l; ^, ^8 L2 T3 q9 t2 ]  [
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    9 E3 r9 B: x  a| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |( D: c/ `. q  b) `& N$ F, |
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 U! L& |# z7 a; A: e: l
    " K2 N+ Z% b* B9 q* D1 _
    经典递归应用场景' V# V+ n, p( @; @4 ^( [6 s
    1. 文件系统遍历(目录树结构)
    $ ^7 d9 }% Q# w* T2. 快速排序/归并排序算法& G4 w2 Q5 z3 X: I
    3. 汉诺塔问题
    5 |# O& y5 a+ v4. 二叉树遍历(前序/中序/后序), d& \& h( l+ K0 H  w' Y# a
    5. 生成所有可能的组合(回溯算法)$ n) g  A) w$ t0 L

    " }7 j* p4 R% @试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    前天 06:31
  • 签到天数: 3146 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% L4 U# h; r- r; p) K6 I) A
    我推理机的核心算法应该是二叉树遍历的变种。+ Y9 ^. a2 S# I) b
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 M" P1 H8 p! b
    Key Idea of Recursion
    4 ?6 P, N; z. R# T9 i6 N. E9 Z$ ]; H. _, f" G; A& C
    A recursive function solves a problem by:% l8 _. G6 u; _! s3 y

      s2 j6 l1 r; g! G  E) S) o    Breaking the problem into smaller instances of the same problem.
      p3 g& o1 b9 i+ E* w9 _% I% _
    " j9 [. s# k7 o( o* K1 P, V, u    Solving the smallest instance directly (base case).
    + u, ^/ ]) z' L; w6 O4 N; @0 g$ }, L. E
        Combining the results of smaller instances to solve the larger problem.) e2 b# `8 y- ]9 _) v. o! N
    : `; p3 J$ ?% j
    Components of a Recursive Function- ?& {9 g; g. f
    ! @, x( i; L4 T0 S, s
        Base Case:5 R6 J3 @. k- d) l7 J7 K" e

    2 ~' Q+ |3 _& m0 h: ]( u. U  Y        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 H5 `& V  m" u1 p8 E8 O) P; g. k+ T9 A/ c; H, ?
            It acts as the stopping condition to prevent infinite recursion.. ?3 W( @" {! _1 a. z) U% G

    " X/ L7 D/ [6 Y4 _- e0 F        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    7 s' n# x1 s' h
      B& a' t& A% _% x3 c    Recursive Case:) S! E9 }; r' s; `
    7 i7 _6 l7 o$ ~
            This is where the function calls itself with a smaller or simpler version of the problem.
    ) j# t) F0 t  j) R9 N) V4 n* a$ f  @
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    * c1 U! \, B( Z, v2 N; c6 Q$ k" A' q9 Z* B# D
    Example: Factorial Calculation
    ) _$ G% L7 s- e; h& i
    ; T. T) X0 m0 c" z$ t0 r! o/ QThe 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:
    9 F2 F0 I2 J1 [' u3 E
    8 s- O- {( K9 b. y8 U* n    Base case: 0! = 1
    * e) Y8 c5 @) ]: R4 y! F
    / B( w( N. n$ c/ z# _3 v: C    Recursive case: n! = n * (n-1)!
    ) v& @4 H2 P4 W* ?" p2 c4 D5 w# d+ \' ?7 D: l6 y3 M& [
    Here’s how it looks in code (Python):! k, j" l) M' Q/ g; @- A8 L% M
    python
    ' J1 z/ f, y/ V6 I' b( D# Q/ t7 ~

      ^- p" W& Y; Adef factorial(n):
    9 m4 @! t) m; N& o. E9 X    # Base case0 v4 y% x7 ?8 ~  o0 \. r' K
        if n == 0:
    / ]3 h& I% I- C9 G" j; b        return 1
    # |. a: G3 F$ X( S3 V    # Recursive case' R" ?2 j% O3 o1 d- F2 o; ^# }
        else:* i# E( ]# j$ Z% M. G: D
            return n * factorial(n - 1)
    6 E6 f* L+ x2 |) K4 S5 o, V* [
    0 n: H: U- y" [6 q# Example usage* z4 J# V2 {& ]2 ~6 i1 r5 i( T/ v
    print(factorial(5))  # Output: 120, L) C2 H9 P& g
    % w9 D! b" P0 L8 d4 H( _
    How Recursion Works( c$ V) c* b" ^7 u; I5 x- Z) c
    ; ]8 f, c& N" @$ q
        The function keeps calling itself with smaller inputs until it reaches the base case.7 A$ M1 ~( C7 G4 F- Q
    1 w5 l) o( k; X3 a5 M1 X
        Once the base case is reached, the function starts returning values back up the call stack.
    : y/ \, I4 P0 k$ E3 @5 q$ f# [/ e  S6 }
        These returned values are combined to produce the final result.
    3 R7 u4 h) w+ K/ T8 K0 o% s, l( A. F* ~3 r1 w+ H9 M, J% Z! u
    For factorial(5):
    ) p7 m. z+ C% d; }5 t2 Q* |
    ' s; x6 `3 d7 J1 d' k
    5 x8 y8 o* v  u& g6 z9 _# Efactorial(5) = 5 * factorial(4)) `& Y. J" A4 N6 O2 k0 J. v  X
    factorial(4) = 4 * factorial(3). y! m3 [) B) L/ I, a, \
    factorial(3) = 3 * factorial(2)7 L+ b! c7 ]3 P
    factorial(2) = 2 * factorial(1)! s5 }- h5 M3 T) R1 I
    factorial(1) = 1 * factorial(0)0 p# E8 q% s6 r' ~& D6 L: i' _
    factorial(0) = 1  # Base case
    ) g9 J( D& R' ?9 ^# [% s2 A. J- P/ M; U. h  z4 q
    Then, the results are combined:6 \7 p, f8 Q+ }

    % p3 c2 j6 |9 o7 Y
    1 G& h7 @5 E- gfactorial(1) = 1 * 1 = 1, l. l" H6 W7 [. g! u* f7 ^
    factorial(2) = 2 * 1 = 2# o1 l- O& I3 a+ R: L1 W% n6 m
    factorial(3) = 3 * 2 = 64 S& E; _4 a! u4 P
    factorial(4) = 4 * 6 = 249 R. _1 `6 u% l: K1 _
    factorial(5) = 5 * 24 = 1208 ?( ]( _6 W$ }! R" o& F8 M7 z
    ! |" u1 q1 s) m) O  Q; X5 _0 @. e4 }. G
    Advantages of Recursion. U$ U5 J$ @3 U% [# _: z
    9 v: Y! v5 n. v
        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)." H  D. L) A6 Q# o: S( r
    ; v' t# W* n# D0 G5 l# d0 \
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ q+ M  u; j1 @0 i' [6 {

    # h6 m2 \" s5 @! m+ n+ ADisadvantages of Recursion4 u$ b& n# v$ B) G& W# p

    / H+ a- _+ G2 W7 g    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.
    4 w/ j% E: I( Z( U: F+ D. S
    - _5 G) M$ C+ I6 N. H    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).- W* e7 }" r/ \% ?/ V% U
    0 b$ h/ J$ \5 R0 \9 `* q. T5 e+ l
    When to Use Recursion
      z8 n# n2 q0 l- P" ?3 @& l! s9 c
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % g* W3 j2 S+ t# ~2 I" T% J1 o
        Problems with a clear base case and recursive case.6 @- w6 ]0 _3 D0 w0 I$ C

    3 f5 G$ Q. g/ r0 z8 PExample: Fibonacci Sequence6 \* W% @3 z; r% K5 U6 p5 D
    3 M; B. I- K! J, j; _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:5 M1 j3 ]" }8 [( G2 H' b
    * J: N" {! g6 g! g1 c( D
        Base case: fib(0) = 0, fib(1) = 1+ T" N0 I0 g' B: o

    ) D$ ?( ~/ G, s% J    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ' U6 O6 r5 P  ?* u4 e# l3 @" d- \0 a- t. o0 {; F) J, J
    python6 _. m2 N) ^+ X' Q3 }6 A# x5 l

    / L9 X, b2 k% r( a, l. j
    3 o9 K% l; e' A8 A+ J$ c0 Zdef fibonacci(n):. u( C" s: O! T7 y/ J
        # Base cases5 j+ L9 r6 ], w2 J1 w0 [6 I
        if n == 0:
    6 Q3 W1 o( ]% H- b& ?% G        return 02 U5 c& z, j$ \' k
        elif n == 1:4 P" y! C0 U. k1 I6 w+ [
            return 1  ^& Q/ b' T7 D/ c  k6 C' s
        # Recursive case# `3 O) g0 `+ s
        else:* Y8 Q$ ~  T: o/ H& @% X5 v
            return fibonacci(n - 1) + fibonacci(n - 2)
    9 g+ w& ]4 S7 U# P" q+ A) x5 \: i0 P$ k. D# \, s. `
    # Example usage0 Q$ V0 ?" p$ r7 g8 t0 z
    print(fibonacci(6))  # Output: 82 T0 F* F7 q5 g2 H" Y* h) |
    3 I: O* H/ [* V* u
    Tail Recursion3 f9 z) U: \& I4 o3 z% b
    * u0 r& P. O% y. u* 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).: x: ?4 Y0 e0 G# \

    7 v. C- m2 D: Y7 }6 t; ]* cIn 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-16 22:01 , Processed in 0.031965 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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