设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 - m1 Y2 n4 d4 @. y# k' k

    + v% `; y" {. q) T' }5 A5 \解释的不错4 D+ r# k$ Y4 @$ y$ `. Y
    7 p/ s, H5 O. _8 ~7 s! H$ n3 z
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  w2 h( [; Z$ E! C4 K7 S

    3 v+ r( r) ]* k, t 关键要素
    ; u! A: ?( _5 K- `5 p2 ^; ~8 t' d2 {1. **基线条件(Base Case)**
    $ A1 H( p- K' E6 J  v. @2 H6 D& D, O   - 递归终止的条件,防止无限循环7 }0 ?" w8 T# |
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! d0 }. }+ ^$ h) n4 k3 l* a3 K) E

    + n8 w0 Y) D* f; Z9 e2 R2. **递归条件(Recursive Case)**/ s, }1 z! {7 a& T: |. q/ {2 z
       - 将原问题分解为更小的子问题/ t( J; h: f8 @, u
       - 例如:n! = n × (n-1)!9 C$ t9 o6 v# `' j8 N. z2 ~$ G/ i4 d7 N

    . I" b; u" b9 {6 B 经典示例:计算阶乘
    ; Z6 g3 \, {' u3 A# F( J2 apython8 H/ ]0 e2 E" ^( M1 v
    def factorial(n):! h4 o. @& {, }
        if n == 0:        # 基线条件% c; H" T* x8 C0 v; E# _
            return 1
    % v4 _0 ?, g% \  I. S5 j- {    else:             # 递归条件
    : Z/ d$ K, s( i' K        return n * factorial(n-1)
    ) C; l- E- S; r/ K, N8 x4 G执行过程(以计算 3! 为例):; J3 S9 d% [2 n1 e; H
    factorial(3)6 v: n' Z/ s' y, h) d" I
    3 * factorial(2)
    # y1 B: l1 I( z; v6 ~3 * (2 * factorial(1))) Y* I! h! V) j" E- K0 J
    3 * (2 * (1 * factorial(0)))8 ^& p# C, n  k3 ]( w
    3 * (2 * (1 * 1)) = 6- D0 ?( u, E& w5 R  x# J

    ( i6 v/ S0 ?3 m/ ? 递归思维要点
    * ]+ t& }0 Q, ?2 ?$ J9 }1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 h8 Q' N$ X+ J. U- N2. **栈结构**:每次调用都会创建新的栈帧(内存空间): \* B" D. A* B1 G' `0 z/ b
    3. **递推过程**:不断向下分解问题(递)
    5 p- x! g9 E( x; {! B, g% Y3 a4. **回溯过程**:组合子问题结果返回(归)
    6 q% d. e. L( R) W, T. I9 u, |" T* H1 I/ `3 Q0 N5 p
    注意事项1 e# K3 z! h0 K6 ~' I
    必须要有终止条件
    8 y4 |5 A$ Z+ Z/ T6 \5 I9 L递归深度过大可能导致栈溢出(Python默认递归深度约1000层)& Z  p1 G1 i6 _* m: V9 I" ?
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    $ ]$ G$ ?5 |  P1 |尾递归优化可以提升效率(但Python不支持)
    7 v  K2 s! b0 ~5 L$ Y% ^$ W& B; O/ q! Y+ W: M4 n: _4 @/ e7 K" F3 m
    递归 vs 迭代
    $ z; a& C/ L5 ^. F  ~; d  d|          | 递归                          | 迭代               |
    & S) m: a; K$ c9 D% C2 K|----------|-----------------------------|------------------|
    3 z, m5 Y. B& @" m| 实现方式    | 函数自调用                        | 循环结构            |
    + o' [' B! U8 K+ B- J| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |9 C6 T( p! ]4 V1 d) C# d
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 w( k5 F$ r/ L0 Q
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    & h, C3 r" |  U$ O0 q$ [; L2 h9 `; S) i0 T  H
    经典递归应用场景" p+ }' H1 x7 n9 z
    1. 文件系统遍历(目录树结构)/ P! @3 K3 M* J* X. k- s
    2. 快速排序/归并排序算法
    ! r; V; l5 F; p+ T$ w3. 汉诺塔问题  I6 t/ d5 x; f5 I4 ^. X7 t. n
    4. 二叉树遍历(前序/中序/后序)
    $ @8 e. U, k( U. {5. 生成所有可能的组合(回溯算法)
    , O6 i! M( b  r) j4 T$ X& h+ G* x$ B* o3 z. C: v# [; h
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% N" p$ T- |! P
    我推理机的核心算法应该是二叉树遍历的变种。
    & @) t2 A9 B: h. n% }! \  M另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:9 Z, ?+ c0 G) H# B% v4 K; s9 [4 j
    Key Idea of Recursion5 y1 E0 ^. I& g3 I

    9 _. W8 F" o2 E! o3 s9 RA recursive function solves a problem by:
    # f" r+ N2 e+ [- I9 W, C  g4 a
    0 O; _0 a3 V! o: Y. l9 S7 i    Breaking the problem into smaller instances of the same problem.
    + x2 M7 Y* d& R
    ' q- _3 Z6 x8 T5 Z" J3 w    Solving the smallest instance directly (base case).
    " z  R( T9 \: \; \$ Y5 n; N) l; @9 X% |" G( V/ t: ]5 r4 G; I
        Combining the results of smaller instances to solve the larger problem.
    ( S0 l' A. z5 X# E* S5 J
    # n7 W  q$ u% v/ J  b- s6 P) ]Components of a Recursive Function, w2 w2 f! B+ j

    * v( @, U6 B' d- K$ B& g0 @! r/ e6 p    Base Case:* x( h: K+ j0 h- o" |1 r4 N

    ; v. m9 K# w( B% c0 D/ m" ~        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 c' L# z/ [* k, a/ j, p& P

    & |+ O! @# |9 ]0 y5 p5 M        It acts as the stopping condition to prevent infinite recursion.% n) D5 p" |  |6 @, T

    ' D* |: S+ V% o5 A        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! t+ j! o2 I8 U  ^' @

    " X. n3 a) v9 P* c% M    Recursive Case:& S8 v+ d) o8 A( T. [

      L0 P: D. M; C, X8 F* {2 d        This is where the function calls itself with a smaller or simpler version of the problem.) M6 Q/ q* t4 X, C. R; T

    + A- A: _! F7 Y9 n) }: Q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    9 @* d: I4 ]9 {& @$ A8 d1 w) X4 i  Q0 V2 m% B& d
    Example: Factorial Calculation
    : C( h& L2 [  s" Y  \+ g8 H+ w; N- F7 ~
    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:3 _; s# @, c. J% y. F

    % j3 C, Q. o, P! [2 @) d    Base case: 0! = 1
    ! `/ Z$ r6 ?" e8 E6 \! X3 f3 M" I% N3 X5 w  [: p2 F7 M6 A
        Recursive case: n! = n * (n-1)!
    / u* O7 ?7 y2 R0 j* R0 p
    5 ]) @/ z& A8 M9 D  j; P- cHere’s how it looks in code (Python):) ]  V6 Q  ^; |! E: ]
    python
    ; E9 J7 t# y! v- v8 F3 h
    3 e  N" I0 d+ ]# U" ~0 B* a! O0 Q, x/ e
    def factorial(n):0 _4 W. D" p, A: x5 z# T
        # Base case
    0 y6 `! V- r) ^# {    if n == 0:+ L' r2 F5 K4 m6 Z/ x
            return 1
    + W4 q) b, S' [6 m: @7 u) q( ~    # Recursive case. m  M$ {/ g  H4 X1 m
        else:( f# d( f, @& o1 q
            return n * factorial(n - 1)
    6 @6 T# `6 A% z% s' u" ?3 T. A
    ( b2 R+ V( Z. B  v, f# Example usage
    - G# N! g; X# O9 Q: d+ ?) zprint(factorial(5))  # Output: 1203 p, w+ [# A6 R5 a; }1 G' |+ I
    * z+ O* w4 _& r1 W
    How Recursion Works5 p. Y0 n% F4 A% ?+ L6 Q

    2 ^7 S1 u) Z: S! D: m& `    The function keeps calling itself with smaller inputs until it reaches the base case.( V5 e2 C2 H: N( y/ L
    2 P3 d, F% [- R  Y( m6 {! E) l& x7 v
        Once the base case is reached, the function starts returning values back up the call stack.
    6 N, S( l5 x+ d
    1 X$ ^- z7 t" d/ t6 w; R    These returned values are combined to produce the final result.: P! u0 Q- {: ^2 V0 F3 [
    5 R2 o' H( T( ~, U" S- G0 Z
    For factorial(5):% N: H1 L4 j+ h. \9 Q# s

    ) d8 d7 o- f. t8 w
    0 \# i1 m- x* Y3 A, ^* A/ Bfactorial(5) = 5 * factorial(4)3 o8 U3 l6 f+ R8 d
    factorial(4) = 4 * factorial(3)
    & _' H7 O; e9 I/ Z' }factorial(3) = 3 * factorial(2)
    7 v( ]- m' j* |+ p' @factorial(2) = 2 * factorial(1)
    % C5 f2 M4 r9 j3 Q% t8 Gfactorial(1) = 1 * factorial(0)
    4 @. z, s/ A1 }  H8 v1 Z, vfactorial(0) = 1  # Base case
    " m/ ^+ \" H7 |  z7 `; Y+ F" V1 y- R+ p& T& z
    Then, the results are combined:
    % f! \! ^' r1 K' g# u
    : w5 J% y8 j3 Y- O$ J" A# `0 c* A9 E+ w8 r7 e
    factorial(1) = 1 * 1 = 1. M: g' g/ y) x
    factorial(2) = 2 * 1 = 29 M! l# E/ ?# Y; L+ \) |" L0 L
    factorial(3) = 3 * 2 = 6
    1 F6 u* R: B- T$ k$ D4 {" ufactorial(4) = 4 * 6 = 24
    4 j9 O; S! n$ Z. ]' @factorial(5) = 5 * 24 = 120
    2 u6 K  K+ _/ n( {2 ^8 L/ d5 v3 W9 y
    Advantages of Recursion
    " @! g9 V( v  \* Y) y8 \8 A# o6 u) i+ Z- j: T3 [  E  p' J
        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).
    9 E. `, u9 O% `9 M5 Q
      G4 `3 s7 ^+ F" \% P8 t0 L: q    Readability: Recursive code can be more readable and concise compared to iterative solutions.2 T0 B/ E  @8 d5 x* `. q

    . {( f# q/ L- D2 y) l& DDisadvantages of Recursion
    * l0 Y, w$ d3 a+ y# f9 v; d8 r7 j" u6 i7 i# F2 U. u
        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 z" L6 i. ^7 v- T. A, u5 n* R2 s1 o
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' K% G) I: U; M4 ^- H+ [/ R- K6 N
    5 f2 W% b) H4 M- f/ RWhen to Use Recursion
      \, H3 M7 Q% o1 i) n6 @5 s& F9 H* ?9 [$ ~
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)./ f! U+ G, z& d4 ?9 S8 I

    ; G( w8 |7 P* M  h    Problems with a clear base case and recursive case.
    , Y. J$ m: t, n; O; E( L
    - x' e- x* d: Z/ k9 l# @Example: Fibonacci Sequence. n1 X. H! b7 \8 E+ c

    7 O, e5 R4 K# c5 _3 X1 pThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    2 g" G- x% k  G0 ]) U# E$ }. l! \# f6 A4 n
        Base case: fib(0) = 0, fib(1) = 1) [$ `8 G2 U& C* O  v

    + x; ^% ]6 b. _& e6 z3 \% F6 A    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    & y$ F/ j5 R2 e8 i0 X1 r& y& g* l" `
    python
    9 C) b6 E9 X. \( {! S$ @. D- L2 K! i6 x) [) G$ R& [% `

    - }1 g; r1 i! C, n9 N3 d- Tdef fibonacci(n):
    0 b: ?6 s# R' w9 {" n. }    # Base cases
      z2 ?* x. A, \& ^3 U) m/ W2 T    if n == 0:% W; R. |/ s/ I8 o4 N) F
            return 00 Y5 P1 G! z: [& u5 @' D8 Q1 K& J
        elif n == 1:% [$ k# d# b  c
            return 1/ M; m  w( s4 X$ y3 G  [: s4 ]- }' w
        # Recursive case
    0 [" O; o; n  i/ e2 L- S0 c( i) t    else:; z; I1 H) J& _) q8 R
            return fibonacci(n - 1) + fibonacci(n - 2)& C  V$ G0 T$ r8 Y# b- ^% _

    # A- j6 C3 f3 e# Example usage, a; P" L* E, D9 {  `4 a4 r$ ~$ v- X1 e0 W
    print(fibonacci(6))  # Output: 8  Z9 N$ R& s0 u! |/ K1 @

    2 _* w5 K1 r4 o- _Tail Recursion
    ' U; t; ~- L7 U; R2 Q* y7 ^& j9 [  w. u- G( g0 k5 o: v
    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).
    - ?5 k4 F7 K  ?: R
    , o0 N) ^9 J9 J* 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, 2025-12-10 09:58 , Processed in 0.060300 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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