设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 - z1 `/ \% V" t4 m" U- c& b
    * W7 j" A* f! U$ \3 N. b% }  ^7 A
    解释的不错7 U" @2 v9 A1 b

    " ?8 m5 t0 `" S递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 d# C: I2 p. u6 D$ u) a# n5 {" M) c
    4 Z* J: J4 K$ r9 h$ G
    关键要素4 J6 U6 ?  I8 k0 z
    1. **基线条件(Base Case)*** p0 `2 I, h- \9 A0 m
       - 递归终止的条件,防止无限循环' L: k0 F" Q0 m% f. ?8 |
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1. i% g) D6 }( I- Y) O( R

    7 c6 l* h8 j. g( w2. **递归条件(Recursive Case)**
      k; a+ v  C3 z6 U5 A   - 将原问题分解为更小的子问题! @! T5 [6 O: k3 T# X% |. \5 H
       - 例如:n! = n × (n-1)!
    : m; Y& w/ a* B- u% w. ?1 T7 k/ R1 l  T" P9 `1 D2 |9 `
    经典示例:计算阶乘
    $ o" m. E! ]  x; z1 h6 F* I! `$ dpython+ d' N( |5 G( R4 d2 f) M  N9 z
    def factorial(n):
    $ r$ L7 K, D' {' k/ T    if n == 0:        # 基线条件! }) s/ |2 ^+ x( W
            return 18 z' ?- T+ d6 q: b. N9 r( l: x
        else:             # 递归条件+ e8 f& G* t; O1 D( V. N
            return n * factorial(n-1)
      W( {( D8 @4 g; d9 b执行过程(以计算 3! 为例):
    6 r6 s8 @4 P6 Q' l; s* c6 Nfactorial(3)3 {% t6 b3 G/ Q0 q  |" n' K
    3 * factorial(2)
    " r, \, B2 h5 \" y, j: t3 * (2 * factorial(1)): Y5 J: H" X$ ]  Y8 ^6 }
    3 * (2 * (1 * factorial(0)))4 H. V7 @+ [1 z( G# W% d+ J
    3 * (2 * (1 * 1)) = 6
    ' J( O7 v3 V& D$ ~
    8 ?* `8 Y  `0 O4 y' Y( g 递归思维要点- O! n* K' g6 d- u  o. [6 A2 n
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑' L! `$ s& V- N0 P
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    # ^7 b+ ~, V& E, T0 S' ^3. **递推过程**:不断向下分解问题(递)% j) H/ l8 d4 G5 k. o6 [7 A- k1 g
    4. **回溯过程**:组合子问题结果返回(归)
    5 a: I# t% e2 N( q
    3 P/ y7 i9 U: J+ b  i9 u 注意事项
    * n% k! B" r6 `; x. C必须要有终止条件$ g. ~0 k& z  w4 x! o
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! k$ v2 g" ^- o* R& S5 w: J) y$ _# I某些问题用递归更直观(如树遍历),但效率可能不如迭代( L$ n- K* s" x; y
    尾递归优化可以提升效率(但Python不支持)
    " E: V0 G9 y+ L! [
    ! E# X7 [$ G, ]  \% P, T 递归 vs 迭代
    & l+ A2 _) s, P! t; x. ~4 s5 J* C6 G- i; C|          | 递归                          | 迭代               |
    1 g2 r$ M0 F1 f3 r6 ~9 ~+ Q|----------|-----------------------------|------------------|
    4 l+ Y" ~5 ~4 T| 实现方式    | 函数自调用                        | 循环结构            |
    " ^  ]1 p* Q# a9 G/ g2 t| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |. p- Y6 w( M' Z3 Y2 s, g7 k0 c
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    / i# e! l2 c3 M$ X4 G  d1 {: ^% d| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ K- y5 u: G+ b
    / v# P% I; @* \% V8 q- h! G
    经典递归应用场景) k6 ^( z: k- X. s3 b
    1. 文件系统遍历(目录树结构); I, R1 i, \+ O; {! o- n( w
    2. 快速排序/归并排序算法
    1 R2 K( v+ Z+ c2 }3. 汉诺塔问题
    4 b' A0 M3 c% M& k' _+ h4. 二叉树遍历(前序/中序/后序)) J0 Q# h! v$ s, G9 O
    5. 生成所有可能的组合(回溯算法)
    2 K3 K( H: N# D! v0 P6 e0 ~5 b4 t$ D; j6 w
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 05:49
  • 签到天数: 3098 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,( t$ ^% |% t  }0 B. C+ }$ _
    我推理机的核心算法应该是二叉树遍历的变种。) y. \$ J# d, j( e3 c' c
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" i) O+ Q$ H( s( u
    Key Idea of Recursion$ n% y  m/ Z" |1 m) l& ~

    ' g5 Y5 K; z; {A recursive function solves a problem by:
    ( Y5 Y/ E# k7 ^' m( [9 m2 y5 o' i$ q. d4 s3 S/ |6 D1 A
        Breaking the problem into smaller instances of the same problem.
    # a6 O4 \) f9 U: P" d! P* f
    ' V& M! @0 C( t0 g% ~: g! u    Solving the smallest instance directly (base case).
    : \: G4 Q5 K4 y0 T6 H7 G8 t  a6 }* y( M, s! o
        Combining the results of smaller instances to solve the larger problem.
    " V; P7 j9 i+ X7 B( w4 i% q4 r
    " z$ _4 l- T) V  F6 _: {Components of a Recursive Function# m$ [' g; u8 X7 d2 i8 d0 ?3 |
    " V3 r- F% n, ?7 c0 O1 n; i
        Base Case:
    ) K. q5 @5 V% ~* [  D% `
    / M: A% \" C/ A        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 [6 q% v; l; e) w% Y! P
    ( W0 F- L- Z  _+ n( Q
            It acts as the stopping condition to prevent infinite recursion.
    ) I4 L. T" ^6 Y7 z3 }' h
    + f/ @# N$ v% Z5 C3 o        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 s$ w7 w4 T5 f& F6 ]6 O9 k
    % [8 L; _( t4 f! N; ^
        Recursive Case:8 I* p5 c- \4 H/ u/ T7 |1 Y

    * ~) Z1 ^0 p- i* M1 X        This is where the function calls itself with a smaller or simpler version of the problem./ j: s- a+ I( [7 S
    4 k( `* v4 ^% i: }3 S1 O
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    8 @0 ^( G6 n" _! B- T# N4 ~4 Y& k- U) d2 |6 i
    Example: Factorial Calculation! a/ v- |0 p# N7 G# @4 H! N6 |

    ; _8 k6 Y- F1 n6 {0 x$ M  oThe 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:/ ]: _" k  k/ n. t8 H4 g- S8 _

    9 F/ Y9 j& g$ {" Y2 E2 `    Base case: 0! = 1
    ' p" Y, v$ Y  P6 j* N! Q: h: }/ H+ M! Q
        Recursive case: n! = n * (n-1)!7 C2 _" k# S, z* b. p8 P5 M

    8 {( J* B& R. J  t& mHere’s how it looks in code (Python):$ j  N' }5 ~9 i& c/ {
    python4 G9 g" U- P, p8 ~+ d# f
    % U9 z. s: G+ F$ t
    / u6 Z3 `" `6 u' c
    def factorial(n):9 ]/ ]" x: W2 z7 H9 B; X: Q9 M
        # Base case- J! s1 |" E& r( L- z
        if n == 0:
    8 M5 V! Q" G& K4 G4 C8 X! X% k) n        return 1
    * L( v. O% b, _: h' {! s2 S6 G    # Recursive case  H7 T3 O) f& |0 Z+ I; P1 e
        else:
    " I1 z5 Y6 M2 n! P% o+ a" M        return n * factorial(n - 1)* S' M+ ^4 J, T

    1 M- D' `; A7 i5 v+ _8 d& ~# Example usage
    9 u/ P* V' r$ G% tprint(factorial(5))  # Output: 1205 t1 [* B# R$ C: T% v+ V8 p
    + ]- v! n, Q. H
    How Recursion Works4 H/ C; O- a: ^0 _9 s+ G* s7 P
    ; C# w; Y: z9 q- Q2 f) A+ Y3 C
        The function keeps calling itself with smaller inputs until it reaches the base case.
    % I$ s! Z4 d" O2 d
    / O1 r7 D) q$ Y' U    Once the base case is reached, the function starts returning values back up the call stack.! b: q' X2 J" r) p* h  r
    / p8 K  B/ U# [; t" X8 P$ ^2 M
        These returned values are combined to produce the final result.( d; E  E6 p- {' v: R( e, U

    2 i$ n, E5 n2 v4 a6 x# oFor factorial(5):" `% X1 o1 N& U3 F. f7 y6 M4 `

    & M+ Q/ y! b$ @$ o$ W! Y+ N; _" h  e( P" ^* S1 `
    factorial(5) = 5 * factorial(4); Q, M' j& K; j' l+ [& S8 ]
    factorial(4) = 4 * factorial(3)
    * z+ v: r. n# ~- tfactorial(3) = 3 * factorial(2)
    9 n8 M& S# |. ^- l2 T3 n: Y0 L1 Kfactorial(2) = 2 * factorial(1)# w0 `; y& j  g! T. N
    factorial(1) = 1 * factorial(0); o/ R  X" y* N
    factorial(0) = 1  # Base case
    " Z8 \: ]' f% l3 ^
    4 J/ \7 m" r2 c  T2 F9 zThen, the results are combined:- T, |+ R4 L& Q+ Y( N( w( a

    . x, m9 e7 Q( H+ Z
    : }! o, e; p' Y& o- b8 ofactorial(1) = 1 * 1 = 1
    . H" G& P1 h8 z! s/ G. D& E0 Afactorial(2) = 2 * 1 = 22 y3 q. ?9 k; E! w' v/ D: ]2 b
    factorial(3) = 3 * 2 = 6
    & T* Y' r) A( B' Q2 y5 h; pfactorial(4) = 4 * 6 = 24
    ; }) c2 a: K" ?: N) rfactorial(5) = 5 * 24 = 1203 x& W( a' x$ Y
    0 _$ q$ u; ?) O, h/ ?
    Advantages of Recursion
      A' p) W0 M! T6 E) M7 z- L. e9 |
        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 N( Q" a8 b( G' q5 t

    ; @% n& a) G  l( {6 [4 m% m$ I+ p    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    $ Y5 h4 _( z5 }6 |* f  Q3 h8 J  }7 ^& c7 T
    Disadvantages of Recursion
    % [1 c7 c4 [6 W1 j* S6 q7 E1 f/ W% V2 C& ^. h  g. R+ G+ Y
        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.8 Y+ J! M7 I4 l
    - q7 Z: |  T" H* U& l# I, E9 Q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 ~3 i4 D& l) I
    7 w3 C3 Z: G- W: p/ ^, X$ z
    When to Use Recursion
    : X0 Q. ?* r$ H3 Y" l+ @% ?3 M' E. ]. S6 C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ! z6 {; ]  G, q7 o0 [: |) Y# N( y
        Problems with a clear base case and recursive case.- u$ J& Y& ~& X9 g  V3 Q
    ; \. W5 ]5 W9 u. U2 s3 X& D' D$ ~% G
    Example: Fibonacci Sequence
    7 G4 F8 o0 {' {
    1 S# K& U* _5 s  `- p% `' XThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    0 o, D2 |2 N0 F3 q5 b6 A- m+ W! r& R% m" b1 `
        Base case: fib(0) = 0, fib(1) = 1( G0 x; L% X) ^9 \5 p3 F8 t1 b' a

      b5 V" w6 C% J' x  q: [    Recursive case: fib(n) = fib(n-1) + fib(n-2)6 @2 E/ m7 F' c- X8 B; q# G

    6 x7 t9 y; k0 p$ k2 C3 Qpython+ y, z! y' X) b8 H7 X

    4 p# y$ d6 w1 ^, F. I: C( f& o' |9 N8 d, u0 |2 Z
    def fibonacci(n):8 ?9 L6 ^- ^, G9 d
        # Base cases+ e! P8 o. W5 B
        if n == 0:
    ' V) B8 L9 D  x$ z8 v+ m        return 0: ^7 d  K1 _/ Z( I
        elif n == 1:
    3 Z) ~0 z( Q/ ?7 M; I1 T& j: i        return 1( h, U: L5 S' ]0 l
        # Recursive case
    4 a% K4 j; ^- d" K& E( s2 o    else:
    5 B. U- |: \: O' h& N( a        return fibonacci(n - 1) + fibonacci(n - 2)8 N% }: R2 _/ e% j" n

    / Y  Y# x# ?" w# Example usage4 a/ y! n+ k* ]9 y% S6 G, `
    print(fibonacci(6))  # Output: 8  q( Z- P, j& q/ O! k0 V2 i: M
    6 [* ^0 u6 r/ Z7 P+ z+ m- P
    Tail Recursion
    ( l7 _: K& Z8 n, y* I& u# G4 c* G
    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: @2 Y6 t4 n  z" K. Y7 ?8 ?* T+ ?. ^6 t/ I
    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, 2025-11-22 04:39 , Processed in 0.030965 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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