设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ( C: g8 N1 h3 ^# N1 J9 y  J& D) J8 N; H4 V& B1 }$ @4 R! k
    解释的不错
    8 ?, S$ |0 c  a# q# d4 r
    5 a, y6 U9 G8 e5 J7 i递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。( q0 j& E: K$ w9 \8 B
    6 T+ R$ @7 A; Z2 O- e- p2 T) c4 z
    关键要素
    0 d4 h0 c8 o& h8 z2 s8 [1. **基线条件(Base Case)**
    5 Q2 h) L& y5 w) ]+ }   - 递归终止的条件,防止无限循环9 W# J( S# t. B; d9 s& H" X" Y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ d9 Z( ~; A+ J
    1 B# T2 k1 G- ^/ W9 U
    2. **递归条件(Recursive Case)**
    . c0 C9 V# w( s; e; ]' L6 Z   - 将原问题分解为更小的子问题, \, U7 u2 l3 j) o
       - 例如:n! = n × (n-1)!( T0 K+ F, D5 @( I
    2 [* H' X# O4 Q& ?* }
    经典示例:计算阶乘
    / c+ s5 l3 C3 |python* O& S3 C; Z  E: l  p  |1 Y
    def factorial(n):* v0 k6 }7 c2 @' g' L8 F
        if n == 0:        # 基线条件: c- Q; q) h# X5 \5 A( T& m
            return 1
    % J+ P6 b( S  O& _* W6 q& k7 c6 k    else:             # 递归条件/ D4 F: O& S: u4 G
            return n * factorial(n-1)
    * J; g* p. z( A6 C4 k/ _执行过程(以计算 3! 为例):
      f* U* ]" L8 @: Jfactorial(3)! O( o: v! T' m, v6 k. t
    3 * factorial(2)/ w) \" U4 J4 p' v' l" D! J5 A
    3 * (2 * factorial(1))
    6 t% F' s$ e9 J" A6 Z5 J( _; b3 * (2 * (1 * factorial(0)))
    8 \& g& L- F5 S5 V  h. ]% R# M8 c3 * (2 * (1 * 1)) = 6
    - _, l9 y; Z5 `: d2 L% p& ?
    ( y; y3 [' x4 ?* v 递归思维要点$ H( Y7 T. h0 m
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    - T. [) D  K, Y2 G5 R( e8 ?2. **栈结构**:每次调用都会创建新的栈帧(内存空间)& h" ]" f6 u+ i& Y- i( L
    3. **递推过程**:不断向下分解问题(递)- A6 f9 }2 `/ B  J$ H# X4 }5 N
    4. **回溯过程**:组合子问题结果返回(归)
    * \1 O" v+ t! C5 T8 y5 `
      W; t7 Z! K3 b 注意事项
    & Z. H9 ]$ d5 M1 q+ H/ |( D9 V" @必须要有终止条件7 ?& K  J' V6 e" L- {  j) }
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 t% b4 a. l# o# z, p9 ^+ w
    某些问题用递归更直观(如树遍历),但效率可能不如迭代/ ~" r  ]2 r) G7 J: Y
    尾递归优化可以提升效率(但Python不支持)6 E+ U; D9 g, X

      m* x7 t# m% E' y 递归 vs 迭代
    " h) k% @; l2 q|          | 递归                          | 迭代               |  x0 \5 _6 `1 S; y* g# ]
    |----------|-----------------------------|------------------|7 T! i  n. }/ @1 z: \6 v
    | 实现方式    | 函数自调用                        | 循环结构            |
    3 z1 o$ _, I8 O, T# Z$ d| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 z: T- l  ]6 h7 ^! N/ w" F| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |" U+ c9 O, Y( x1 ]
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |; \0 q% D$ e4 T( _" }0 v* [/ i
    / |+ M; [6 U+ m3 @
    经典递归应用场景
      ^! i" Q( w6 B1 v1. 文件系统遍历(目录树结构)
    : G6 B2 a* {9 f, M2. 快速排序/归并排序算法
    , Z9 `4 B6 C1 T9 z) F# I" _$ p3. 汉诺塔问题
    6 P% W0 f* Q! I( N4. 二叉树遍历(前序/中序/后序)
    9 o. e- V0 S+ _0 Z  w) D5. 生成所有可能的组合(回溯算法)
    $ w& {* s) G# a$ H5 h
    3 S2 i; O$ G% |$ z, c4 I" e试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 10:43
  • 签到天数: 3169 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( N" D6 C) r) N- C/ ?" a: N. }我推理机的核心算法应该是二叉树遍历的变种。' D; Y# F( l3 s- ^+ O6 N: A
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:) Y; o& M2 C, {4 V- q7 E  x
    Key Idea of Recursion
    8 x; H7 ^, m0 q- V9 }2 w. a* S' \% v( E6 X9 A  @' g- T, G
    A recursive function solves a problem by:
    0 O1 a* h7 o& Q! O1 W# _/ ]9 K! M# H! T6 A% P. O
        Breaking the problem into smaller instances of the same problem.
    ; R) y( v4 e. Y* V, w8 T! n& D/ I0 @$ F( p
        Solving the smallest instance directly (base case).1 L$ d8 B. u5 A
    1 B3 R1 Y6 o: C+ ]5 ~, Z
        Combining the results of smaller instances to solve the larger problem." V! o! Q/ v* b& z. U# u2 x' B

    4 w3 s  Z: y3 v2 AComponents of a Recursive Function$ Q/ N; o! d! z

    : U9 B1 M% r0 I, ^4 e    Base Case:; F: b2 J. X: w' s# c5 d* l

    2 V: p- m9 \& c        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 d' I/ D. a+ c5 m" J8 M7 O$ b2 f! k% B
    , Q& v! r9 ~2 o" d
            It acts as the stopping condition to prevent infinite recursion." E3 L# T! ]# |  b% ]

    % A4 I$ G- Y2 E' h6 P        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.) h* `4 K8 g% f$ K

    : E3 }) D# J# Q% m: g% V- a    Recursive Case:
    0 n+ N8 D! F$ v2 |2 q# M& X, g3 D/ U" [- F% ^4 s
            This is where the function calls itself with a smaller or simpler version of the problem.
      _6 c/ z: Z* J
    . f6 {' x% l- O3 a7 `; Q/ d        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 {8 N0 b/ ]1 [( n6 Q8 y9 p
    # X0 S8 ], \  K8 e$ Z" j
    Example: Factorial Calculation" X; ]0 k0 W3 M8 C9 T4 C( w
    , P; C% }" z7 D2 N) p
    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:
    + \) `; X. G& ?+ m) V. e( Q8 t! D+ F
        Base case: 0! = 1
    ) _; w7 S, Z3 U
    8 V8 P! ]7 A7 S1 F* R    Recursive case: n! = n * (n-1)!' K( g$ D) j' O9 T% x& ?
    $ C  O8 [( `8 s$ T, i' r
    Here’s how it looks in code (Python):
    + B& O8 p+ s1 ]" T! t) z" lpython
    ) H( S# E' D, l( k- r/ c: h' J* O2 C2 H  j- V  X

    ( }! W9 L9 [3 B5 b+ {9 l/ ?def factorial(n):
    ) }9 [$ Z' o: ~5 v2 [, k! ?3 m    # Base case
    + }0 f7 l0 T0 s% }& C, m) D    if n == 0:
    0 O. N) v* X3 S! k, G        return 1
    6 `  ]! G$ G  A0 N    # Recursive case
    ; p- {6 f. m- c9 w! |  r    else:0 D! P% w* A7 `1 \4 A0 k- W2 _- y
            return n * factorial(n - 1)( Y$ A$ Y4 k5 N. X( I# h6 |2 m8 l
    ) L9 y: V$ N9 m
    # Example usage
    ( n( Y7 s! M3 Z2 uprint(factorial(5))  # Output: 120$ H) V- x! Y* y7 D, p( B  ?# l& j
    3 r7 b: }  _' H- f$ n6 x$ u
    How Recursion Works! c& t' p( j! E( j9 e

    / R! D' x: m& R* A9 J2 d) i/ c    The function keeps calling itself with smaller inputs until it reaches the base case.
    , u+ S- Q' g+ h3 m
    : m4 a& V0 K/ }    Once the base case is reached, the function starts returning values back up the call stack.
    * J, p9 h$ `& j2 H# S5 i* R# q: D! w" M) N0 c, Q( R1 j8 R
        These returned values are combined to produce the final result.
    + F' T2 c! P9 \8 |" c( }" T2 @) x0 l! n+ n8 g  h/ x3 \
    For factorial(5):& l6 n9 ~( V* I8 q" @/ S
    1 J* i5 H- q3 |/ L& j* o' x- \6 h

    / S" W% `' E5 g. r4 sfactorial(5) = 5 * factorial(4)
    ( P8 B8 d6 e6 G. D9 c' x7 bfactorial(4) = 4 * factorial(3)
    8 O+ [8 Y" N. i6 Q9 Z+ V/ \factorial(3) = 3 * factorial(2)' n% U, k5 @4 |; t0 [$ l" }
    factorial(2) = 2 * factorial(1)$ ]7 b$ C; S" Q4 W# V
    factorial(1) = 1 * factorial(0)
    ; @9 J2 |0 i6 Z8 |, s2 T0 @factorial(0) = 1  # Base case
    ( [& [- c' c, F9 @: a$ E+ C$ t2 |" p' Z% r
    Then, the results are combined:
    2 r8 r: w' L7 {& L
    / ^: Z& o- o$ ~0 f9 w, @; d% \4 O7 {" ~' L- g: y2 g5 s" S- X
    factorial(1) = 1 * 1 = 1
    5 w8 k" z! w$ _( y0 f' a% ^, E# _factorial(2) = 2 * 1 = 2
    / k9 U5 n: M# T! N1 u. J; mfactorial(3) = 3 * 2 = 6
    0 C9 b) f9 h$ \, n! y8 [! lfactorial(4) = 4 * 6 = 24. R% z, ]+ _; j; B0 Q2 B8 r7 Z, I: u
    factorial(5) = 5 * 24 = 120
    / y* n  V( h9 E( e3 h2 A: D9 B$ W# U- U5 r
    Advantages of Recursion* ]" e; @# {9 E- X9 e5 {3 s/ K
    9 p6 B/ X  P# l: C% v
        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).* Y7 F: t* H2 I# E5 S9 u! Z
    + c8 y! ^5 Z' n
        Readability: Recursive code can be more readable and concise compared to iterative solutions.5 w$ J6 X# C( `$ Q9 G) t
    * I0 A6 }$ F7 k# K
    Disadvantages of Recursion* q: o7 {$ y  ^0 Z+ ^

    ' h# N9 t! Q0 _/ [( j    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.
    " Y! e! A' _& t$ @  Q6 w& [6 X$ o2 l+ u7 l+ t& c4 l1 c0 R% U2 q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 o/ }) z* u( ]( A
    ! a" t/ I) V) {When to Use Recursion
    * }& u/ o8 S" Y& e: I
    9 L. h! ~# C: J; ^# P    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ; B! L  x( j' f% C! g! S+ ?$ u6 x( A
        Problems with a clear base case and recursive case.
    * A6 l" I6 }9 Y6 H2 g% m, B0 i5 f3 v$ Q+ T
    Example: Fibonacci Sequence9 _% U, N5 u! U2 N
    5 [) I, J8 _) A; Z/ y5 M0 E5 A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:% T4 U# N7 f* x8 l  f4 G3 ^7 v
    . i/ K' f; P5 W
        Base case: fib(0) = 0, fib(1) = 10 t. k8 T. z3 d- t# ?. M6 V
    ; w& i- T8 c: G, n
        Recursive case: fib(n) = fib(n-1) + fib(n-2)( P) Z- ]5 k* K
    : Q; P1 ~+ ?6 ?/ i) S
    python
      z' {$ ]. g: k% e% K% {* n8 f6 @. H; G( \* V

    6 X3 \; e2 k# w. ~4 ydef fibonacci(n):
    ) c1 `4 D/ \9 H$ m: [/ ~6 p    # Base cases
      x3 a6 W5 K: U8 F; }1 J    if n == 0:8 ^7 ~) H  o5 _' M6 D, S9 T
            return 0) l3 v  {! q: _0 N5 P7 K
        elif n == 1:
    + p7 S5 T# @8 m% o        return 1; H' J- C- H; k2 z0 {/ P& E8 V
        # Recursive case
    : f9 q+ f3 Q* C7 g2 l3 w    else:: B; u/ \3 G, U- w3 z9 f, |
            return fibonacci(n - 1) + fibonacci(n - 2)
    5 r& D) {, R. K9 H4 W: q) G2 n. |/ f& a, |+ P
    # Example usage
    8 U/ G" V4 J' }1 L: B# tprint(fibonacci(6))  # Output: 8
    / z. a8 ^3 I" d) T; I2 W6 z" G( g( O1 q( G
    Tail Recursion2 B9 n' c: e7 @2 V4 Y( m, A
    + i' A. N  p- q
    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).$ ?3 @: C7 y6 w

    9 {# {+ e* W5 I$ `0 E* PIn 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-11 02:31 , Processed in 0.057194 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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