设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    5 P) r' J/ Q# M) z" X
    ) M$ }& r  C  d4 c- v' `1 Q解释的不错
    & J0 J9 t' E1 j( X. E% ]8 T3 T; x" D. ~
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " D8 E1 B) l0 f. a; G
    6 L7 N5 [8 H2 L, a, K6 @* X: x 关键要素
    + u+ l  h1 U: y# A8 \1. **基线条件(Base Case)**
    4 n# t  t5 m0 M" k- f7 `6 `: y4 |1 r   - 递归终止的条件,防止无限循环
    ) t6 v) J$ g9 K5 V, w   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1+ Y+ M, J: h( s

    0 Q4 p' U  H$ ]! b$ ^8 D2. **递归条件(Recursive Case)**3 L9 a8 V/ Y' y0 `8 F2 E
       - 将原问题分解为更小的子问题
    9 J3 e1 C6 f# }0 i9 r   - 例如:n! = n × (n-1)!
    9 _+ T0 E4 p5 z7 u. b! n3 P9 Q& _" r+ e, f6 K! l
    经典示例:计算阶乘; v6 v9 u) Z+ s" E; r
    python; h% p- B3 |; W, T- f9 W3 z5 H
    def factorial(n):
    ! Z9 g# S2 W5 w- P- a& o# V    if n == 0:        # 基线条件) q; I& f5 q* s; n8 }
            return 14 o! T7 F. _  A" x6 c: y
        else:             # 递归条件
    5 L1 v. v, P, {+ N% r! F        return n * factorial(n-1)
    ( p8 R+ W3 [1 _0 }执行过程(以计算 3! 为例):* {$ o- W7 o6 L8 v
    factorial(3)6 z' s. z5 P, `$ y# e! t
    3 * factorial(2)
    8 [9 s; l: Y- @2 \3 * (2 * factorial(1))
    ) w2 g4 W9 p/ L' R6 j3 * (2 * (1 * factorial(0)))& L+ y9 j$ }& F. k0 U, `
    3 * (2 * (1 * 1)) = 6" q  D" m# t. I. h4 |
    4 p! I. x2 N, D3 ]# g, G# ?
    递归思维要点. G$ D5 S+ G. o  [; @4 U
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! Y) K. v4 Q8 _7 N/ C$ ~. w2. **栈结构**:每次调用都会创建新的栈帧(内存空间), g1 x3 S( X# X; S1 O& F
    3. **递推过程**:不断向下分解问题(递)1 P; \# _( K  ^  F% X& ]5 T
    4. **回溯过程**:组合子问题结果返回(归)
    + M' ^+ k1 G2 z8 h/ _6 y
    3 Z3 K5 U7 I6 @! E2 |9 M 注意事项
    / Q4 ^  H5 h8 G: y1 ]# c必须要有终止条件; j2 k; [$ u6 o& ]
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 u; L* E' X$ r  D1 o) N
    某些问题用递归更直观(如树遍历),但效率可能不如迭代- L% K) M5 N0 D% [3 j" s4 |
    尾递归优化可以提升效率(但Python不支持): g  f+ c  h" x) Q0 _# e

    " u, }: w3 O6 K; K9 S 递归 vs 迭代
    - D3 l- @+ Q$ |, a/ J|          | 递归                          | 迭代               |* p/ s, u* k2 U  A; t- I  S
    |----------|-----------------------------|------------------|
    3 C& L2 k# c; {, M$ ]/ k/ N| 实现方式    | 函数自调用                        | 循环结构            |
    ( j& o, M1 }/ l; r, Z8 m| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |( V) e+ J. w3 j5 h* n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    - y! [5 D0 w$ Q8 Q| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    / L, q# N: P+ Z4 E- M) a+ b% L& B9 W0 V: `- L3 S4 r% v0 i" a- g
    经典递归应用场景
    6 S  b# N  h1 W7 a, ]  [' A5 p1. 文件系统遍历(目录树结构)
    2 @! P2 n  t# C( E# b; D2. 快速排序/归并排序算法% f! E9 ~; p) Y" r
    3. 汉诺塔问题& X3 ^0 L8 u" j1 P. k
    4. 二叉树遍历(前序/中序/后序)& x5 G4 S' X! A- W- M# E6 R
    5. 生成所有可能的组合(回溯算法)
    : d1 W# B8 c# X3 V' V/ z, a" u1 R1 A$ F" e& n% X- P
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    6 分钟前
  • 签到天数: 3139 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒," Z  u% H% e5 A! |: \  }2 y
    我推理机的核心算法应该是二叉树遍历的变种。
    + b5 ]3 w. |0 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:
    1 \- y; \" E6 {( H* j. s9 @Key Idea of Recursion& Z* N1 k8 S' y1 b

    1 U! f. c0 ~" l4 p5 vA recursive function solves a problem by:0 p, G0 o) P2 `  T( O" Z6 t$ U+ y

    1 }# R. f0 ~/ w    Breaking the problem into smaller instances of the same problem.
      f/ F$ u$ @! K- D7 ~( ~( ?+ m) u3 F
      m; e- K5 M6 e; j    Solving the smallest instance directly (base case).& u/ d4 I) {9 O4 @7 n: `/ Q

    : p0 S" I! y8 \6 M/ q    Combining the results of smaller instances to solve the larger problem.
    3 t+ ^# f* w: V
    & h, [$ i, q3 mComponents of a Recursive Function
    # b) K* ~6 r  y3 h" h; k3 b. M  ?* P5 R" P4 p, o
        Base Case:0 T( e# l) V7 J2 p, w

    3 G3 x; @7 f; O* i7 l4 l4 R        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' H. }( z0 o7 I2 M* s& R) d
    / s# P4 n" @3 s8 a- `
            It acts as the stopping condition to prevent infinite recursion.
    $ I/ Q8 p: K$ l) {# Q9 e
    # D+ _/ ?2 M" v4 B6 r" T; g        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.9 u( A* G% v/ A$ Y* z1 q* w

    8 L" C1 w# ^4 B' g7 R; R    Recursive Case:
    # z8 Y' X( K6 {
    % f/ g. U, V( I2 O0 u        This is where the function calls itself with a smaller or simpler version of the problem.
    ' x/ }* a" `$ @5 W3 L  ~
    / ~' Q- h! O. O' i; [5 \% z        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 m: s$ M) J- G& \, g1 _8 e( y& V. ]
    / E% Z4 k1 q) u9 h2 q
    Example: Factorial Calculation. H9 n7 s9 T4 d: M0 M
    2 q5 b( s1 h2 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:
    $ h& p6 ^+ H  q5 E7 {( E& w& d1 w7 M
    / m/ d7 A8 I+ a+ I& R& {% t- V; p    Base case: 0! = 10 j; l6 H) `; ^$ e+ F; S

    7 p1 f' e$ ^8 i4 `" m; j; A    Recursive case: n! = n * (n-1)!( K) Z4 F3 B4 R; k

    / y2 D  b4 g3 {Here’s how it looks in code (Python):0 B' ]. b  u- J2 w. u( ]- s
    python
    + ~5 k' O8 N9 b( M9 W1 G4 \- L8 I3 i$ E/ v
    , h; b  O0 V0 T! I/ Y
    def factorial(n):* v/ O, g5 v( u# n5 u/ K3 x- D! j
        # Base case
    1 s3 X. N5 J; d! k: S' i    if n == 0:
    6 x" E  v) Z) h" L        return 1
    ' |1 }9 M8 z+ y7 c    # Recursive case- ?* O7 }( e5 I" z! k1 R
        else:
    8 x) U/ D0 L0 J; n        return n * factorial(n - 1)& m- o/ @7 e7 I' U& c  J3 l

    4 ~: d! K1 Y$ y/ d0 i# Example usage0 k( A8 j) K, B- m4 B
    print(factorial(5))  # Output: 120
    , Q) z! a4 O& \: i" A/ L5 N) c9 M+ w$ y' Y2 V! L3 Q: \
    How Recursion Works" e  @8 o$ q4 l
    ; Y8 h; Q$ l  h6 x$ Y
        The function keeps calling itself with smaller inputs until it reaches the base case.9 W/ K1 o& ~! J
    2 ^- N! O! M1 k; c
        Once the base case is reached, the function starts returning values back up the call stack.
    7 Z" N! P8 I0 Q) @: X! I
    7 X9 n$ Q: U# x1 M' i/ R# h) W! Z& r    These returned values are combined to produce the final result.8 H+ e7 i: I: ?( m! Z  C  Q

    3 W6 A/ b" [/ p7 {/ A: H" ~* p0 IFor factorial(5):, ~, j& u. @3 g3 q
    $ Y0 Y1 Y9 H+ j4 g  H! l

    " m1 @$ ]! e8 Xfactorial(5) = 5 * factorial(4)
    " t1 G8 T; l4 v/ k2 h( Lfactorial(4) = 4 * factorial(3)
    - _1 Y5 c5 U1 t; j- Zfactorial(3) = 3 * factorial(2)9 ^6 r5 k3 K# F( N5 B! T0 y( I
    factorial(2) = 2 * factorial(1)4 }! L4 h" J- R7 a' ^
    factorial(1) = 1 * factorial(0)
    / a/ s6 s1 P# \% B/ Q! [factorial(0) = 1  # Base case
    + n' p' G& L8 i3 {9 b. i1 r: f! C
    Then, the results are combined:7 i# _4 V; x- I- k( j' f

    % H, o2 |, s3 E0 g
    1 K1 }# c8 b  W. s3 ^factorial(1) = 1 * 1 = 1) M8 _; V: i6 Z) Z/ r; G" [6 w1 k
    factorial(2) = 2 * 1 = 2* r- _2 k* p  }) @
    factorial(3) = 3 * 2 = 6
    - j$ W- q* ]# K; d8 xfactorial(4) = 4 * 6 = 24
    8 Z. Z. r" k& Mfactorial(5) = 5 * 24 = 120
    0 J, F0 S8 R5 a: m/ s8 x7 N0 ]2 A
    ! \. W6 ]1 G8 UAdvantages of Recursion  G! q' G# l) ?4 }/ v9 ~% p

    , X  S" {6 y* k; n    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).4 T- i0 J' p7 E) z) U% d* S2 ?; @- Q

    4 V. R; K% a! Z; y  i% p! |    Readability: Recursive code can be more readable and concise compared to iterative solutions.+ k$ u: v( t0 u
    7 @3 F0 ~( {* U* O* _: d
    Disadvantages of Recursion* g1 Q% u2 r0 v( ]

    + |5 H/ Y, b( T    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 J. ^0 F" R: ?( A8 j7 I# `; j5 u, L, U
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# A$ P- t' E9 R$ V) k3 \+ U

    " q5 \3 ^. F/ N8 [When to Use Recursion0 _  n& i& ^' [3 |  W7 c' n

    4 |: q; X3 t! I3 p    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ) J( H/ l, S2 ^3 c1 B8 E
    ; |) \8 y, _% i3 m2 i5 ^    Problems with a clear base case and recursive case.
    : r- r5 {4 T1 _- M7 ?+ p3 k
    - c  B$ ~8 r5 M9 ?& ^5 VExample: Fibonacci Sequence; T+ O: f2 p- C1 q

    , i" b/ \& Q$ }/ GThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 J! m. m$ d* ?: d9 ?5 ^' m& y* U
    + U9 E8 \) g8 C+ Z( E    Base case: fib(0) = 0, fib(1) = 1% i2 H8 q& ?+ {- n0 I8 m& [

    7 Z8 n6 {, E$ b' d1 O" t- ~% c9 c    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      w7 q( R# F* P5 m# I
    8 K2 J0 M8 V2 u& wpython0 c: m; r/ ?: c

    % k: T5 @8 C3 G& C
    % W% T$ A9 R  odef fibonacci(n):
    8 @2 @3 {  o# N6 F; Z: m" n    # Base cases; D* h2 b# [/ \1 W& H+ b! f- E/ i
        if n == 0:
    & B) n9 Q8 {/ \/ c        return 0
    6 [$ a/ W/ i2 g$ k    elif n == 1:
    : n+ s, X2 ~6 K& z) t* I        return 1' g; a+ M- C" y# d  l' u: ^$ j4 I8 }
        # Recursive case; s" m& N  N- y/ y" ]- O7 E; u
        else:, O( d7 a9 K7 D  Y' J; e
            return fibonacci(n - 1) + fibonacci(n - 2)
    2 i* E& \8 w: m: x( {# L+ I! i# F; }$ Q7 v  g
    # Example usage  I- l/ u3 i) G1 e5 I* A
    print(fibonacci(6))  # Output: 8; P/ q# r) z) L$ F+ v! `4 ]

    1 H: f8 x: ]$ v& R9 @8 O: v6 CTail Recursion
    : k0 d5 J% ]0 ~8 a8 t7 ]5 v/ C* v" ^6 u) l  D! g% ~! I% I4 U; 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).4 G2 _! _/ ?; v& o

    0 ]# @9 {8 r: I2 r) V0 v6 gIn 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-7 06:27 , Processed in 0.031344 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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