设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! p& X2 V! p" a0 n3 U- a; J
    2 t7 t+ Z% L1 ?4 F4 q/ L2 U解释的不错3 R  q. N& z* `) G+ V+ a* `- h

    ( ~, @6 ]& Z9 |, [- _! O  ]递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。: ?; @& d/ K/ r7 a* }
    : f, P* d; K# [+ i
    关键要素% p0 m, M0 v1 ~! n3 }' V: D& r9 k
    1. **基线条件(Base Case)**; b& T5 g- K6 H) o; G& L9 j
       - 递归终止的条件,防止无限循环
    * A* ^8 Y7 c6 u2 ~9 i. `" k- c- U4 L   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1  o, L  _9 B8 \/ O5 m

      V$ d- I$ b+ Z3 _2. **递归条件(Recursive Case)**5 x/ ?" f+ }9 ^; B/ _  q
       - 将原问题分解为更小的子问题
    & N, |9 @; L7 }   - 例如:n! = n × (n-1)!
    8 X+ ^1 o2 ?7 k) p+ i, p& i) ?8 h8 d; X
    经典示例:计算阶乘
    8 i, G$ [4 Q% |1 i9 Rpython3 A8 @% z1 i$ t5 s2 K+ W
    def factorial(n):
    - Y) P/ P6 Z, M6 P; @! n    if n == 0:        # 基线条件  {: h9 N( {7 s% F" H% ^  b
            return 16 |+ ]  D8 u1 T3 z6 F
        else:             # 递归条件  J% w8 J  o" Y6 ]  ~2 g4 T3 D
            return n * factorial(n-1)
    2 N3 }5 Q% t7 o1 h" C- A" m执行过程(以计算 3! 为例):
    ! j- K  i- u$ mfactorial(3)
    : {+ `! e. K7 K7 Q$ G' {$ ]6 v3 * factorial(2)
    ! q1 r8 X9 h' L% a* ~( T$ C3 * (2 * factorial(1))
    , u9 D" r7 M: c" b( Y3 * (2 * (1 * factorial(0)))7 b' Y8 R; J* w& [9 A1 ^' _  b
    3 * (2 * (1 * 1)) = 6
    3 d/ e$ J5 d, q. A
    0 z  [. T6 l. I$ p6 E 递归思维要点* n5 w" h6 L* S) }
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑, H: z: b9 l. `  t
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 |0 ?% J* t0 y4 }) E3. **递推过程**:不断向下分解问题(递)4 i1 H; d) C$ T- [
    4. **回溯过程**:组合子问题结果返回(归)
    - P0 `5 F8 x  D' i+ p
    - S* W: a- Q, w% @9 d' @3 K 注意事项  W0 p. [0 i/ T7 a
    必须要有终止条件7 h% X' e8 U. r! I' A( T
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    - |, H7 i: n' ~% z, k0 u某些问题用递归更直观(如树遍历),但效率可能不如迭代, s$ \+ T$ X; k, t, T- ^- A
    尾递归优化可以提升效率(但Python不支持); C& V; Y6 Y. K2 {. d0 k5 G! c

    " y% _# Z2 Q% \: b9 B# \ 递归 vs 迭代* m. y0 z7 S( }
    |          | 递归                          | 迭代               |; B9 W* V  ]" k/ V% Y5 z; B$ N+ n
    |----------|-----------------------------|------------------|
    - V- P) W* D: h  U, Z/ a" x| 实现方式    | 函数自调用                        | 循环结构            |8 G- }  D$ d, ?5 A# a# F8 z( H
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |! `- d8 ]5 P. v( R2 y; [& X, s. W
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! g% t% y, L! _+ X" u! L| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 g+ {% W: E0 T2 M3 p, ]3 w: q
    + g4 @$ Z3 A) t7 H- r
    经典递归应用场景' T2 r9 [) j% ~( Y* U5 a) M+ d
    1. 文件系统遍历(目录树结构)
    + M+ V9 K# S, Q6 m$ z2. 快速排序/归并排序算法
    9 W- f6 ~* q  d3 F; R. Q: U; S3. 汉诺塔问题$ g% G; D( g% y
    4. 二叉树遍历(前序/中序/后序)* U& ~& a# c" T" L$ j
    5. 生成所有可能的组合(回溯算法)
    ; s& u9 b2 ]+ h  f9 i0 W  ]
    - Y- G3 _. Q$ {  x/ F" R$ p) L7 u试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    9 小时前
  • 签到天数: 2919 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 S  m" b. G( m. W+ P
    我推理机的核心算法应该是二叉树遍历的变种。
    ' x$ u- p. \2 X另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:  `' d1 _3 [, O; N0 Z; C7 M
    Key Idea of Recursion
    8 R' I% U% {1 ~2 _1 R& k& S
    * @- D$ _9 c! J# N: X2 R+ s' MA recursive function solves a problem by:
    . F1 M: T6 }' F. L
    : X0 ^& y5 h# J; G& ]    Breaking the problem into smaller instances of the same problem.
    ; O/ L: h0 y' p6 Y6 w9 n' \4 l5 Q, \4 A5 \) P- V0 `
        Solving the smallest instance directly (base case).
    , y9 c$ p6 B( r/ f! t0 ]
    - t7 k2 }; G, Z* ?/ K8 i$ [! ~    Combining the results of smaller instances to solve the larger problem.! Q4 Y9 k- T" u1 S# J$ U
    1 e) [( K9 }. n2 b8 n
    Components of a Recursive Function
    6 d. N' e  e9 Y7 D: r3 {! J
    7 w6 T2 {. J6 N' }, Z    Base Case:
    * j+ M0 K5 `  D* c6 ~" K. g6 ^# H; B# M' I/ l- U, I
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion." r1 e: [9 {2 N5 {

    : C) }3 C. y) |1 \1 R        It acts as the stopping condition to prevent infinite recursion.
    , v9 \- `7 b- B8 \
    & S% k, S" [2 K, p  I( B2 [        Example: In calculating the factorial of a number, the base case is factorial(0) = 1., t  T! Q9 f; y% E" k& ~. I
    3 V9 S- R$ m0 J
        Recursive Case:
    ' h$ |" q8 l) ?1 g3 [* @2 w: K, F+ ~/ p4 K' _# J
            This is where the function calls itself with a smaller or simpler version of the problem.
    4 r- b4 r' P  s% u0 \- D% C( O. N, o9 n. |
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).2 Z4 Y3 ]3 G) J: X6 ]9 W
    ; |: F9 v7 A" l& s4 W9 y" a- ~7 B
    Example: Factorial Calculation  c$ \1 u. m8 |+ g1 Q, [3 g
    6 }2 A4 ~! t3 F; w
    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:$ T, [" ?! |9 l/ x* P
    # _( ]6 k# o  u
        Base case: 0! = 1
    ' \# T3 h. N# i) B- u  a7 c. d
    , [6 ~. Z' h5 d& `1 K    Recursive case: n! = n * (n-1)!
    ) v# \. C9 j- a: P2 f. C; q; b
    4 ^" H$ u2 }0 Q/ s) N2 ^Here’s how it looks in code (Python):# s2 U: q0 @5 w" F! O
    python, j( Q/ R- n& ~: x1 ]

    " x6 O& F3 U* g/ G) N! E8 e7 r3 x, D. `# b9 S! J
    def factorial(n):+ w) p$ y7 [' n
        # Base case
    + Y- F3 w; Z# J2 p7 w  M    if n == 0:5 u' \2 k; n, X4 c1 r: J4 u
            return 16 C6 X" i! W$ x. d
        # Recursive case4 q) e4 Q' g" N5 b! n$ @2 V
        else:) s2 G4 E0 n' \1 T7 r
            return n * factorial(n - 1)
    6 Z2 o3 J& W+ P3 L2 Q: V# t3 w& s% W6 B* C, h5 `' s
    # Example usage/ r) }! T/ _! c
    print(factorial(5))  # Output: 120# `/ V4 }. F4 B7 o6 V. A
    5 v! K! a  j2 x5 E# X: J2 M
    How Recursion Works
    9 F& _0 a) h) P6 F/ b- Q( E6 ?/ p/ W' Q7 B. d9 f* m5 U
        The function keeps calling itself with smaller inputs until it reaches the base case.
    - h$ o' P6 x8 a6 |  ?. U, V; B2 t" z$ l6 I0 Y& t6 W
        Once the base case is reached, the function starts returning values back up the call stack.
    # W+ @! S0 t3 h# {/ M+ f! ?) o2 c% b2 Q5 M, \- Q! I
        These returned values are combined to produce the final result.
    , C& S$ y0 R- c- V! L+ R; n8 }1 X6 Q' I( V
    For factorial(5):3 e, S; G9 g/ ?" X3 g) p: E
    % ?8 D, w9 Z8 E  g
    : s$ u! _( B8 Z* w' L6 u
    factorial(5) = 5 * factorial(4)0 F' `0 @% q2 o/ K: N
    factorial(4) = 4 * factorial(3)8 ^# z2 M# D& z7 N# B
    factorial(3) = 3 * factorial(2). J% X" Z( p2 t$ C; E
    factorial(2) = 2 * factorial(1)& w, q* X" m; Y! _2 S1 f' B& O
    factorial(1) = 1 * factorial(0)( A/ j0 t; U, U- N3 Q0 F6 q+ L
    factorial(0) = 1  # Base case
    1 A; O" I8 a$ t
    + W* p: b! E) f1 I' {; L+ k4 IThen, the results are combined:
    + g% N; k: t: G  i3 E0 `; Q* H  @4 k$ E

    ! y0 [+ E6 n2 V  I) a. e5 Ffactorial(1) = 1 * 1 = 1
    ( H5 L5 @  p# I: f3 kfactorial(2) = 2 * 1 = 2
    % L& k) h1 h  k$ ^, D9 B7 C; w. Qfactorial(3) = 3 * 2 = 6
    * {4 T( \6 k2 ~/ \  rfactorial(4) = 4 * 6 = 24; A$ H% D  f3 V! y
    factorial(5) = 5 * 24 = 120
    9 f$ D3 O( R1 l; `: s* G% j0 m$ N3 p( ?
    Advantages of Recursion
    " |* i8 ]& T+ D4 \6 t* q
    ( i1 ~: f- m: d1 {    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& ~  ]# K/ P

    + c4 p5 o( l& h5 E  c    Readability: Recursive code can be more readable and concise compared to iterative solutions.
      x$ @' N% A7 P+ M# r, Z
    ' k1 F" i( D7 x/ W. F, v7 h+ aDisadvantages of Recursion
    5 ?9 i" v5 x8 h5 o  Q( k
    & {- P" d  D# R3 \- Y% ~5 m( P3 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.) Y0 p" u* s: J& m7 G1 E% E3 n

    2 e; |: Q* x4 z  f3 `: e    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)., |% S/ X, J* ^
    / a7 C( i6 i8 {
    When to Use Recursion
    8 z  y8 e) d( G9 v8 ~8 L: r" H% T9 s+ @* q: ^3 K3 s
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    - R1 {5 I1 d/ T2 O) G5 V
    0 `# s7 `7 z8 U# H; k7 I    Problems with a clear base case and recursive case.
      u) r6 |- m" E4 F8 Q' `) T
    ( S6 t( p: `, Q5 [; J- BExample: Fibonacci Sequence
    , ]" t5 ~' h, I* \. E) ?' V5 V! Y/ X$ |8 O
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    % q* P0 D2 s% b) P. n) R7 m7 v, Y& k, L# R1 K
        Base case: fib(0) = 0, fib(1) = 1
    5 ~/ M5 v1 k$ _( i9 d& h/ r8 B! S; Y0 _
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ |. T8 m0 Z$ {) Q" ~

    , D: z5 w( H1 X3 e! spython
    7 m, I! N8 _0 N* D/ t
    7 M# n0 D, ]  O3 b* T7 y  q; v2 l0 N4 Y$ c- }6 i- n0 F' {& b
    def fibonacci(n):
    + H: g) h4 W2 k    # Base cases( H6 q% i8 n" {# E* a: z4 {
        if n == 0:& B" @) v( l8 K# u
            return 0
    0 k5 I0 v  i. j3 I  G1 b    elif n == 1:; |4 P/ c9 T& \, m( J0 C
            return 1
    ! X2 k+ I- o8 a! u5 b" F' ?- o0 V. y* V    # Recursive case
    2 \$ s& G3 h* ?' f2 m, l2 h    else:3 V+ ?. O, D3 o
            return fibonacci(n - 1) + fibonacci(n - 2)
    1 o. X; v; P: ]7 V& d2 s1 P+ [4 j7 C0 Y; Z& p; q
    # Example usage; y4 v- I" v2 j
    print(fibonacci(6))  # Output: 8
    6 X& r8 E' K/ A. D# d5 Y6 o# Y+ z
    4 m: E# }; [) m1 ^  O7 p7 ^9 oTail Recursion
    9 \: D2 _9 P. z' _/ d, k! z7 x, |4 m" N' Y- A+ a
    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).# i, S4 r" G1 i0 w$ X3 V" A5 a# H, V

    + r( ]2 G$ b/ `2 q* d! n- E* BIn 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-5-13 15:19 , Processed in 0.036878 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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