设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 Y( W* }) l! g9 H! @/ q
    * l( X2 |5 B' Y0 [% o- n3 ^4 ?) a解释的不错7 Q$ b# b4 o: o

    2 p0 ^$ a, s; y" j+ ]: B" E递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 I6 o) b) J! B3 h/ R" o

    ( M  |- [7 R( N* K 关键要素1 x# r0 R/ @- i! y
    1. **基线条件(Base Case)**
    0 j' ]1 b. ~0 d' a+ D; F   - 递归终止的条件,防止无限循环
    + _9 M( w$ E" E! b' A9 `* |( m   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    3 f+ u! o8 S( \1 n. }1 W6 {- ]8 B: n9 C9 f& L' ?* e
    2. **递归条件(Recursive Case)**
    , ?5 I5 q& n+ b4 W+ L/ t   - 将原问题分解为更小的子问题
    ; v! j. U! @  t8 X   - 例如:n! = n × (n-1)!' q( V* z5 }* s: E

    : M* m6 b: Y2 ~. Z 经典示例:计算阶乘
    ' }! ^, n$ U& O" a' cpython
    / z2 _. ~, Z3 C8 D& Cdef factorial(n):! j) @" @$ d4 }- t
        if n == 0:        # 基线条件
    . R4 k: a- s3 r2 R2 c# Q        return 1
    + T; N. ^7 }4 |. O    else:             # 递归条件
    . x  U: {  g9 w, R5 E4 `        return n * factorial(n-1)
    7 \0 M( C+ p3 m: S% [" Q执行过程(以计算 3! 为例):
    " j* h4 S3 K, Y) ^, e2 i4 ~factorial(3)- y) t5 X- ^. n' f8 `1 Y6 S
    3 * factorial(2)
    , F  W0 J. j5 d% K1 e3 * (2 * factorial(1))
    ! M2 o7 }! U; S7 X1 g, Q3 * (2 * (1 * factorial(0)))8 l, y7 ~: J/ y$ Y. G; z  F4 N% w
    3 * (2 * (1 * 1)) = 6$ F8 u6 S1 H5 x% v+ b2 z

    1 J2 \+ ~  |4 g& o# R9 g& U1 _ 递归思维要点
    . `: }+ O, ?# m# v! g1. **信任递归**:假设子问题已经解决,专注当前层逻辑) a2 e5 x& V. t9 E1 y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 t- z: _) j2 Y3 j. I" w3. **递推过程**:不断向下分解问题(递)+ l" V7 t8 ~# X  U
    4. **回溯过程**:组合子问题结果返回(归)* L# P% L- ?7 L2 c( y0 `5 |
    # g, L+ A# a) q* j+ B/ u
    注意事项
    9 g7 J9 b* b' ~+ W6 G/ R7 Y必须要有终止条件+ U7 @. E0 v# y' p7 x- ^) U
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( H5 j, z4 m- E& g8 H$ b: B. I某些问题用递归更直观(如树遍历),但效率可能不如迭代* J0 R8 v9 ^( ^) o% }
    尾递归优化可以提升效率(但Python不支持)6 y! p8 A9 {+ `  O  z
    5 d" z& z& |* W$ }0 O
    递归 vs 迭代
    ; ?; @8 u/ t2 x. B( ~! B& @|          | 递归                          | 迭代               |4 X4 n4 D  G5 s
    |----------|-----------------------------|------------------|
    ( O- e* |1 C% {% B. `| 实现方式    | 函数自调用                        | 循环结构            |! w. K6 Y$ o# b+ Z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |5 }5 y: J1 p' D) h
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    / Y9 k1 F/ Y' u: y  m| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # g2 l5 @' H1 _% g4 Y4 P
    1 e' f+ S$ i1 E 经典递归应用场景) Z& v  U  n4 ]% `2 B  a4 F, L
    1. 文件系统遍历(目录树结构)2 F* [: h9 G6 |- E
    2. 快速排序/归并排序算法
    & {7 L6 a! A1 E& ^' X3. 汉诺塔问题
    1 Z4 h$ R! C7 m* N  N4. 二叉树遍历(前序/中序/后序)' k; u$ U5 R, [6 Q0 J
    5. 生成所有可能的组合(回溯算法)4 ~) W) S- |  B& A% x
    ; g, J. Z. U) i, K& M2 x/ ^5 R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 07:51
  • 签到天数: 3161 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,9 Y$ t# ?/ ]) |8 x; c
    我推理机的核心算法应该是二叉树遍历的变种。2 r+ v5 S) {, L
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 O; S, F1 \+ K& O, s" P& jKey Idea of Recursion
    ; ]0 S% A- \6 ]% z% K
    # i) W$ h5 z- {% BA recursive function solves a problem by:7 X, m) J5 J7 }, t6 ~

    ; k, l: c0 R# ~/ C7 }' ~: W, L    Breaking the problem into smaller instances of the same problem., }  E: ^& H0 b" H$ l# k3 b) C9 O. _

    * u- T; G4 F- @; p& L( \    Solving the smallest instance directly (base case).
    # ~) Z( A; e/ [$ ~+ w: E1 p  e0 E. ^1 ], n) W: B8 \, Y$ M5 h4 O
        Combining the results of smaller instances to solve the larger problem.
    7 b" ?& R' F- U( T. p
    , s$ L4 N9 j, E" p# O! G5 ~" UComponents of a Recursive Function
    9 I: i. h  N& h2 p3 P9 L1 G& _7 A5 b; O0 L" e0 z
        Base Case:
    0 }- C8 O: n# @7 ~+ \; V! w
    - ^% e" [' P$ a# n8 l( B9 Q* G! P        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 v7 G5 \% s1 ]9 w) {( P. S  i- w+ L1 c; Y. ~, W- v* Z; |* ~( h
            It acts as the stopping condition to prevent infinite recursion.& z* X1 Y) j& u
    # J# H0 x' L4 F, j5 a0 h
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( u! e* f/ _6 ]4 W8 {5 u' K
    2 c3 W1 J/ r( r) m
        Recursive Case:
    & n8 \% S) R" K/ B
    1 N- |7 d' k& ^+ {5 ^$ U        This is where the function calls itself with a smaller or simpler version of the problem.9 h6 Y$ ~4 @3 C6 ]( i* d, F0 u
    6 ?' j/ K* ?0 _) x
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 Z) ~9 F( J/ K/ d' D/ F1 a

    1 X1 ~* h: r  Y1 v- g# K1 i; oExample: Factorial Calculation3 r$ }, ~7 u# ^/ Y0 L% N
    + \" g5 f' X8 H# P
    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:
    8 n: p6 G0 d' g' o8 d& f2 S& Z2 W+ j$ l+ q; t- d6 [1 ^! x; \
        Base case: 0! = 1
    ' R1 p. z6 s1 W. L& I8 d5 a/ j+ E1 s$ l! A3 x! Y6 K4 l3 H
        Recursive case: n! = n * (n-1)!$ p* s+ k7 [8 B. H( w
    ! A3 ~: c/ e' C& O+ C4 w
    Here’s how it looks in code (Python):9 }# P: M7 ^% t4 Y
    python
    0 l5 e& p9 B/ ^; q8 I2 I
    + F; K- q7 {: {0 l+ N
    / w" k1 I1 ~9 m% y8 @def factorial(n):& i# ?" z" X' o2 b! b# _
        # Base case5 m( Y$ r6 h) g: c6 k, V6 A, I
        if n == 0:2 n( s! F. l8 p' @2 S: y! R1 I
            return 1
    0 u  C% Q; }7 I. f8 @    # Recursive case
    - |0 ^  G7 k5 {$ M! w' L    else:7 h2 Q  J; R5 O. r: A2 |3 ^+ w
            return n * factorial(n - 1)9 q, R! A5 K) H8 Y5 B: [, `/ i
    ; N  f, i4 y. D2 }0 F
    # Example usage
    : f* `" ^) j( a6 p% }print(factorial(5))  # Output: 1203 _5 M' V- _- Z! Y
    3 T0 b: y! \9 S* H$ }6 U
    How Recursion Works
    . e: B9 w' A6 i8 }
    ) I& i1 S. [, k' q  v4 j0 v# _- p    The function keeps calling itself with smaller inputs until it reaches the base case." s1 Q- L2 N( Q

    7 `  x" Z3 l8 H- Q5 o    Once the base case is reached, the function starts returning values back up the call stack." r. ~  u- y% g6 f/ X2 F
    2 b$ l) w, [( j* Y) ]) p
        These returned values are combined to produce the final result.
    9 y3 k. z3 n/ P6 o: R" [9 n- C* V, n5 S* ]% i* h' T0 [/ N
    For factorial(5):9 b; R$ I" n7 V3 Y( G5 e. U2 A# P/ T

    8 Y3 I' m- [. `5 H1 n/ ]% r+ L1 `9 @% _* E( B$ J/ M
    factorial(5) = 5 * factorial(4)  j" {7 L7 c! m8 F5 l+ E$ ~' f
    factorial(4) = 4 * factorial(3)
    : p" Y; g9 B+ Y' P1 wfactorial(3) = 3 * factorial(2)7 E0 Q6 t5 v7 N9 t: V8 S# w
    factorial(2) = 2 * factorial(1)
    5 U+ V+ H4 `$ ^5 |/ @! dfactorial(1) = 1 * factorial(0)
    . t5 w# e$ t* A; F; Yfactorial(0) = 1  # Base case7 h9 |% Y: J( ~! T! D
    2 Q6 w. E. S/ ?
    Then, the results are combined:
      O" O  I( r5 g+ p7 m! i
    # H9 @. n1 h; V
    / m, e7 F: o3 [( k" r& sfactorial(1) = 1 * 1 = 1" n* x' F; H3 M! W
    factorial(2) = 2 * 1 = 2
    ' R2 _* V. }+ zfactorial(3) = 3 * 2 = 6, i- s+ g, I0 E) _! ?& \
    factorial(4) = 4 * 6 = 24
    2 k) X4 m2 Z. Wfactorial(5) = 5 * 24 = 120
    - }! C* s2 C3 `4 ^+ d
    8 m8 K4 K' U  d3 a7 E# u! h6 LAdvantages of Recursion  M# {0 [% s+ H* A; c4 u, t

    & o8 m9 ~, w; K7 f4 [    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).
    8 i9 h) ]( [) \5 T- h9 o/ m/ ?# N9 T+ U: C: Q7 ~. ?' h
        Readability: Recursive code can be more readable and concise compared to iterative solutions.9 c* m3 ]- I' f  n0 v  q/ u
    7 }% y6 G7 X, l* c$ m9 S" P6 R
    Disadvantages of Recursion
    1 W+ M$ E- `" e* R+ A3 V8 }8 |! Z
        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.2 n% a$ c6 F# Z9 ?* b

    : R! m9 u  ~9 |4 U9 d' S) C; R    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    7 X4 `7 m) h* Z: M7 L, s/ Y$ ?- H7 Q* d
    When to Use Recursion/ ]# B; P% P  _6 {( \, g
    : g) x5 P7 D0 d6 U
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).% \/ A8 ^9 ^- g( F3 L
    # u- C% z4 V) i
        Problems with a clear base case and recursive case.
    0 t# e; J5 ^; h3 g- z- Z7 x3 h' l- T+ e1 m- h; X, J0 o$ d9 h
    Example: Fibonacci Sequence. Y. D- \0 v0 B2 a- W. G2 `( G
    ' Z6 {. y+ S! D1 ~
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:$ F4 `- |4 ]. c  `  c
    ( B* E4 F6 [( n, T( z
        Base case: fib(0) = 0, fib(1) = 12 @; k+ ~  ]: [) ]
    / W/ s; r6 F3 e9 @* \* ~
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    1 y* F. l' ?' P1 v2 S4 ]3 N! p
    ) v. i/ M8 n* Npython3 k' K) D* ]- ^8 }0 t6 m3 A* D
    : M3 |+ c2 O) K) z( G

    " T, k# Z  ^# S* r- [3 G' P" o' D: bdef fibonacci(n):; V# b( K. a% M- g* B
        # Base cases
    # g6 q: G. W" J1 u    if n == 0:
    6 h/ \: Q: `* i        return 0
    " k( w% Q% ~0 O5 \+ C    elif n == 1:
    3 M: A: B  }" U. \* \/ [: {5 A        return 19 E# b  l/ D, |: C7 L" b2 @* s
        # Recursive case
    * A: ~4 y  _8 o% ?% r) U" l    else:
    3 G9 A/ ?, G2 \& u- T7 X        return fibonacci(n - 1) + fibonacci(n - 2)
    - e" w; j6 O7 N& {9 H! Y0 X( C  a- d: h; A* a2 l
    # Example usage
    7 \5 ]5 L! {6 V! T; {" A$ }2 K. oprint(fibonacci(6))  # Output: 8* b( D  w2 Q, v3 u& Y( I

    0 }0 S4 [* x$ RTail Recursion
    6 F0 e5 k8 s: {! \- X2 l
    * w3 E1 O; a9 v% z$ m1 sTail 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).8 o& E0 ^# T' C2 b! k5 H* I

    % {3 Y. [4 f4 ]3 O. O# JIn 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-2-3 07:37 , Processed in 0.058476 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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