设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 4 |! ~' i' W! f% K; @: N4 b, {
    + ]: k: @) ?' }
    解释的不错
    ; @% M3 c2 ]5 J! O% D" P
    & ~% l6 e5 O6 C  E8 b# ?递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% @$ I! r  ]7 a( `+ s0 c5 K
    8 @+ `, G$ t; Z$ D
    关键要素- d- p' I7 k/ a! R
    1. **基线条件(Base Case)**6 {; s) E6 [& H* S* J* X. E
       - 递归终止的条件,防止无限循环
    2 g! D5 V1 G1 n1 P0 i5 E# L# }! X   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 T" b* S. A! ]

    8 V' Y1 K+ y# T/ m: c2. **递归条件(Recursive Case)**
    " p9 R  }9 p( R) T0 U7 T, [   - 将原问题分解为更小的子问题# W. H4 D# h, D7 {) s
       - 例如:n! = n × (n-1)!
    * ]; l0 O8 K' D* `6 A, B
    - J( x0 z$ z/ M6 d. ] 经典示例:计算阶乘( r  L& n5 k( e: v6 Q" {. [8 U
    python
    / y/ p9 I9 V5 P) i% |def factorial(n):
      \, N+ n0 z. Q2 H    if n == 0:        # 基线条件
    4 U/ Z3 C+ l! ?% Q+ z/ S. l        return 13 {5 M7 Y. c# J, C" A8 u: u
        else:             # 递归条件
    4 n6 F! V! k) p7 i        return n * factorial(n-1)8 \5 p) s, I5 M8 ^/ O: o
    执行过程(以计算 3! 为例):
    1 p  q# s2 H/ v: m% Gfactorial(3)
      u# b5 Q" }' a# y3 x0 v* U( A! {/ k3 * factorial(2)
    ' E, G! e8 J/ l# M. T5 ?3 * (2 * factorial(1))
    4 d* c3 w2 Z7 ~' p2 b3 * (2 * (1 * factorial(0)))1 B& e; d4 a: k0 a3 K5 R% s
    3 * (2 * (1 * 1)) = 6
    7 x9 }# ?' `2 x5 \* J. s
    " ^0 b- x. p3 v: Y2 V/ J: ?! e6 v1 d 递归思维要点: J. z; R4 _, s+ L9 D0 K) n; l  M$ R
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑, o" ^7 |+ F# R0 [: N6 D% y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - s) k& q  E, r. W8 k3 z3. **递推过程**:不断向下分解问题(递)
    3 g* S" v/ V, O* r4. **回溯过程**:组合子问题结果返回(归)3 \) u4 Y" w& y

    9 B2 ?% m' f% L1 @( I. c 注意事项9 ?7 x* l% t6 c$ z9 u' Y0 O0 D
    必须要有终止条件
    ; G2 i& B; E' n" A# N2 k递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    7 u2 s+ F7 l6 d& a  |- d9 k某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " ^! u  k4 u$ s尾递归优化可以提升效率(但Python不支持)
    $ Y: i( Z. [8 }3 _- F' D- B: G2 Z- l% _* p8 }1 C" i
    递归 vs 迭代
    9 n6 c7 c# l& E+ Q|          | 递归                          | 迭代               |7 X! M0 a( [3 I* t+ Z
    |----------|-----------------------------|------------------|4 ]# ~  Z0 V# e) @! I5 N
    | 实现方式    | 函数自调用                        | 循环结构            |) W, z! K/ }: d8 c; M+ q6 T9 I
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 D/ k4 D8 M- y# O! n. v+ Y6 t6 k| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |4 y: c9 Z1 Z/ M+ o3 r( e' Z$ B
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |3 y9 f+ Y" J, y
    , t# h0 |( u' u+ _  `2 C. Z: _
    经典递归应用场景
    5 w4 F( `  J$ s5 x/ o1. 文件系统遍历(目录树结构)0 C: y; ^, L$ y5 B8 {- K/ M/ X
    2. 快速排序/归并排序算法$ {( n9 G0 o, o8 f
    3. 汉诺塔问题
    3 O7 t* r# y. ]* l! z4. 二叉树遍历(前序/中序/后序)7 \8 D0 N# s; H+ p+ }) q
    5. 生成所有可能的组合(回溯算法)0 e: u: f& Z" z8 A

    1 p6 h6 `/ w+ M. t8 T0 d试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ i( E" j1 Z7 G( B( R  n
    我推理机的核心算法应该是二叉树遍历的变种。
    % B1 y$ c( W; O: m另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 h) l5 s+ y; uKey Idea of Recursion
    ! G8 n0 j6 H$ ]! a
    $ ?2 ?& g2 @9 K$ ~5 ?A recursive function solves a problem by:
    9 [7 b* e& r- B; s0 N; W+ Y% u6 H% c/ b
        Breaking the problem into smaller instances of the same problem.
    7 b) F5 E6 ~! o. @" f" r# X2 \0 c+ s
        Solving the smallest instance directly (base case).. _0 x) q9 x3 e& l; y9 V

      y$ \8 ^! p5 d. ]    Combining the results of smaller instances to solve the larger problem.% r* O& H' N' W( W

    5 }! V1 U. y$ a! H1 ^9 I! }2 {Components of a Recursive Function; c4 ~# w- N. s2 j5 ]

    - D* N# [/ P9 D. w! t3 x    Base Case:
    * ?9 Y( w7 C2 U# J; s9 W* s7 z  k4 ~2 _" X; f7 F& f, u: v' }
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& g8 U2 _# ?9 W& c( `

    # K% U# h9 g9 e        It acts as the stopping condition to prevent infinite recursion.
    8 p) Q& Q3 x3 w9 O
    ' E& A0 n4 D8 R" H6 R) D        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # J+ V! J6 A9 J0 q1 ~; O' k
    7 a1 g: g5 s, f0 Q. o9 m    Recursive Case:; Y& a/ M5 R* [$ j
    ; c$ a0 p* I2 x* P% D
            This is where the function calls itself with a smaller or simpler version of the problem.
    : G% Q" L3 M7 W9 x3 G
    # H% Q1 l- a% N        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ y5 S% N0 O. z# J

    0 B/ D, }0 ^, x! F* O  iExample: Factorial Calculation& ~7 |$ ]2 m9 Z

    # T6 e( w$ O  J  \: R! ?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:
    0 D) ~, J8 c$ m0 b4 z+ p5 L
    7 E- H" L  z; j0 s& y    Base case: 0! = 1
    + b1 y9 Q* H% G* [/ Z; w# y
    * l  _8 \# V& S3 [1 d3 @+ g( w1 M    Recursive case: n! = n * (n-1)!
    & \; }! y7 Z, R3 e5 o# H* V9 z
    Here’s how it looks in code (Python):4 {& X0 U  C9 E+ c4 I: d4 z, y
    python% ]4 Q9 s7 [' t
    / k& C5 g, q+ ^$ ]3 G8 n3 R% \

    9 o! u$ H4 Y& B. |  i7 zdef factorial(n):
      X. U8 V( Q% r    # Base case
    7 X2 h5 ~+ Q: W, v4 u( K+ @    if n == 0:( O" Y& E. V- D; ^- w; }$ A' g
            return 1* r/ E: `6 H6 p* j
        # Recursive case- j: F) x- c  K/ [( U
        else:$ v! R* }3 x; R6 x' {5 N7 @8 c
            return n * factorial(n - 1)% Z2 J0 n0 H7 Y5 o+ ^+ n

    ' R1 U6 A1 `; \0 o: s  t# Example usage
    " w5 S$ Y- g2 T  sprint(factorial(5))  # Output: 120. t0 z/ w# n5 }- a

      T5 z3 s' ]7 v- l+ V0 cHow Recursion Works
    - \2 r4 I5 U2 W, N& @/ f1 y- G+ j& b2 X% h+ Z. g$ e% q& G
        The function keeps calling itself with smaller inputs until it reaches the base case.) X/ H" K2 j  x7 d  k$ w; X
    : b7 Z$ F) z- S- t# t5 h7 V
        Once the base case is reached, the function starts returning values back up the call stack.2 p6 i9 v0 O  }# x/ a2 L
    . z  [4 G% W2 b0 D. z
        These returned values are combined to produce the final result.3 _! D/ R' L2 J% _4 x6 H

    ) I1 G* J4 b$ {# x) {$ nFor factorial(5):2 `5 M/ w9 K9 Z
    2 G) S) w" N( Z) e9 d7 E; V4 I, {5 [
    & `& u8 v! h* Y4 _5 L( O1 }. @
    factorial(5) = 5 * factorial(4)" U/ P7 Q, h$ [. z
    factorial(4) = 4 * factorial(3)
    0 j! W1 H& J( f$ G% M. }factorial(3) = 3 * factorial(2)2 B# n8 q) N' Q$ @/ p% A: y
    factorial(2) = 2 * factorial(1)1 R( k. h& s2 O5 |; O4 j) x# [$ L; @2 [
    factorial(1) = 1 * factorial(0)- t  f& c& _, x% ~
    factorial(0) = 1  # Base case9 E( I& ]; b' }/ N( `
    % o$ A8 W3 [# T# I- I
    Then, the results are combined:7 [0 f% o$ \9 o+ e2 |! `$ i
    + f! `/ [  i% B

    6 I$ X" ^0 _5 m, U+ e1 h6 @) vfactorial(1) = 1 * 1 = 10 m: d/ @/ ]5 o; f, h. ?
    factorial(2) = 2 * 1 = 2
    # I% ~3 v, I2 Qfactorial(3) = 3 * 2 = 6
    7 k2 K2 q8 Q0 t5 k  |factorial(4) = 4 * 6 = 24! d7 V. T2 N" n' L3 m! M
    factorial(5) = 5 * 24 = 120. n$ r( X1 K( e9 i% D$ h  D

    % i8 k4 ?1 V1 {, h- NAdvantages of Recursion
    / J* n: E: Y% W
    ) o8 {  F' l: @% m3 s: b5 }    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).
    , U, w) f  G; F4 M, T4 t- q
    2 |* ^" n8 i+ z9 W/ J    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    3 t! b; i8 h1 K( l) ^9 _2 t, V0 J+ V) V
    Disadvantages of Recursion# y# }) i7 U0 n0 [1 T6 W* s

    0 @8 d; p7 B& L  |1 k% F% n& n  l) 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.
    % H, e' h) c! e# k0 F! y" q! j# \6 B# x
    9 Z2 Z* ]/ |( o% P1 r    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).  j* {4 W; z6 D

    , e7 j" w) G! m# GWhen to Use Recursion; T. W( R$ g" n' o7 G

      B2 X+ Q* H3 i    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . u9 ]" V* @6 {/ S! b0 b5 {% @" Y$ q8 k3 l# B7 a$ W  ~, C
        Problems with a clear base case and recursive case.0 d/ c5 k4 a$ i( Q; @) @" {; B

    ) {2 L4 E" ]) o& ^9 m. f/ J; [) {Example: Fibonacci Sequence
    6 M# i1 r/ Y4 X- u7 S, W6 s+ i" [
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # W1 ?. w0 L$ a1 V5 y, ~6 ]* S3 L$ z
    + C% f& `! A, L- y! Z2 C8 ]% l    Base case: fib(0) = 0, fib(1) = 1- b% z) E, q* o- H: b4 k, p
    : Z; k/ H0 Z2 N6 L" c
        Recursive case: fib(n) = fib(n-1) + fib(n-2): ]3 x( y8 s; {9 N* B% h0 Q5 l- D

    , D: ^) I# |) _0 upython2 _0 y3 W) y9 _/ w3 `: _) T
    8 X6 [/ w0 a& {" x

    4 q, b- N/ W- `' X4 Z/ L1 E& sdef fibonacci(n):
    / o3 g8 ?% D  M* k% T& k    # Base cases  r% w* G, h5 V' f3 C7 `7 O
        if n == 0:8 x$ B1 r- u9 B4 O9 p5 K2 |1 }
            return 03 d) x' K! y) O) Y  O6 j
        elif n == 1:
    # `  H& S" L. d/ w$ I        return 1( ]- C5 C6 [- N% F8 n+ r+ Z
        # Recursive case
    5 Z' u# e% Q' r9 r1 f+ o    else:' q& {- j+ ~4 G, E' U- R
            return fibonacci(n - 1) + fibonacci(n - 2)9 z5 J1 U0 i  q& p0 S

    3 q- h+ Q2 i; w# Example usage
    / }/ b3 M, z; bprint(fibonacci(6))  # Output: 80 T4 l6 f$ a7 a" Z. t
    % I- T6 @3 i9 \! {$ j' V- v7 L
    Tail Recursion" y. o4 C  @. z& n8 S
    ; s) P; f8 F3 {
    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).
    - g) ?& D) X0 S7 S+ w0 @7 t: g: k
    ( X: P; [+ N$ A, QIn 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-5-23 18:47 , Processed in 0.069085 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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