设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ! R* I( h" W+ Z% S% m  B6 ~
    % C  c8 {9 Z7 E- C; r: N& c, `
    解释的不错
    8 j' k4 B) c  J- R5 k. s8 f
    + O0 K( `" |6 s, F' Q递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- f3 w5 f+ n; S8 e" G. |% R1 \
    7 z) M0 Z! l2 ]3 T
    关键要素
    0 ?( e5 F2 t/ R1. **基线条件(Base Case)**" ]. W/ X. w' h* w
       - 递归终止的条件,防止无限循环
    & D9 E& d) O6 a- E4 |   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " x2 q$ b! k$ ?4 I8 C0 @" w$ ~6 P% |# `7 ]* x2 M& Q5 h
    2. **递归条件(Recursive Case)**# \" R5 O/ k! P/ _( z7 h
       - 将原问题分解为更小的子问题
    # d% h7 E4 P& O5 C8 V3 A, A   - 例如:n! = n × (n-1)!
    % N" l3 I  `9 P- f/ A; q  X
    8 z# E/ a2 r" R5 @/ Y6 S 经典示例:计算阶乘/ e% R/ A( i" k! o7 P5 `# Q
    python( {# D$ c6 ]  d  f/ |% F2 J- e
    def factorial(n):
    , ~# j+ U9 l1 f. `, c0 E7 L    if n == 0:        # 基线条件
    # S( O/ y' W8 B( S( W" i( d        return 1
    ! j3 D% K/ K; I    else:             # 递归条件) s. g! }, S8 [' h* N" T& N
            return n * factorial(n-1)$ e( x; r' V( m1 G
    执行过程(以计算 3! 为例):8 N: T0 H" p$ V3 R5 {0 y
    factorial(3)
    & n* q6 K) ]8 o0 a* i/ n3 * factorial(2)
    1 g' }  f7 h4 W# z% ]7 g3 * (2 * factorial(1))
    ! @6 Z# E6 A$ Q0 i, |+ w, h3 * (2 * (1 * factorial(0)))
    * K8 D; V+ u, g, N  {) z! x3 * (2 * (1 * 1)) = 66 l  B' U( D+ c) |0 ~, E5 O% z

    ; g3 u! d5 o& r+ t  Q/ a0 r 递归思维要点
    8 u3 L! D/ N, X0 Q1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    - f- o7 O/ S* L2 ?, m2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " L9 p$ n7 y  b+ n3. **递推过程**:不断向下分解问题(递)
      q( K4 m( v/ y# ?4. **回溯过程**:组合子问题结果返回(归)
    / b# K" G: K3 p
    % z8 X% \6 D& w* E# x4 j: u 注意事项" S/ J- L3 y: E4 L8 A
    必须要有终止条件
    ( j. v4 o, E* w0 p: V( L递归深度过大可能导致栈溢出(Python默认递归深度约1000层)2 Q% e6 J  _1 {( P) {( P+ M
    某些问题用递归更直观(如树遍历),但效率可能不如迭代5 B: S8 E! u7 E7 X4 C
    尾递归优化可以提升效率(但Python不支持)
    3 S' c4 D) j, t$ }" t  o; o+ W* c$ G, H- h
    递归 vs 迭代$ O0 v7 [8 g; S8 W  V
    |          | 递归                          | 迭代               |- R: p) j$ n5 d) s- v
    |----------|-----------------------------|------------------|
    6 T1 t) e: a# J$ ]9 d| 实现方式    | 函数自调用                        | 循环结构            |3 j& t" X9 I. `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , Z6 V% Q* M/ A# C  k| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |( Q: o) [* b% W! D) ?% I; ?1 I
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |4 S, l' E8 ^) t0 q" [* J
    ) T+ p1 a" {3 l: ?) V& b- Y
    经典递归应用场景& w, V: q2 K" M2 H4 X, R$ i
    1. 文件系统遍历(目录树结构)0 G3 a1 @7 {4 v: r, K  ]
    2. 快速排序/归并排序算法
    ( r' H7 V% N1 G2 c6 k3. 汉诺塔问题
    : d# T2 u0 \. S* m* u- `4. 二叉树遍历(前序/中序/后序)
    " ]) H5 Q2 n, o# w9 v& p7 d2 P5. 生成所有可能的组合(回溯算法)
    - I. H. V' Y: o1 B, h& O+ q+ h( w( W4 [9 t; W; L
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 08:41
  • 签到天数: 3228 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) e0 ?5 K5 y8 G2 m
    我推理机的核心算法应该是二叉树遍历的变种。
    % [! L& q; O6 x' 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:% \$ O  m! G6 X0 q* C
    Key Idea of Recursion
    + ~$ p6 ~: A' Q& q7 g1 j! e0 j& a" l& W9 j8 F5 W8 ]  G7 \
    A recursive function solves a problem by:
    ; \8 g5 [. y' _6 J" E
    $ K& }, F% }; j' a8 |" ~    Breaking the problem into smaller instances of the same problem.% E6 x, Q9 S6 p+ X

    # q$ U% `1 l# _9 l$ }+ y& d: {% B    Solving the smallest instance directly (base case).
    9 o" }% q/ c- x* s
    ) I* M. P/ Z2 }* x! I    Combining the results of smaller instances to solve the larger problem.
    2 t2 t  l! m2 @) m
    2 o+ R+ v0 a; ?. f3 u/ ~Components of a Recursive Function
    % K& N3 u3 f3 Y; M( a/ p! Q+ M6 \2 f0 p& a" h& e' Z) a
        Base Case:
    . ^( q2 F0 A/ m/ R" b
    : P; i* S8 i" L- g        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & c9 s- \% k# `: B  u7 s5 e9 v/ E# g3 X: D" t1 s
            It acts as the stopping condition to prevent infinite recursion.4 c+ i# n, s4 C6 n& ?$ t
    ! @1 {# V  ~1 O
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.1 [, ^7 I+ H! l

    0 ?3 H$ \/ g2 M    Recursive Case:
    ; A  L# ]! L2 h: G# g; `2 U5 j
    & O$ {( F1 V8 Q2 {        This is where the function calls itself with a smaller or simpler version of the problem.- ?& U! q2 f* J& z

    & z. q. y3 o# I! Q8 P- h        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 I/ G0 k" h& N  \0 A' @2 ]! s; l; {1 C/ v1 k  t
    Example: Factorial Calculation6 R( P  Q' D. L) Q3 W: f

    ; u" s4 I0 U7 ^; {) o, y, NThe 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:/ R& ?( C/ E7 F. n8 z& ]& M

    & ^& }9 ]8 g$ e) W4 I; y* ^- j' g    Base case: 0! = 1$ n1 C/ l) F, r* K: f; B" r: d
    ! N3 O  s8 S+ l1 H3 R; U. ?
        Recursive case: n! = n * (n-1)!
    8 v, A; U! l& c( N8 ~5 j
    ) _& o# `1 i4 z/ F  J  V, jHere’s how it looks in code (Python):9 Y+ @3 Y% D  u! |0 P
    python
    8 p* J. r; ?1 O4 _" q' V
    8 U& \$ c5 T, C$ H
    ( {' L. J$ b" ^( e' `0 }def factorial(n):% `6 _1 x) d! A4 z5 g/ w
        # Base case$ S/ c+ E  d' p; W% s9 E5 U
        if n == 0:
    * I9 y" o* T3 Q2 I$ A  A6 ?/ Z; N! P        return 1
    5 i# c1 e7 }* O1 o+ N    # Recursive case
    ( f1 P9 C! ^& h0 e4 t1 i    else:( t' Y1 U  o- E
            return n * factorial(n - 1)) g$ d# |/ r) n, i
    8 _( s1 M/ r4 D# t" }; u& Y" b
    # Example usage
    % N1 J6 Q  f0 W% M0 Q# p6 J7 D/ Lprint(factorial(5))  # Output: 1205 x* r7 v  B. X) t- f

    1 F' a& j. ]" b& aHow Recursion Works
    + L3 Y/ G4 B9 d4 d/ T9 Q" B# Z, N5 q! q% N: {+ K
        The function keeps calling itself with smaller inputs until it reaches the base case./ ]) W$ R9 l8 q  i% |, n
    " ]0 v1 j0 j% j" j& x
        Once the base case is reached, the function starts returning values back up the call stack.6 I4 k1 K1 V- S3 U2 \% x

    2 c5 o8 p. {3 t% H# u5 L$ n) w9 J" t    These returned values are combined to produce the final result.
    8 W( r6 w( \) ]3 u5 F( U# P% d/ _/ J' h- H4 l! |9 ~- Q- F
    For factorial(5):& k- l$ T. I8 }* s

    + C  O$ j0 q; R" a4 Q6 X9 D  O7 @; [- l) Q4 M
    factorial(5) = 5 * factorial(4)) E) G/ I, K$ P# i6 q. u) Y
    factorial(4) = 4 * factorial(3)
    . j' y- W& T% ~( R" B" o, ^5 T( Yfactorial(3) = 3 * factorial(2)$ t6 d+ O8 {" }8 l  U: M& \
    factorial(2) = 2 * factorial(1)
    + |+ I- z' |# z9 u8 ?factorial(1) = 1 * factorial(0). X) C& j1 q' p: K! ?1 W: Y
    factorial(0) = 1  # Base case
      x7 h! R2 }' Z$ ]5 S) j1 Q- t
    / R' N/ i% H! _; C, AThen, the results are combined:% f6 E6 u) q5 Y/ a: G4 E
      p: P$ U" W+ W( |4 q2 j
    # Q7 }$ E( k0 |& J# Z
    factorial(1) = 1 * 1 = 1* \1 g! m1 Y4 u  @% z; ^
    factorial(2) = 2 * 1 = 2
    . V3 @: I6 K: Z4 }" tfactorial(3) = 3 * 2 = 6
    * A! W+ X! l) E! y) f: T% Wfactorial(4) = 4 * 6 = 24- \$ C4 H& p6 X, @" ~
    factorial(5) = 5 * 24 = 120
    ; [$ W% `. t* p4 G" U! f0 C' H  F2 C" W! J5 r. c
    Advantages of Recursion
    2 Z4 Q/ n% N9 i% P% w% g. H0 ^7 \# n2 Y1 a; U. g' \
        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)., X5 A/ a# Z  a1 e* c0 Z6 S
      r4 s) r9 S( K! ~
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    # g& q7 X/ U3 m& `  w
    4 p) B6 l" X7 G# ^6 b' iDisadvantages of Recursion+ p0 ^- a, p, {

    ! E2 W3 Z+ l: u4 x    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.
    3 D8 p/ ?9 P0 ]7 |+ b/ x) T7 \1 K6 X1 H! }8 B
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    4 u! y, U* e- {  R7 }% a, n* \! [% ^4 g9 u8 l7 p
    When to Use Recursion1 \3 S9 X# e0 v% w

    1 V) X6 p5 }  o0 r8 \    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) S( Y* c0 Z- y, y1 K9 Q/ J; F# v
    ' p* k2 m  }# T) v( L
        Problems with a clear base case and recursive case." H& s, T8 Y$ p1 b' j6 Y
    # M1 H1 p4 a3 n% W7 v% X1 Q6 ?
    Example: Fibonacci Sequence
    # P- H8 l2 B, `9 O* r  a
    + X; Q2 @. D5 Z  A$ |The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:6 d4 Z2 L/ Z8 l6 [- z0 ]5 j; E1 l

    ' U7 g9 ^, w$ x  U    Base case: fib(0) = 0, fib(1) = 10 |& [  ]% m9 d1 d
    ; ~5 R4 M, ], M& M
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    : x5 ~; m# G- q2 n! O, k! D9 S$ [' Y  X7 r
    python
    0 I9 m# h' h) v* \$ ~6 v5 q0 e. I! h: I4 `6 n5 X( M

    " P4 n% ?/ s8 {" ?* T4 I, ydef fibonacci(n):# I, r8 i- X9 [) z
        # Base cases" V" y  X1 T. v* M3 ^6 R
        if n == 0:
    & j  C1 N$ @0 l1 t, D        return 0
    # ]' z6 X) N3 A# l    elif n == 1:
    , s: Q2 b$ \, j6 t5 e1 N        return 11 k2 j' M" C9 _  Q: ^! b  k
        # Recursive case2 R- j: s+ e* q% O! o
        else:( e8 D1 O$ _. v" I. p
            return fibonacci(n - 1) + fibonacci(n - 2)3 S/ K9 N8 p( }7 D
    , l9 Z: K' e1 i
    # Example usage3 _5 B: h' o! A2 e
    print(fibonacci(6))  # Output: 85 d6 Q/ o/ y) S# z; }+ C: a
    6 |: R" U1 t3 @- k: e+ i
    Tail Recursion& b& {! {6 ~' x% F+ a

    $ p: X) x) i# N5 R* N2 d6 |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).
    & K8 F% B- f. G# y1 \: Z% V
    4 ^0 u- X" n( s$ F6 h3 S" 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-5-2 13:25 , Processed in 0.083883 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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