设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ' _& b% @, j/ b, H! Z. m# h
    # [0 [4 W4 X/ |( X1 b$ f6 R4 Y. M解释的不错1 X/ P8 l% Q4 L" _! Z: g
    5 E: X8 z" {. k# s/ b/ O+ s& \
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' ]3 J8 s1 O3 W9 E# n% t) n  v
    1 n( Z* M8 C1 ~& n
    关键要素
    $ B6 y% s5 R4 d1. **基线条件(Base Case)**
    4 p& q8 Y2 o" O) P# h4 s   - 递归终止的条件,防止无限循环
    6 n) k+ C3 @1 K9 R- C% O# T) s" B   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    * Q  {' [% Z; E2 D1 E! _5 K) X: v2 W
    . j3 }, T: m4 c2. **递归条件(Recursive Case)**) ]* m) q9 c" F+ r6 E# X6 w
       - 将原问题分解为更小的子问题' Q  |( p7 |5 _0 J/ V
       - 例如:n! = n × (n-1)!% f: Y0 e+ p! X: u- s* V$ Y

    7 k0 Y5 ~2 A3 l/ p: ?$ }& C 经典示例:计算阶乘
    + f3 B; a& D: _% C/ P# ?python1 ~3 G* P3 F, h
    def factorial(n):6 j$ ?" W, X$ w
        if n == 0:        # 基线条件1 e3 E* y  Z: ?' r" Y& Z
            return 1" t: O1 N, M# N4 G
        else:             # 递归条件' j5 S" O) i8 m# |
            return n * factorial(n-1)
    / U4 z+ s2 \/ g8 O执行过程(以计算 3! 为例):
      Z7 d1 j# M2 W" k- i3 r$ z) |' ~factorial(3)
    9 C  s- X0 Y1 o+ x2 p( M3 * factorial(2)
    . j# J, d$ Y5 u' }4 u# D0 u  j3 * (2 * factorial(1)), f; K: i% C. A+ h# ]
    3 * (2 * (1 * factorial(0)))( [* n  f6 b9 e/ L' ?0 W! V
    3 * (2 * (1 * 1)) = 6. h. h" ]2 F; G; V3 g0 h

    8 P# H7 C! _# n! I 递归思维要点4 [/ W+ f6 S, q" l8 j% d
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑! ?* O+ S* ~* e/ V$ t+ Y0 N" j
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 D! U8 K' |1 I  w+ a, W8 c; B: k3. **递推过程**:不断向下分解问题(递)
    " R( y7 D; \  K5 \# D4. **回溯过程**:组合子问题结果返回(归)' P5 e# s% z/ b' j

    1 Z0 b! {$ e3 Y* t 注意事项
    1 T9 p2 C9 E) }1 }5 g必须要有终止条件
    6 J5 I8 S$ O1 J/ i递归深度过大可能导致栈溢出(Python默认递归深度约1000层), }* X. i' h6 z, n; n6 r
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 `* {/ m5 a% X5 O$ g尾递归优化可以提升效率(但Python不支持)' }% i( w6 u" ^0 D

    + G+ {6 }5 C% f8 ]0 S+ N- w( N: I 递归 vs 迭代
    9 R9 ^* Y. m( K4 p& E9 d9 i|          | 递归                          | 迭代               |
      A5 z- \$ P6 a$ x( ||----------|-----------------------------|------------------|8 i/ J2 A. o( N6 ~4 ]7 e
    | 实现方式    | 函数自调用                        | 循环结构            |2 B; j0 i5 G( t* |
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ }6 P) O2 l$ i. S9 }' ?. {8 x| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    9 c# c: J, J! y- W3 y| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ f% b0 m4 m" g
    & d5 E! W! x2 e3 _+ ?$ h
    经典递归应用场景
    # u4 i" C: D7 e, ~+ m: Q0 Z1. 文件系统遍历(目录树结构)
    ; ]" S( G/ j. G# L5 O2. 快速排序/归并排序算法
    # h7 N0 V, j& h  N: J3. 汉诺塔问题
    2 [6 ^  S% t! `7 d4. 二叉树遍历(前序/中序/后序)
    + l5 `1 X: l- j- {& f5. 生成所有可能的组合(回溯算法)
    * ]8 r9 ~3 k7 r2 i1 w+ x" H8 M, ]( p. x+ \. @; Y1 V1 F% {* `
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    16 小时前
  • 签到天数: 3115 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. G! t1 c" c1 m( C
    我推理机的核心算法应该是二叉树遍历的变种。: W6 C! N" j% p- {, O4 Z" i' |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    , n2 \1 ]5 G3 G+ c. J' Q4 KKey Idea of Recursion3 x1 t; F/ E( u0 u( X
    " n1 b) R3 K3 L; y3 J6 K
    A recursive function solves a problem by:
    " g: C  d$ I# u& H3 G( |( S% U( [, ~2 ?
        Breaking the problem into smaller instances of the same problem., H* }% Q7 |; a) j0 E* n/ A( o% f3 n: B
    ) G$ \. {& i" A/ l! N2 P8 \
        Solving the smallest instance directly (base case).
    / k* P: L# Q7 Q* ~6 h* C3 n2 y/ i# `
    0 g" e1 X  G3 j* D# O    Combining the results of smaller instances to solve the larger problem.' [3 q, N! E/ R9 A/ w+ r9 p
    ! ]* P# O  h2 ?8 P3 F2 s
    Components of a Recursive Function& H5 _6 Z+ A5 P# E1 K" q8 k# A

    , G* v' C. t9 g3 O    Base Case:
    * a0 U: E, e* w
    ' i. L% M/ t* R2 O/ V        This is the simplest, smallest instance of the problem that can be solved directly without further recursion./ G+ o$ h5 D, r- x

    ; m6 X& N8 s; O! K, t) H        It acts as the stopping condition to prevent infinite recursion.
    4 P8 t3 a% p* D8 F1 V- `  |7 u  D% N! b" P
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    / \9 S) o8 A  _% p' x
    1 J" s3 {0 T6 ?+ h, l( u    Recursive Case:
      `9 {: T1 [0 M
    " C* f) b8 Y9 A( T. ~) X        This is where the function calls itself with a smaller or simpler version of the problem.7 z% P: o& P& U' T3 g2 s. p1 ]) n8 w
    & G4 U/ X5 F6 l. }. _
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    # J9 V3 M( P% V) o0 {. U
      B/ z# v3 p/ g5 O& V5 q/ O/ sExample: Factorial Calculation
    * A7 b9 h' E- t" @$ u* R0 k( u) ~2 b2 l' a: g6 k8 `5 e
    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:
    % p, Y: H6 N" }
    & s0 C$ _/ A2 p! q7 i* d$ G6 f    Base case: 0! = 1
    & A- D4 b" `8 n  r. i: m
    , ]$ _0 x6 T3 l! v- W7 d$ L1 O    Recursive case: n! = n * (n-1)!
    3 D* f& ^! J5 u" M3 N: m9 L/ Z0 H% L6 C; N7 X" s9 W5 u! F
    Here’s how it looks in code (Python):4 {3 N& f& k2 ]8 p
    python8 R3 ^" \$ T6 @5 Z- e

    3 N2 t; E/ ]; H* T
    $ V) [7 x2 N1 b; rdef factorial(n):
    - F! C8 H4 o3 W% b# _    # Base case. Z7 W  F& V$ M% S
        if n == 0:
    9 B  Q. F* n4 ^2 k        return 1
    " z$ E* T- l$ {/ g2 g    # Recursive case
    % ~8 ?; w! A# C# c4 |    else:3 L3 b1 _0 [: L7 S0 q0 T
            return n * factorial(n - 1)
    " U$ W8 u. J- r& X
    5 D, q0 M+ t0 Y# Example usage
    & X" e0 z3 F& d, P# Mprint(factorial(5))  # Output: 120
    * G8 U8 G; `5 r: ?1 X1 B' C/ O2 }) n7 s! u
    How Recursion Works% T9 ^5 D( L: r+ S2 Z' X

    - M( O0 Y" m9 g4 O! I    The function keeps calling itself with smaller inputs until it reaches the base case.' L  m4 _/ Q: Q8 o
    9 e7 [( }5 N) G9 G$ A' u+ S2 T
        Once the base case is reached, the function starts returning values back up the call stack./ J; j  `, a; D. N7 o

    ; D, P! k7 i; ]& C$ T( h( Y+ E    These returned values are combined to produce the final result.
    8 {0 J4 U0 ]5 A: p/ l; d. W4 L1 P3 m* e' G5 q  t: Q: [6 f! j
    For factorial(5):
    ( h9 [& l' o! F$ x4 w) T; ?) W3 N- B/ l( \$ K" U
    8 d$ D9 N9 i+ x" K% O: p
    factorial(5) = 5 * factorial(4)
    % j2 a9 x# Q: F& E" R1 Ifactorial(4) = 4 * factorial(3). J; X6 |% `' C$ a; f
    factorial(3) = 3 * factorial(2)
    9 C; u: B. o" f9 Qfactorial(2) = 2 * factorial(1)2 s* u2 K' k. B. s% T
    factorial(1) = 1 * factorial(0)5 M2 E+ C2 e1 W; K
    factorial(0) = 1  # Base case
    $ x  P7 R' N4 F0 o/ r
    - g  o# M5 i- P! c8 IThen, the results are combined:
    3 V' R7 g9 J1 o6 S8 D
    1 L) D0 L$ K; p: [% `8 J. P4 A0 D; z
    " r, `4 i5 Y6 T& i* X: B. Afactorial(1) = 1 * 1 = 1
    , i; x- q9 M; n4 w5 B7 O8 \  N, D! lfactorial(2) = 2 * 1 = 2
    ' n. L8 R. |  h8 pfactorial(3) = 3 * 2 = 6
    # Q5 _! H6 g, W" ]; r9 yfactorial(4) = 4 * 6 = 24
    , r# ^* M5 J* wfactorial(5) = 5 * 24 = 120
    9 j3 R* ~, c- D& q- W9 C- _$ A: Y) j5 y% l
    Advantages of Recursion
    3 W  S, R: \) F0 s! F
    & y+ s+ _  v3 Q% a2 K2 p    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).
    $ d) n' b4 i* o
    5 D; E3 h  _4 b* b; s# F    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    + P% X+ p' H4 K; V5 d0 j/ h+ g* v( t4 p! Q% {" _
    Disadvantages of Recursion
    1 t' d5 g7 y7 L, X' O& b, Q8 ?3 |3 o* m" 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.
    + c" L* a# ?- p1 B
    8 c& N7 R7 ^; E: m3 ^) b    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).; P( `6 B: l8 P* x
    # \0 b0 N  a! h" O- \0 |
    When to Use Recursion
    " j( P( J" H1 M. v0 C
    / k* b! |1 p$ x    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    , l$ [9 |( V: H1 P
    ! N% A5 V$ t+ m& g; K9 |% G    Problems with a clear base case and recursive case.# Q3 U: I7 C, L$ V. y$ g5 L
    ) z' [$ ~! o/ ?- f/ u
    Example: Fibonacci Sequence4 b! K* E* d$ g& c9 ]  y

    ( q4 Q8 F; R/ c( b( P, yThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:2 G3 b5 y$ V1 p: x
    # s( U+ s* j* f! x% m# u
        Base case: fib(0) = 0, fib(1) = 1
      \. N! M4 m8 X( |7 I$ \
    8 B  r! [7 W: ~, I9 e. M) ~2 F    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ; [$ f1 j6 E. V6 p  p2 g1 T: e5 ~
    python
    1 W5 I9 H- }, Y- ?% N5 P' r' P; `3 `! t6 O: {9 ~* L

    7 \# `5 L  S5 h9 M4 U/ ~def fibonacci(n):$ d6 j4 k& D1 @2 b* v* w
        # Base cases/ |6 n. G. N) j
        if n == 0:/ t4 H% R5 c* @: y; s5 S
            return 0
    % I6 m8 a2 H2 V8 E! W    elif n == 1:
    2 @% g6 Y, u7 Q9 E2 G        return 1
    % N3 q; V7 x3 D    # Recursive case
    5 g1 q3 B* W3 d% Q- N# ^$ H6 j- E    else:3 x+ R. r' _( D0 J5 w! I
            return fibonacci(n - 1) + fibonacci(n - 2)8 M) P: `+ C! o# U! m/ W
    8 W( _) T3 O8 C# [! |
    # Example usage
    ! b2 i  {0 o3 Uprint(fibonacci(6))  # Output: 8
    ) e; A: s$ d$ @1 B! n/ t+ I: L$ u/ q( y
    Tail Recursion
    . w# x! B' ]  E7 M3 W8 D9 s; r, I8 ?% W; Q
    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).. W# p& s; ]+ @

    + s: Y* `& y5 I* t& pIn 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-12-11 18:02 , Processed in 0.035237 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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