设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑   V2 C2 I/ b0 B+ A

    ! E, S* }4 M5 s$ W解释的不错6 Z* Y/ h7 w! f9 V( s, T5 I

    8 W# p( C. P2 ^/ A& h& T- m递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    1 K6 q% y- F5 s* o" L2 N7 j: G4 V. @) c% J
    关键要素4 V2 {+ A- D) [* f5 y. F8 y0 ?
    1. **基线条件(Base Case)**0 ^. ]( `1 H4 _/ H" [" O* f! V
       - 递归终止的条件,防止无限循环
    3 r( u$ P. a; R4 R: B' n   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# n( O  V8 \( Z( Y
    6 d+ A: ]8 G* s; t+ n. C
    2. **递归条件(Recursive Case)**
    $ k9 x! v4 D- M/ E& a! L! `3 {1 Z   - 将原问题分解为更小的子问题
    * T) }! T5 S* }3 V5 j   - 例如:n! = n × (n-1)!/ z, t" K3 p! Q3 U0 Q. V, ]. W

    ' w: R/ O9 y3 H 经典示例:计算阶乘
    ( K/ F. e3 b3 \% @& Q, ~/ I+ ipython
    ; |' b+ f7 r. v* J4 x& q$ r/ bdef factorial(n):& o7 y. Y/ L2 P- ?5 U& i
        if n == 0:        # 基线条件
    & z# `  R0 z. x5 Z* O        return 1
    ; R6 r. ^3 a! |3 B2 D8 Y    else:             # 递归条件
    # }7 D: R4 u: U/ x        return n * factorial(n-1)0 p% L; \) e3 V- N
    执行过程(以计算 3! 为例):& Q% w5 n3 t; r- l" u, c: O+ }
    factorial(3)
    1 ~2 ]1 N- }3 V: b) n3 * factorial(2)9 I+ g& x9 g$ I4 r) u1 w
    3 * (2 * factorial(1))
    6 k; k1 [# g, W3 c2 \& W: |9 ?3 * (2 * (1 * factorial(0)))
    6 x1 ?1 v2 F: Y# w  Q: W3 * (2 * (1 * 1)) = 6& C1 Q+ y- j2 f5 |8 f; G. E

    6 N; b7 _# a5 [ 递归思维要点8 y4 X" r3 j4 J9 u
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑+ X) G" }0 x! F, t
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 ?  \9 W- I0 n+ Y6 G# A3. **递推过程**:不断向下分解问题(递)" ]: R2 x5 s5 A" S9 o. \# N
    4. **回溯过程**:组合子问题结果返回(归): Z, s% q, E) x9 ]( L* m

    1 B% o( Y; k! c) L8 O( m4 q; J) b8 ?, X 注意事项. h( @1 _1 k0 t  D: E/ f- g/ ?
    必须要有终止条件. p8 A0 S+ u; C7 g+ g4 H9 N* \+ `
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! A0 N: P: `( ^* F' M某些问题用递归更直观(如树遍历),但效率可能不如迭代3 Y! H2 ], @% F) o- Z
    尾递归优化可以提升效率(但Python不支持)
    : R7 f4 Z& j' x9 s& _% h' v; G9 X0 o% W4 z4 V, s
    递归 vs 迭代% A; n; j4 v' s$ }& V/ J" |. N* I
    |          | 递归                          | 迭代               |% l" I: l9 ]% U; S+ {7 q" a* M# @
    |----------|-----------------------------|------------------|' W: G3 C# {' i) I3 N' a  V3 L4 j1 U" T
    | 实现方式    | 函数自调用                        | 循环结构            |
    3 Q2 o) A7 H1 c9 [| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- C5 C/ ]: f8 k7 f" ?
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |2 F& O% |3 q5 {0 x1 h; V0 `
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    : O6 Z3 J2 I8 c) j5 ^  u0 Y2 a1 }7 D( p' B! L- t) s# ]0 Q3 `$ H
    经典递归应用场景) H1 \/ R3 b4 b7 ~3 F- W5 ]/ W0 b4 R
    1. 文件系统遍历(目录树结构)1 R" t& }; F; p  A) K
    2. 快速排序/归并排序算法4 {: V2 l& X! V1 d, v
    3. 汉诺塔问题
    , f* }: H# c7 W9 i$ R1 [0 v4. 二叉树遍历(前序/中序/后序)5 l% {1 E0 f8 e* g( V
    5. 生成所有可能的组合(回溯算法)$ K' E" F. O8 h2 |; P
    8 G( d& Y6 x* l$ k; t7 P4 P" u8 o% ?
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    13 小时前
  • 签到天数: 3179 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 o( I7 }& C: R
    我推理机的核心算法应该是二叉树遍历的变种。
    9 I& A/ S5 {+ \: K另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . `7 J( Y% P; s$ }2 `6 xKey Idea of Recursion
    ; F4 G8 S8 k0 L$ }
    & n% `0 f- ]; sA recursive function solves a problem by:
    2 N' ]! f! n, [8 P+ F0 p' E1 u6 _' o$ T) w: X
        Breaking the problem into smaller instances of the same problem.& d- u1 e. t2 {( L0 m4 _3 G( B
    5 _6 P! i5 ]+ S' K
        Solving the smallest instance directly (base case).
    6 g5 O& g9 d7 N/ ]  h7 d8 G& y6 q
    ! Y% d' q- d# h6 K8 \    Combining the results of smaller instances to solve the larger problem.
    - {7 ^* O& S$ x( ^! p: w( \4 |, a' ]# T5 u7 M
    Components of a Recursive Function0 n; m* Z, I. h& p( {
    - N7 [# r( G$ V# G" i& N7 }
        Base Case:. \* s2 t4 J' H& @2 K" ^
    + U4 S- a# @6 t
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    , n* ]2 _* C% ]" v* V6 P, R/ n, V
            It acts as the stopping condition to prevent infinite recursion.0 V" _' K, M0 g' h5 X
    : [9 R* @" O/ `) b' i1 u7 n  Y" e
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 C' i( c2 m1 g
    2 O, \) l& @6 o, ^* B
        Recursive Case:( r' L, {, p' M/ h/ Q6 W% w( W  P
    0 s, B/ z, Z, z' x2 c
            This is where the function calls itself with a smaller or simpler version of the problem.
    : L. H, x& m$ _
    8 @8 n! H7 A* h/ m& a        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    - U' d( H5 C. D0 b$ J
    ( [: j% K* ~6 y1 _4 R& S# F7 x, z0 EExample: Factorial Calculation7 o  f- w! U  L( h
    : U* r# G! `( N- T
    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:
    + m" U* w$ q6 N, P& p3 `/ ~  N* B
    " X( L& J  G& \$ i5 v    Base case: 0! = 1
    2 l) i& K' L0 p' l9 A6 a5 W3 A. U& X3 s7 S
        Recursive case: n! = n * (n-1)!5 D) w# J2 s+ @7 g7 h/ t' o( M! [
    5 p# r# k- J" V
    Here’s how it looks in code (Python):
    + Y0 \# q- c/ w! z( xpython; G) O3 ^  C/ O- L( b5 T

    6 x6 P# F7 k2 M0 J1 A) c$ g5 I! c8 a. D, `3 e" j
    def factorial(n):
    6 t' Q0 C8 a$ \    # Base case
    2 n/ m4 [2 H. G" C3 h& H    if n == 0:
    , F% `. E# }3 R% N; G) @        return 1
    & k( x* V2 r' X. o$ j$ r1 o    # Recursive case+ K# r- T7 T+ A' V0 k- [
        else:
    % r9 J8 O1 X# |6 Z        return n * factorial(n - 1)
    ! H# [& y% M% ]  h- g1 E, {4 e2 ]* R& w
    # Example usage
    2 q% \0 X4 k, A+ c3 V6 _" x9 nprint(factorial(5))  # Output: 120+ D+ w, q' z& |( T
    % r6 F1 {% K% Q& w9 P: G. ?
    How Recursion Works
    . T) S/ D! I& k; V3 C) ~' _/ B  ?% c# z* D! ?; Y; V5 `9 `, j
        The function keeps calling itself with smaller inputs until it reaches the base case.
    , Z) r* L* F- b$ N" q- y* t
    , n+ U' g; o! U: q9 d; c    Once the base case is reached, the function starts returning values back up the call stack.2 ~6 k* ~3 T% X/ T+ D% i
    . @+ g3 [/ o. G" Z% G6 V" [
        These returned values are combined to produce the final result.; l2 R6 ?" [$ _* Z! {; T+ x/ z

    # R0 Y& v. R9 hFor factorial(5):0 o. }; a7 H3 C. u3 `( F+ r

    0 L3 ^5 R$ Q! P; L/ H* n1 l7 F5 l6 M" [+ W
    factorial(5) = 5 * factorial(4)$ ^# Z5 O4 d  N( M
    factorial(4) = 4 * factorial(3)
    4 A- t5 e; |! U- U' Cfactorial(3) = 3 * factorial(2)
    ' T2 [, s- E+ ?; y( W6 U: O; z7 Cfactorial(2) = 2 * factorial(1)
    : B- c! k' X$ e0 U% m5 l5 e' zfactorial(1) = 1 * factorial(0)
    ! v- G  f6 j! ~5 sfactorial(0) = 1  # Base case! I3 b8 s$ y1 }3 ^; ?

    ) |, ^" t& x9 B; e% n5 L1 BThen, the results are combined:
    # q/ l2 G/ o" [- r- w3 |4 {  ^" J" U. i! d+ ~( J' x% X/ X8 {
    1 @5 I  @3 Z0 ?3 B: Y
    factorial(1) = 1 * 1 = 1
    $ a3 I& W  g' @factorial(2) = 2 * 1 = 2
    7 o) \8 \8 A, \2 R, T3 B$ |0 Q6 O; ?factorial(3) = 3 * 2 = 6
    $ `% v7 i$ `+ a2 k) afactorial(4) = 4 * 6 = 24& I1 u$ E8 }; D+ [' O: g9 K
    factorial(5) = 5 * 24 = 120
    7 h  Z0 \9 `) g6 J( @+ X2 d" p( Y, f8 _( E9 \" J$ t
    Advantages of Recursion
    , x$ ]" k* R# p% U2 @
    & p2 z7 B$ q9 E, v2 }: H+ ?/ D" R/ t    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" h* D+ ]% e9 M9 v
    8 X# a5 V. j- B: V
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    + v1 p$ C- C% R6 E1 z# x# Z: N) Y9 }9 ~7 V1 D
    Disadvantages of Recursion
    6 {4 R# G  b( p; X
    ) U# p2 t, b3 A/ g' @# X$ w; H    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.7 |( V1 A. Y( r3 ~/ B, X
    + y: V2 [, y5 Q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)./ e* N) e9 j4 `
    : N; f4 z$ r% S- i0 |3 \
    When to Use Recursion" w5 E" f. `. s6 B4 z0 o% e

    ; F6 [2 F: I. Q' {. {    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . N. \" {7 ]3 ^% F2 h6 R! [6 K; w0 z  M% l$ R: I
        Problems with a clear base case and recursive case.: X9 J0 Y  k1 Y& S

    2 F0 ?, @5 Z: S- t% @: \4 @Example: Fibonacci Sequence
    7 I1 o# O8 ]: D! G$ O' O- u4 P* c4 T" i
    " g9 W! }2 E8 N" ~8 K+ E& WThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    7 ^8 N7 y0 e' q" g+ w' ~: {& _. b( X6 Z& q: {+ |
        Base case: fib(0) = 0, fib(1) = 1
    / ~4 }5 b$ N8 ?9 X/ U
      t0 J8 R% c3 J! R% ^8 ^    Recursive case: fib(n) = fib(n-1) + fib(n-2)- [! U$ e9 f7 e3 W# G4 F  p

    8 L* u1 N& M% w* u. Gpython& p' g/ |2 Z7 [: o: F5 V

    6 N  E6 j& `; I6 b  ~8 {# Y
    5 c+ F/ U" ]5 d* \! wdef fibonacci(n):
    ; T' z, V+ T; G, u3 U) t    # Base cases
    & N6 }! c; ^$ ]% p5 E/ r9 B    if n == 0:
    / l0 W2 f1 N8 v5 F        return 0
    # M0 s1 P/ s/ m    elif n == 1:
    + ]# T' F; _6 F+ l- u# S' i        return 1( T- d1 g+ I' _5 {, r4 E1 |
        # Recursive case
    # n; K$ U( R' r: S( S    else:+ \: ?9 h$ }- _: O. E
            return fibonacci(n - 1) + fibonacci(n - 2)# q: i6 d: ~1 }) I/ [: j' A# n

    0 l7 t$ m6 o7 a7 ^# Example usage9 m# m0 i+ F) e5 y/ \
    print(fibonacci(6))  # Output: 8, q- w" B2 N% ]! z$ k4 C0 ~& ?3 s7 `
    6 s; k5 }6 C- e3 U
    Tail Recursion: G3 |/ \; Y6 s  i
    * T9 o2 `# d8 ~
    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).
    , D- A) I$ s& H3 ~; @( ~8 y
    % |  H) w+ x& m9 B) 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-2-20 19:29 , Processed in 0.068384 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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