设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      }! X- |9 B' d! a- A0 M8 E# |) V+ l% |% u' o0 }# U
    解释的不错
    8 B  H1 h7 e. H, i; a- L' _: a* F4 M% a5 w( T$ q" P
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。) t* I8 ?8 @9 @( D1 O2 l& C4 j# V

    6 F" x3 G. ~1 ~ 关键要素9 t5 e' c. S0 O$ x% v/ `: U6 u* x
    1. **基线条件(Base Case)**
    7 H& `! @; S/ m7 R, Z3 }   - 递归终止的条件,防止无限循环
    8 V: v  f+ M: l9 I   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ g% n8 Q3 y9 S: h
    * I3 o7 h3 o; }
    2. **递归条件(Recursive Case)**( Y5 x/ e) C  D  f$ {) n* q  w
       - 将原问题分解为更小的子问题& Y* b1 l; ]) q/ u# p( S
       - 例如:n! = n × (n-1)!
    ; v+ v( ^' J! a( V+ \' J
    4 T0 P! g9 h) ]3 y3 Q3 i8 K 经典示例:计算阶乘
    4 e) Y' f' V; `python
    6 s- y, w2 G" Kdef factorial(n):
    ( @2 H6 t% {& M: N8 `    if n == 0:        # 基线条件
    6 M$ N7 s1 ^( t3 A8 |9 l1 l, g        return 1" v: G; ^' c* t2 K( r' F( l6 U- g
        else:             # 递归条件
    & {- V4 u4 n6 p3 y/ j! {5 q# a1 N        return n * factorial(n-1)" G  D# i4 v. r1 i0 S% f% ]
    执行过程(以计算 3! 为例):3 T$ a4 h% U, b1 P
    factorial(3)
    - r* }2 G/ a) G" e. L) l, N2 o3 * factorial(2)# `" ^, v. R6 X; C
    3 * (2 * factorial(1))
    " X: R/ D4 O) H8 c+ b/ n) I6 [1 U3 * (2 * (1 * factorial(0)))3 _1 _5 \4 j; C- W: H0 G6 O5 n
    3 * (2 * (1 * 1)) = 6
      b' K. j# H2 s5 ]: U% S6 `7 m5 h, ]$ h1 [- J& O- Z. s
    递归思维要点
    1 B; F& l: ?. j: S) j# c0 b1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    4 F0 n* c9 M4 y4 e8 C( t2 j8 W8 `2. **栈结构**:每次调用都会创建新的栈帧(内存空间)$ e4 a' D( u% A2 b4 p- |8 p  z
    3. **递推过程**:不断向下分解问题(递)
    " {2 R" }* N2 _: X3 {4. **回溯过程**:组合子问题结果返回(归)
    / M5 D/ u4 \) L, E4 j+ r) }) D+ k4 O" D: x6 t! }
    注意事项! b# ]; M2 _) F4 u; i. `
    必须要有终止条件
    " ~/ F7 p0 t4 H递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ; h# D7 k7 t% N某些问题用递归更直观(如树遍历),但效率可能不如迭代' p2 P* [1 J( G7 W! S. |
    尾递归优化可以提升效率(但Python不支持)& {1 g6 v: m7 |. U, {5 `

    5 h+ t2 j, f" I; @* F) V: d 递归 vs 迭代, I( h. G2 Y, H! g' ]) [
    |          | 递归                          | 迭代               |
    1 j  p& H4 `$ f5 Z+ ~$ `|----------|-----------------------------|------------------|" o8 B# q7 G+ ^* a; X/ J2 Y: \* [
    | 实现方式    | 函数自调用                        | 循环结构            |
    : N& z9 y/ l% B6 ^" K& f$ M| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |* _' P1 X% P; P. N
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |) V; m# i( `- n  w
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% L; r8 \- n# j- Z
      o/ C/ ]+ |3 g
    经典递归应用场景, k$ {2 l1 h( {, a
    1. 文件系统遍历(目录树结构)4 r) K0 h7 ^# g  z- J; N' E
    2. 快速排序/归并排序算法' J" ]: t; t: N) ^' m9 k; E
    3. 汉诺塔问题7 R+ c0 Q" O! U. L- f# T
    4. 二叉树遍历(前序/中序/后序)
    & L$ a% l3 N4 T5 [- B5. 生成所有可能的组合(回溯算法)7 P+ }$ \$ j0 P- z8 G! x- |" J8 Y

    4 H9 a0 V5 v# {7 B4 T& r* B试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    / P0 |3 ~# X& Z# E; Q) T" X) ?5 i我推理机的核心算法应该是二叉树遍历的变种。
    " A7 r( R* J- v5 ~3 f7 J另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:/ C; e; u( r* I' F( ]0 a
    Key Idea of Recursion
    * B) |% M! u3 ^5 v" d4 p4 c; u4 [" g1 I% Y3 u$ m
    A recursive function solves a problem by:
    9 Z5 j' l) b! R- ^2 Z. }) K
    0 j9 i7 ~7 _# A: {& b9 J: P" r    Breaking the problem into smaller instances of the same problem.
    5 L9 y, Y4 z- v/ K
    - e: z0 K* F* N" l0 g) ]* p# {    Solving the smallest instance directly (base case).
    0 |/ ?, ~( S0 m0 q2 W. G) }, @- l* t" M. n( ]# X0 [# a1 d
        Combining the results of smaller instances to solve the larger problem.0 I: H) x/ p$ v% r9 Z
    : n# X( G' h- |) g
    Components of a Recursive Function# y; \: b6 D' p/ Q/ r% b# a% j

    & I8 r# h& U8 @" e; r2 t* T; {! N4 W    Base Case:
    6 T* h  L+ [( @% s0 P+ m2 c7 Q( m4 Z1 j1 k+ M$ r& s$ j  J& ]
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.) E+ _8 @. [1 ]8 j* z+ A
    ! x4 c9 U/ ^3 v( K
            It acts as the stopping condition to prevent infinite recursion.- L3 C! V3 p4 C( d, S& j

    ; f7 K( v' p4 U! ~* d6 o        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.4 K2 O' p8 c3 V, P

    8 F9 i( U% s; a5 u% [( t' x    Recursive Case:
    ' ]/ N1 X3 }- x! ~: X" A' \4 i
    - c8 V! K- O! O5 \! C2 p3 o        This is where the function calls itself with a smaller or simpler version of the problem.
    ' Z$ x8 }, p( n0 [- g  Q5 u! C) X" d: o! i. p8 g+ d/ D, M5 p. d
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).+ f5 u3 q+ v) h2 h
    2 T/ @; d' a  i
    Example: Factorial Calculation
    ' l3 C" }9 ]: {4 \5 p5 q
    5 u  h6 U1 J$ `+ N$ m) D. hThe 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:
    * X1 J5 ^4 X/ K" e. `6 A) H: s
    4 G; N7 s+ x1 y, [* H/ ]; C+ V! v    Base case: 0! = 1- g# n% V5 l5 ~1 S$ Y3 p1 \4 P
    + D0 X: `6 f: v" D$ N# C0 p
        Recursive case: n! = n * (n-1)!( @2 j- a' J/ \+ w3 @9 @
    7 h- g! d6 n# X
    Here’s how it looks in code (Python):6 B# l4 ~! i: ]4 C8 F7 P& }
    python
    / R/ G' v8 x3 \  L- S4 Q1 m. u; [7 ]7 b' U! T6 b' `7 k( x
    - r. y3 ~7 y$ d1 s$ \1 p' n
    def factorial(n):$ I# w) Z! h: |' o9 Z
        # Base case  F" [9 |) E9 E# ]1 b5 u
        if n == 0:
    4 I1 D- Y3 t' G# d. u        return 1
    - L+ }4 X% r/ \3 S    # Recursive case
    / T7 M4 w" b, J* e9 H7 E    else:! o5 L9 M" B" R2 q
            return n * factorial(n - 1)
    * K- B7 H2 T$ m) R2 A7 g' l" O! \. ?+ n9 H$ I$ }. F
    # Example usage1 D, x# [0 X) X( p3 Q
    print(factorial(5))  # Output: 1201 e$ U) ~- n6 T. w& h1 l! x

    5 v% N/ Z0 m! B6 L' R1 Y8 k3 vHow Recursion Works
    1 g( L# [1 X( |+ \; C% V, V" e4 F4 s. {
        The function keeps calling itself with smaller inputs until it reaches the base case.: {0 c/ Q/ E# E

    8 p$ I; d( c8 y  j1 A, E    Once the base case is reached, the function starts returning values back up the call stack.& H) u0 e# B( t8 w; Q% D

    2 d! J0 r+ P$ Q6 T/ c. K9 s  x  g    These returned values are combined to produce the final result.8 z+ |0 I6 P: D5 ~" f

    " I' M4 Q  f+ L) tFor factorial(5):2 }2 O. g! d" s) w5 f* c+ Y
    1 y! R1 v( b+ y& ~( K% z

    & w2 i& T, ~& P0 t# Ofactorial(5) = 5 * factorial(4)
    5 t+ k/ ?  M' b/ lfactorial(4) = 4 * factorial(3)0 F0 H3 P5 r5 }3 }8 i
    factorial(3) = 3 * factorial(2)
    . ~5 l: N. t; l( i- Lfactorial(2) = 2 * factorial(1)
    3 ?2 ^. Y7 w( k+ x' ^factorial(1) = 1 * factorial(0)
    3 I( Q4 O+ g5 y5 h9 |factorial(0) = 1  # Base case
    $ o3 K& K. I2 S  d9 p3 X7 T0 h) U& D' w# {
    Then, the results are combined:
    9 w& e6 J+ @* C& W+ M3 V" |; j9 v9 q  C% e( m" \
    0 A" i: z3 x, I8 {) K+ A+ p
    factorial(1) = 1 * 1 = 1, Y  G  I$ ^; c1 f) P$ S
    factorial(2) = 2 * 1 = 20 A8 f* Y" a7 l/ Q% ]& T
    factorial(3) = 3 * 2 = 6
    : Q# Q; V. j& A+ h4 q4 A: Dfactorial(4) = 4 * 6 = 24, w2 U5 ?' _; @
    factorial(5) = 5 * 24 = 1201 ^+ r6 _* {2 _$ @; ~" G# @
    " @; y5 ]  U. ~2 }/ X; y
    Advantages of Recursion: q7 y; q/ O- D  W$ ]" d

    $ P5 n; L3 [* D    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).& }$ t6 z" S: l  e  f

    / _% W8 m3 L( T6 V/ p4 Y- e. m; P    Readability: Recursive code can be more readable and concise compared to iterative solutions.) a# {( {  A, E8 h% q! M4 n

    ; b- `: n5 p3 w( \Disadvantages of Recursion- F9 A7 q. }, [4 e. j
    + ~5 x4 z, a9 ~' }2 M
        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.( Y7 j9 z: K: d' v$ B* P0 Q

    4 \( C5 _; e0 q    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 O; Y+ ^+ A/ b6 e3 Q  _, x; ?
    6 }; i- V8 S2 {4 w8 y/ i% U
    When to Use Recursion+ s: r) F+ ]1 W+ w9 |1 p) L0 F

    2 [- G% y2 G/ R* ]1 q( v$ r    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # x- w6 k- _5 ~; y" j% i- s9 Z5 X8 [2 O  v( Y+ E
        Problems with a clear base case and recursive case.
      s9 Y. L8 P/ J9 W/ \5 N
    * t. ]/ J! [5 v: x9 `+ IExample: Fibonacci Sequence* I5 P8 _- K& h% K# X* k

    , X& u9 r# ^+ U8 b* L, p; g- _) jThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ! c# _5 ^, ?8 d) ]
    & p: ^& m, `/ q3 I0 U    Base case: fib(0) = 0, fib(1) = 14 [$ S4 d0 H7 h3 t8 d

    2 o2 D, l8 i+ w* t    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . \, j+ Q. I2 Q" y
    : m, U" }- U. q$ p; ^! K" mpython2 S0 [. m2 x/ H) w2 [
    , A: A0 }5 y1 B7 I6 g
    & q0 U* h$ U* [
    def fibonacci(n):
    ( o) b: }: t" k7 l% E5 p    # Base cases$ t: k0 z: ]- ^: W# t# Q) S4 Y3 `
        if n == 0:6 B+ c5 O5 O7 j. i7 M+ s1 k
            return 0% U2 C  R" N" C' F, |; T6 M
        elif n == 1:2 E3 {& w5 k  ?* @. @  |, r( v
            return 1- S$ L% k) o' y4 e. O# W
        # Recursive case
    3 \  @% ^+ o# |, D: y0 ^    else:8 {2 U) [/ E$ ~
            return fibonacci(n - 1) + fibonacci(n - 2)& z" v7 r! X5 f/ z" _# i

    " E7 B8 ^& @; f8 B5 E# Example usage
    / s% ?, m0 X0 N" V* M! {. cprint(fibonacci(6))  # Output: 8
    1 K7 z) k  y1 p7 x
    , ]. c" S5 s* I& n! J) ITail Recursion
    & ?' n4 w2 h1 r. N% Z
    $ I5 e( ^; o9 \* Y2 gTail 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).
    1 {4 c2 R6 C/ ~6 T+ T8 i) S& ~4 e( a7 l3 ?
    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 15:18 , Processed in 0.034995 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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