设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 5 Q! T. }" {+ }( M& ?+ Q+ H

    4 _7 q3 ~0 @/ U: S8 k/ M  T解释的不错9 Y8 N: l: Y/ b* u. ^  v  v8 V# Z
    5 m7 T) ^/ e$ i$ g4 Y, l0 o. S
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ) U4 r# y- p% @# h; [& g& r7 {
    ' {4 z3 ^. ^. r# o& m" V- k. y 关键要素; B6 W9 F) P# t, u
    1. **基线条件(Base Case)**
    3 J' V/ X2 @! c, n( V$ ^   - 递归终止的条件,防止无限循环
    " ~/ H- z* E; i% V   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1* B+ u- l" |2 j/ B$ V' I% Q

    : c+ @" f+ f" V) ]/ C2. **递归条件(Recursive Case)**: ^: F; [* g  }( C3 C' c8 D7 h% A
       - 将原问题分解为更小的子问题
    9 |9 c) U( F$ W  h/ E# f   - 例如:n! = n × (n-1)!
    ' B3 ?5 R) X6 ^6 E
    & b" R  P  l! R% Q7 R. c2 f9 U 经典示例:计算阶乘
    / Y  j; r3 G' X2 u. ^  ypython) k2 _+ E2 _$ j
    def factorial(n):
    $ |% O: F6 z( C- w1 F4 {    if n == 0:        # 基线条件! m6 h* y9 |' W. Y! J1 i6 e
            return 1
    - P0 U6 G2 r" h4 O1 H/ X    else:             # 递归条件3 r- J+ A  l9 V9 p1 g5 z
            return n * factorial(n-1)
    " e5 s; h8 M. q执行过程(以计算 3! 为例):) m4 J$ X" e( @8 N
    factorial(3)
    ) ^. a# s) m# g; T: n/ }( V1 R3 * factorial(2)
    0 o4 T0 r/ ^$ T# a. O3 * (2 * factorial(1))
    $ r/ z) A5 y( g* B# i' \3 * (2 * (1 * factorial(0)))3 N5 C! U: E- X5 u( c; L7 h, S. l' S
    3 * (2 * (1 * 1)) = 6; r' K' Y4 S( c( ]
    1 p# ]" {( ~& |* ^! Q& ~( h
    递归思维要点
    8 x% P/ F7 V" @6 r; A: T1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ; [7 W0 _) D3 c9 Z$ i7 A2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 C" V- w% q2 V- i9 v3. **递推过程**:不断向下分解问题(递)4 {3 L/ e: M8 ]: A$ @
    4. **回溯过程**:组合子问题结果返回(归)
    & B8 b- `4 k% `* V6 m+ I6 @! R: |; K- s' x! e" j- u; }
    注意事项
    ' d- X$ [, t/ D" L) c" q  U必须要有终止条件
    ' P% g5 p2 {: j# B3 L# [  u递归深度过大可能导致栈溢出(Python默认递归深度约1000层)- g2 a, S6 ?8 ]; S2 q* O$ a
    某些问题用递归更直观(如树遍历),但效率可能不如迭代+ y6 V; P$ G" h3 c9 W% r' K- m
    尾递归优化可以提升效率(但Python不支持)" m) D) \7 Y$ a5 r; J
    * E& q, j$ x/ n% G
    递归 vs 迭代1 O0 z- ~# B$ J
    |          | 递归                          | 迭代               |% y. R: t1 T  M" }. ]7 w
    |----------|-----------------------------|------------------|
    2 i" B* l/ [6 b( U1 [! y8 G| 实现方式    | 函数自调用                        | 循环结构            |
    ) Z8 w$ m7 E0 n5 \| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |% N! C) R5 O8 n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 W0 ]$ `" T( a0 g8 i| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 d4 g  c( e( `0 F
    % b1 I  D6 \3 Y( k 经典递归应用场景
      g6 }; N* ?+ R4 _7 e4 C* ]1. 文件系统遍历(目录树结构)
    & F5 P, V  m5 G! j; b2. 快速排序/归并排序算法1 a: v0 O. c  G
    3. 汉诺塔问题% s) K1 c9 \0 j. Q( Z
    4. 二叉树遍历(前序/中序/后序)( i; X, v) d  ?2 {
    5. 生成所有可能的组合(回溯算法)
    ; |  O2 y5 _2 C+ z1 C0 B2 q; m+ J6 q1 B5 b, R7 P* I# k+ _; E3 Q5 A
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    4 小时前
  • 签到天数: 3066 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, R/ M% d3 o8 o) F
    我推理机的核心算法应该是二叉树遍历的变种。
    3 r" L$ {, c- B另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    # z+ ]2 f' Z* {) T) u) XKey Idea of Recursion  E+ z9 Q3 L9 g, o' `6 O) E
    , v  |( X0 v, ?
    A recursive function solves a problem by:
    % ?! u# Z4 K! H- M6 u2 L+ d: f% n* z: d! N6 p4 Y- V6 ~% ]
        Breaking the problem into smaller instances of the same problem.
    , n& m! Z2 D- e9 T
    # Y( R* e+ n% t! t% H( F4 y/ v1 O( d    Solving the smallest instance directly (base case).
    9 [  I- p8 {9 z: X
    + E( Y4 c! j$ f    Combining the results of smaller instances to solve the larger problem.6 j  K" ~. ?  D* J8 J

    ( H' N% K1 Z  B) {3 WComponents of a Recursive Function; q6 ~: Q1 v1 X9 t6 k7 R
    5 h$ Q5 [. W1 }; Q; ]$ r5 ]1 c
        Base Case:
    2 o: t) M; o1 _5 j; a' L* g6 g& k; `* N+ B! p$ X; o" u
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 O: G3 g3 V- a2 h* Y! y' ]  C
    ) c7 f1 ~6 i! P8 |5 N
            It acts as the stopping condition to prevent infinite recursion.
    . Y7 d" b- U4 l! \1 g* S$ S8 C" u2 w+ r+ d
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    : h: o% V6 v+ u; Y8 O4 |2 k( s3 E6 H. z$ [* F
        Recursive Case:- U6 u2 v7 P6 z

    # r, k- d# L  ]+ i5 M7 B  A( J        This is where the function calls itself with a smaller or simpler version of the problem.% {( D- l8 L2 R: \. i6 ^
      e, P7 d" I$ O6 X: y
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- c3 B! s$ F/ V5 {$ h/ B

    # [0 U0 h6 ]) zExample: Factorial Calculation, V: g8 J) B$ v  z
    + ]' y4 e/ x3 m
    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:
      Y' T9 U7 B! P8 t$ t: x) H1 }- p: U0 y, @
        Base case: 0! = 13 A' |, i: Y2 U# r* z$ a$ Q2 a
    : \) o! N, P$ u3 z, I( u
        Recursive case: n! = n * (n-1)!) e8 x5 P: R0 ?

    * |1 {6 d6 z5 t! V2 u2 r0 M8 jHere’s how it looks in code (Python):, [  y, V3 O! [2 c' {# }
    python( ^9 T% F3 f/ G' |6 }- a7 O
    % E' t  e: z  v4 o) a
    & E2 M' V' {9 e( @/ M
    def factorial(n):
    / V* X$ N* s1 e0 ^2 Q* Q+ y- V    # Base case. M* Y  O& a0 z+ Z
        if n == 0:( W3 D. Q. {0 {) G4 R
            return 1
    ( r# @/ k8 z$ n    # Recursive case
    9 f# e  |1 Y3 o$ l0 D' y5 h5 [    else:. d/ e* v. i+ Y* C& q/ s
            return n * factorial(n - 1)' l: X, Y: w0 F) f
    1 R$ e7 O1 T( ?8 `/ F* \/ w
    # Example usage
    8 q9 N) N0 R6 h% |print(factorial(5))  # Output: 120
    $ V; U( R; y/ h
    0 }/ _) o2 `' ~! V# p5 ^2 k7 {' \/ u8 THow Recursion Works
    % |, [* R/ M% J1 u( b7 X
    7 q6 Y' M( n- [! {1 M$ w5 G3 {% s& O    The function keeps calling itself with smaller inputs until it reaches the base case.4 N: B" M/ I1 N  X& T/ x4 x

    0 g0 @5 P) Q; v    Once the base case is reached, the function starts returning values back up the call stack.5 z! @; Y; [  W3 Q" Z! |

    8 Q* f1 q; D$ l# p# G+ s$ j    These returned values are combined to produce the final result.( W" c# h4 Z* g; P  q" R

    ( e9 \$ A' w& s5 t/ F) D: [$ iFor factorial(5):2 z% z& e  P- r' q  E
    ' [; I9 }$ f/ p/ q' {. r. @

    - u$ ~9 E9 H) o/ }factorial(5) = 5 * factorial(4)+ i6 }, O$ @; @; c/ Q1 N% @" {
    factorial(4) = 4 * factorial(3)
    2 I; z* v' _, H( p+ y, f) A/ Xfactorial(3) = 3 * factorial(2)
      Q( D/ m5 T0 B- u& K; E7 Ffactorial(2) = 2 * factorial(1). R7 y/ k/ ~7 @5 i
    factorial(1) = 1 * factorial(0)
    . T; E! N6 i. p# ?$ M5 i$ c- Dfactorial(0) = 1  # Base case
    3 N. t' l: S( Z. Q
    ' h' {% {; S- p+ r/ D) K) HThen, the results are combined:
    # V  A  t+ _4 Q5 S4 V
    : p- j4 C" T/ v) F5 |' T( W! n) C' `; V& U" T2 P+ `: g
    factorial(1) = 1 * 1 = 1! P9 d0 |: D1 Y: g/ d4 f
    factorial(2) = 2 * 1 = 2
    ! ]3 j( G3 d/ ~factorial(3) = 3 * 2 = 6* M+ l" m+ i+ o5 y" [' {! K
    factorial(4) = 4 * 6 = 24" s. @7 s# C* }# c- Z! [0 K: u% q
    factorial(5) = 5 * 24 = 120! [4 W) Q  W) E- `% }. }4 @
    5 }5 k7 ]; G  x- u* X1 r
    Advantages of Recursion
    ! P3 u. ]9 \" }: J' {. f: _' r2 p5 ^% k
        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).
    + q3 m( x0 ^0 I! R2 y, M4 f% s  \, l& d$ y; S- g
        Readability: Recursive code can be more readable and concise compared to iterative solutions.2 L( P# H. `. B1 _2 d9 F9 G" i

    ; {  k9 q7 D" r1 b( L: sDisadvantages of Recursion
    7 L5 Q' ^: F( }0 [% \
    , s+ m0 f0 y9 k3 c    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.
    4 V* e" C$ _# H: }4 g$ w- ?. T0 d5 b( m1 i
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 H4 C. t; V7 ^% L" H
    % T: T4 ?- \! x0 g
    When to Use Recursion* y7 Z1 }, H, G. V

    - H# V: ]3 R8 w7 z% I) ]. D8 e    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    3 d) R" D! l+ I% J2 G
    " R1 d7 S8 Y' ?* I. t: y4 L    Problems with a clear base case and recursive case.
    ! {' B) s% Z, |; K  ^9 ^( v
    3 {5 s4 u* p0 K# nExample: Fibonacci Sequence# R' q1 ^3 X. S2 P
    ! E2 i4 ?5 d# E1 _  }: y' e% F
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    * E% U/ \& Z  y# f8 |. V( `$ r* u2 z! D1 _' J* N0 i( f
        Base case: fib(0) = 0, fib(1) = 1% b! a$ Y, q* C
    ) \5 c& t/ v6 u- F
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    4 @; F' [: P9 B0 ^4 _0 L/ `: V
    ) R4 A7 T! j  T' ]( t% Opython( c& O2 A# K1 O9 \; a. C

    0 X# z5 q0 d( H8 g
    . j) j( p+ B8 \9 N# x& udef fibonacci(n):
    5 n; W% L8 M4 `0 k/ i    # Base cases) M, H  y2 L( w! W/ j( O$ o# s& j
        if n == 0:
    # E/ q# z$ i# H6 G$ u" g        return 0
    ) \* E0 v- r7 \# v1 ^    elif n == 1:. }1 R* l: J* T, p
            return 1
    " ?& l! [( P" y1 n  W; v4 Z0 H$ U    # Recursive case7 S' K! y: l/ Q* r
        else:! Y: i$ t- h1 X) H# M7 k+ R
            return fibonacci(n - 1) + fibonacci(n - 2)6 i& |2 _4 }2 E9 F0 z: Q/ }4 k. B

    3 H9 Y' ]4 ?2 i6 |# Example usage1 ^3 G7 f: r0 X% k* ]' D! y
    print(fibonacci(6))  # Output: 8
      x4 D$ h2 l/ e. i8 ]1 Q' j) L- Y9 r0 o0 y' Y
    Tail Recursion
    . Y& z5 g+ H3 d5 g% f! e  _3 C+ l. m4 x! i
    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).
    8 k  O1 {: e& f+ g
    , [. f% ]) r3 T1 s& ZIn 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-10-14 11:29 , Processed in 0.037361 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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