设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    " ^, J, Q8 }! l) D# I7 j( O1 M* r: Y& f. M
    解释的不错
    ! ^# ^+ H; t2 V5 k/ M; k& L. e# ?; d$ {" b" u3 m1 j, P) [$ f3 @
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    . S; L8 v7 z; _" J: u! @2 f" M2 s# Z( T6 w4 H, `& }7 K% I, O' a( ]; M
    关键要素
    2 }) B" o2 X$ t* ?* Y1. **基线条件(Base Case)**
    / p3 u8 R+ H# y% r- B* S   - 递归终止的条件,防止无限循环* t' I' J' f3 h! I7 @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1" T1 Z; H( v; M( S& B& G4 i

    ; m$ H  G* Q0 s% L; O' n% Z, N! _7 I2. **递归条件(Recursive Case)**
    3 R$ s2 x2 @: K2 i   - 将原问题分解为更小的子问题
    7 B' G9 G. ?0 Z6 P   - 例如:n! = n × (n-1)!! T! g5 G$ m: m# p1 p

    ( W: _# Q+ c) |6 P" |0 l: r- D% Y: y 经典示例:计算阶乘( ]3 _4 R7 M. Q& k9 R% J( D7 @
    python6 G( i$ q6 }' [" k8 m" F; d
    def factorial(n):
    " g5 w" I' \4 D) _& x) K1 j* O5 r    if n == 0:        # 基线条件  Z# a/ ^5 @" _) K% o
            return 15 ]- n# \% U. b) L7 W: v
        else:             # 递归条件; X% `& q7 g8 ?0 J4 q
            return n * factorial(n-1), q* J) G6 W6 B9 `. F+ W% U+ C* X
    执行过程(以计算 3! 为例):
    - ^- _6 w& M# z+ O3 Kfactorial(3), x1 L8 z2 s; E
    3 * factorial(2)
    ; _, S! M& l7 s3 * (2 * factorial(1))% [6 p4 v1 o! A1 {$ O  c$ S
    3 * (2 * (1 * factorial(0)))
    7 o& g# |+ o9 g1 `+ E0 _. \' @3 * (2 * (1 * 1)) = 61 y7 }" Q3 k* W" N

    - P/ s/ u( S) d1 }: Q) j 递归思维要点3 v9 Q9 A1 t) q1 N$ l
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    + _; O5 ~; x. J! X# e( ?+ ?+ G2. **栈结构**:每次调用都会创建新的栈帧(内存空间)1 Q9 e( ^8 P  u0 n: A0 P
    3. **递推过程**:不断向下分解问题(递). S+ _6 c8 r7 S2 v7 J+ L
    4. **回溯过程**:组合子问题结果返回(归)- }& \7 s9 r4 b8 f0 F
    7 T3 m. j3 R* Y
    注意事项- I% b4 H( T- N9 {
    必须要有终止条件
    # B) P- i7 @; X) P3 |2 t* S/ C# H递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 Q# u* D2 o' j/ @; j
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    % q3 O' ]# z4 h- _尾递归优化可以提升效率(但Python不支持)3 I7 W& E4 L) w7 k" g: V! C
    8 M8 I4 s" y$ u( p7 M& ?6 T5 ]4 ?$ U0 t
    递归 vs 迭代7 J+ `- c# j2 t  l
    |          | 递归                          | 迭代               |
    % _9 ?- X6 c7 u9 W|----------|-----------------------------|------------------|
    2 i$ c" U4 u& {* l1 K| 实现方式    | 函数自调用                        | 循环结构            |* L$ I+ X' n- x6 R
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |* d# O2 H  ~7 r& C; {3 c
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; d; r; @" h& R( s| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ( x6 g! y7 [4 Y$ W$ u+ _4 Q! S8 ~5 N, {5 I2 H9 [
    经典递归应用场景9 Z# i* Q8 d* M* D- P% R* S( a
    1. 文件系统遍历(目录树结构)
    . e& q9 d& }; r2 E, i, T2 A4 h2. 快速排序/归并排序算法
    , u& Q; q" c% m) L3. 汉诺塔问题% t% s+ ]3 F* f1 a9 ]* Z
    4. 二叉树遍历(前序/中序/后序)
    7 U8 l. u5 O/ F. \  ]3 k1 _5. 生成所有可能的组合(回溯算法)3 @+ m1 s4 s6 ~2 ^3 A& h8 A

    8 F2 u3 o- s! c' V% v: Z# v试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    12 小时前
  • 签到天数: 3144 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ Y+ v9 Z# l. X' \/ R7 G& L) U0 h
    我推理机的核心算法应该是二叉树遍历的变种。
    8 B# ^) d+ C: h; F* H- |& R9 f另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    4 x, v% d1 d1 @$ e& |% ?Key Idea of Recursion/ q4 `, j- m0 X3 U9 A- l
    & O9 j  y3 y" G* W
    A recursive function solves a problem by:, G6 P3 y. r3 [8 V( ~0 `
    6 w0 V1 A' n5 u7 c8 p
        Breaking the problem into smaller instances of the same problem.
    # ]' ]. _/ U& f5 Z% q. s: w; |, v/ D) T  f+ M3 x% w% U. }  y/ H
        Solving the smallest instance directly (base case).* ^4 Z: }7 R* u5 Y7 U, g

    % @) f# r( q! E6 c# ~    Combining the results of smaller instances to solve the larger problem.' q, \6 E7 C: y: M6 j- j

    9 [# }7 H  X* _: [- IComponents of a Recursive Function
    & J8 H: J. c& v4 d/ A
    $ R) ~% V# B# ~    Base Case:' b$ Q, g" b+ f4 J3 t, \2 M

    5 z, b0 W( P* H# @; }        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.8 d7 a3 \% N* E, U" x) X  b
    4 {( L. C+ ~2 c, j6 h3 c
            It acts as the stopping condition to prevent infinite recursion.
    + K8 Y  W1 _# i) U
    7 W9 b9 b7 O* _, l- y: K9 ~        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    & G' @& B+ ~" y) N' ?) T
    ! i7 j! M% N. O2 d. |3 \- l    Recursive Case:
    # \) B6 \  H, @  l+ |; \# P8 a+ ^* |" ~$ h/ o9 a/ I
            This is where the function calls itself with a smaller or simpler version of the problem./ J+ b" I9 P- f* o
    8 `3 `7 l3 q2 l$ H' J2 J% y6 {$ Y3 }
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ r- E: @+ e3 }* i5 u  r$ |& ?

    7 l" D( _0 T8 y, @  w! r0 G  zExample: Factorial Calculation
    ; o* A2 M9 `% @5 C2 c( o5 B" _- u6 T+ `/ Q4 j1 O8 i) m2 v$ X
    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:- {. A. H8 n# Z% w7 R# W

    & _/ q: m' O- C. ]7 k    Base case: 0! = 13 C1 r. R: Y  F+ ?2 I' B, C
    " y% z9 f. ]( D4 V2 F" t9 D+ l+ A2 y
        Recursive case: n! = n * (n-1)!
    0 R# [  S- W- j7 ^& {: a! s
    * L6 L5 U' A# {1 b6 ?; `: LHere’s how it looks in code (Python):' k3 v5 [0 t. P! \8 g
    python
    5 S* H& V! Y- H0 p: g1 s( Z. a4 [5 J- U$ t  g3 K
    * b; T9 U% Q- ?- x! o
    def factorial(n):
    ' S4 t$ G/ ?' H- F6 l# M8 b' q    # Base case
    ; O. j8 m  ~& b4 h" H    if n == 0:3 C+ D& {8 O5 Z- y$ M
            return 1
    1 H+ d* }7 ^3 ?% B9 m6 Z    # Recursive case
    # z/ t' f6 J; g* O! B7 K4 Z1 T    else:3 R+ b% D! |6 Z# k& _, U
            return n * factorial(n - 1)$ J& c) ~4 a4 G& i4 _6 R+ R( [. m8 r
    . Q. Q% j% H' f( ?* Q. S  p2 @0 r, K
    # Example usage! D# I  g0 L* `' M5 V
    print(factorial(5))  # Output: 120% Q  H5 Y9 }3 e, M3 _
    ; X6 J' A) m6 g, |: T
    How Recursion Works
    ' [- E4 T! ]; c" C% m
    ) d6 [5 Z  x5 M: @8 i5 ?/ w5 f9 d0 @2 P    The function keeps calling itself with smaller inputs until it reaches the base case.0 ^3 t; `, L# i' l5 V
    / t6 }/ r: T% I8 q, F8 t) c5 H% p
        Once the base case is reached, the function starts returning values back up the call stack.( |! x1 ^3 q6 \: I* U$ J* W

    1 ^: [& o) G, V1 ?$ |% L    These returned values are combined to produce the final result.) l& G7 e0 d) @' ^& z6 T) y5 F1 x

    ' C, [  Q2 p1 H# S( O) zFor factorial(5):  U9 n2 I5 y% K. S6 H# i

    ( d9 r# T: s1 d2 X, s2 L1 ]. D2 k+ M
    9 n) u4 A: J1 P4 Q5 K3 ?factorial(5) = 5 * factorial(4). G3 D0 q# d2 P% d4 z- d
    factorial(4) = 4 * factorial(3)
    . a, h# T) x: Q7 o- p. t+ Tfactorial(3) = 3 * factorial(2)
    # d7 F7 z. V4 u9 ~3 C/ C* rfactorial(2) = 2 * factorial(1)8 p# O- E0 n& T3 p& t1 @
    factorial(1) = 1 * factorial(0)
    2 r2 m, G: c% A1 Q5 K+ D# m, Ufactorial(0) = 1  # Base case3 o, X7 R2 l! j$ v# r% X
    % h4 U0 Z. O$ K/ u# i+ H
    Then, the results are combined:
    2 v0 r$ Y5 N4 w( |9 ?
    . l1 ?* u4 V2 K2 G/ |7 I8 U( m$ K0 Y+ Q0 X8 s$ \
    factorial(1) = 1 * 1 = 1
    & }$ C9 S4 V: Z% N* u4 ?% k- Dfactorial(2) = 2 * 1 = 2" K# E! z- [3 _1 Y0 n" k
    factorial(3) = 3 * 2 = 68 ~! x1 |: S* i4 E
    factorial(4) = 4 * 6 = 24
    & J3 v* @- t, S& ?# r' Kfactorial(5) = 5 * 24 = 120
    ; q+ R3 |6 |8 |2 s8 n6 M7 h- S! G+ A7 l  p3 G: w
    Advantages of Recursion
    3 v7 m6 @% h, ]! W2 J% O
    ' d! `2 X: ^' @3 g+ a4 u  o* B    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).5 ?/ e4 P$ L4 c5 c6 |

    6 [' ?, D. W9 b; c+ r" s    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * |  E5 J$ r0 F: u% G) K1 c) |- p) h3 d, c' L# ]) ^3 b- M% B
    Disadvantages of Recursion
    0 c1 p# _( [& w* L  \, T( D% i$ Q+ j; D3 H: K$ M, O0 ^
        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.
    ; o" Q# i8 g- t+ S7 W0 [" v. _8 u( p9 i( D
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & v1 D  ^. @1 p  c. L
    - i5 L/ S" ^) w7 F  J# mWhen to Use Recursion
    9 n& D( E2 m5 r  }( L& L# i4 q* @* L7 Z6 d
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    $ d: |1 J& ^2 d9 V; ~+ D& p
    9 z6 [9 s; y" n  d/ e+ ~$ }    Problems with a clear base case and recursive case.
    ' s/ O5 A/ J; q* ~1 ?# j' O* j$ B$ b
    Example: Fibonacci Sequence
      X! q5 Y1 R2 C) x: A0 D5 a( e" Y& l
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:* q8 r  U5 s4 ]* z* n& U: p
    9 o  x( e+ Y# y8 h
        Base case: fib(0) = 0, fib(1) = 1
    ) ?; C! E" i3 t' i; @8 x* J" R% U3 }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    7 [  {2 J' m/ _* z6 {# o) r5 ^0 ^; O! `: S7 w. i8 v1 q7 K8 K9 N0 L
    python: I2 z8 Y2 x: e$ l* t( }
    7 w! i$ p7 W6 O- e* O6 Z

    $ T! t( o! `0 n) D( ]def fibonacci(n):
    # t% T( N0 X2 I( A  v    # Base cases
    7 r7 G" R0 I6 z$ N    if n == 0:5 W! y6 ~1 U' ^2 H
            return 0
    : A+ e7 l4 r& @: x# U( v4 E$ R' Y2 S    elif n == 1:! {  }+ \/ j; U* l# z
            return 1) k% K; c. E# w3 z0 P; @0 C
        # Recursive case" t' ^9 E) U. X+ y
        else:
    6 ^+ j- ]4 j5 v8 F' B2 `8 Q; Y        return fibonacci(n - 1) + fibonacci(n - 2)
    5 D- F% ]; i- t6 ?  ]2 }/ b# x8 W  K- S' A9 ~
    # Example usage
    / t8 h  o/ T( c1 B+ [print(fibonacci(6))  # Output: 8
    4 S; E. _/ D" g/ {( c- R1 s8 B# y
    7 ~! j% Z1 e( LTail Recursion0 P* L/ ?3 ]# v3 P1 f, L/ \

    9 C9 @- Q- W8 t! yTail 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).
    - D7 f" v% v; |" [2 L, O/ b; p# A: Q' d
    In 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-1-12 19:30 , Processed in 0.032052 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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