设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ; v9 {4 ]- a$ d1 B1 s0 }; c) t
    ' j* {7 @, t$ l! ~5 \
    解释的不错
    * O# f% k8 l, Q& [1 l* r
    6 s# S$ ^7 U1 f) n) X9 ]: I- A递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    / P8 s0 |' l. }3 Z# F7 D  N/ {0 W1 o( V' X) W+ ^
    关键要素
    9 D! B" L( m& b. P1. **基线条件(Base Case)**+ Z7 r$ C. H1 m- p
       - 递归终止的条件,防止无限循环
    ! A$ l( l% F' t3 n4 c   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ y/ F; `. J: L$ p& C( Y9 y
    ' F9 Y3 E, K! q: t1 Z: ?0 t" y
    2. **递归条件(Recursive Case)**
      c6 ~8 i8 Q! K$ B9 A# E, O( J& @   - 将原问题分解为更小的子问题
    , A4 Y! @4 `! {4 n   - 例如:n! = n × (n-1)!
    # n( p+ O6 d3 i7 d3 \$ r& t% L4 V/ b
    1 i/ c: @  I+ s 经典示例:计算阶乘
    ! L- x- A# O$ v$ Zpython
    . C( o7 z9 Z( ~+ g: k( Pdef factorial(n):0 o7 D1 v8 r& G! N) A8 X# H5 h
        if n == 0:        # 基线条件' p% q' Z& O- c. d' U
            return 10 V; L( o4 q( Q7 N% Z
        else:             # 递归条件7 O# ^# ^6 D0 E/ ^3 e$ _7 r- x1 W
            return n * factorial(n-1)
    " `5 p! r: n: m, `' y! B执行过程(以计算 3! 为例):
    2 M7 y8 a" }" X/ ?7 W, U2 }3 Xfactorial(3)
    , f# Q  W8 F/ A' \3 * factorial(2)
    9 A* V# Z1 S9 ^5 b$ R! j3 * (2 * factorial(1))
    ) F+ S7 O( e" c+ X, h3 * (2 * (1 * factorial(0)))) S6 x  P, {' M( n/ \" ?
    3 * (2 * (1 * 1)) = 6  q3 G8 Q7 A: G* R% t
    ' X( r: r9 }2 W; t$ n
    递归思维要点
    5 S) o) n' e' ^6 v5 \/ h1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    2 p# I0 H. O- ^2. **栈结构**:每次调用都会创建新的栈帧(内存空间)) H: f  R0 x2 i/ F" S  m
    3. **递推过程**:不断向下分解问题(递)* H# p5 q/ {  a6 R
    4. **回溯过程**:组合子问题结果返回(归)
    % _; \( U7 u9 ^( i* G" {$ s+ ~$ U9 }. s  h7 x
    注意事项
    1 ^4 {  E. R& p8 b& M必须要有终止条件  j( t1 \6 [! R3 N. R
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)2 m, j" h, u! Q: O4 |7 k7 K4 i- B
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    4 J, H/ {# W- T5 ?  B1 B尾递归优化可以提升效率(但Python不支持)
    % _* n3 j; Z3 ~: n
    / q* e4 T) X) [% v# x 递归 vs 迭代) X, s) B/ X& c; l9 M5 Z
    |          | 递归                          | 迭代               |
    % x5 a; N, T/ p5 U' t|----------|-----------------------------|------------------|
    4 E. U; V8 ^3 z$ b( N  {| 实现方式    | 函数自调用                        | 循环结构            |" S- f& M9 @  z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    : ]5 k. g* |. h) |  r% v/ L* V| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |* m3 I7 ]( {, E' w9 T; q- a
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 f: g$ t  `( H& i

    8 u/ I7 r+ _7 E' I) k  Q 经典递归应用场景
    ( \6 c6 \. C  [+ ~1. 文件系统遍历(目录树结构)% l$ z) V: C# w& [+ ^+ g+ T! k% S7 H
    2. 快速排序/归并排序算法
    8 ~1 X/ O- E9 s% w5 [* e/ G. q( [3. 汉诺塔问题
    & \  e! J! A# z% |+ s* L6 \8 u8 }7 v4. 二叉树遍历(前序/中序/后序)
    ' K; g6 _% U2 J' t( d5. 生成所有可能的组合(回溯算法)$ f  Q) A! N- ]

    8 }# D7 j- V0 R' ^. f; E+ X试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    18 分钟前
  • 签到天数: 3215 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# i& J/ a0 d9 g# C5 }! h* V: p
    我推理机的核心算法应该是二叉树遍历的变种。- ]( j+ @0 y8 Z) f; @. |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . ~' J: {. d: h/ Y! V* ?+ x: cKey Idea of Recursion
      X2 k% \8 K/ u- |7 g5 V! q* d% l% U! ^# G
    A recursive function solves a problem by:. b: j* j; t0 z4 C
    # t7 R, G6 r4 d7 K5 _0 M1 X4 C
        Breaking the problem into smaller instances of the same problem./ \$ |- t$ h* i* p( U
    - a& \5 T: }* ?# I
        Solving the smallest instance directly (base case).
    , W# f6 a2 F9 d
    ! Q5 I3 `: r# d# z) K2 {    Combining the results of smaller instances to solve the larger problem.
    ! A# ~( y' S3 {+ ~, k9 t3 Z; h9 n
    6 i( e' l$ u" m$ D0 a* b6 V/ u  CComponents of a Recursive Function" F+ _* Y% r: j

    2 g- o7 s6 D7 Z5 P8 W. F% L: F. ~    Base Case:
    / A1 e! c+ g6 i/ b" c2 B6 v9 x8 O6 p  l% `7 b
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 k4 a; j' M: |7 F! b! O1 ?& R9 @! }( F( ~& c+ n
            It acts as the stopping condition to prevent infinite recursion.
    # C" ~( O5 y/ N/ o' I% o+ L- K  }5 i/ z- g
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    : N0 z# p8 k) ^9 y0 m) B
    7 n' ?8 e0 U; R2 r7 u; k/ V9 k    Recursive Case:
    $ E% I& u8 y  w" \6 f8 e
    " M/ C  X( v9 T' }& v4 v        This is where the function calls itself with a smaller or simpler version of the problem." K- h, X# I9 y8 @) M1 e* C0 p& p

    9 h$ g1 J' f7 {5 Z  j9 L! `        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).* @) T6 `# g' L7 c
    7 P+ G! {7 k( a& @
    Example: Factorial Calculation6 {9 |# ?: C+ n6 ^9 j, |0 N

    . E% }2 E& N9 G, d) |3 _! {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 B, C& q5 g# z) a' G2 d
    ) s( w# y5 b' `8 K    Base case: 0! = 1
    6 V) b: ], v- z4 I' {6 T- A3 Z% M( `8 u( T5 z% S
        Recursive case: n! = n * (n-1)!
    . M8 B; Z" j$ @! y
    3 p% Q- @0 {  c/ QHere’s how it looks in code (Python):: }* ~2 k. Q9 B$ C
    python% i5 S' s2 P' S( @* F+ Q1 `# r
    6 X7 z0 T$ N: m* d) V; W: `, a$ E
    0 \, |" R: ~3 i  e7 T# b2 s
    def factorial(n):( ]2 N3 q( D$ I3 I6 E5 N
        # Base case* L! @( X; C5 Z+ `2 H: C5 `3 r% U
        if n == 0:
    " G5 F1 m) {$ P/ Z3 C# F9 N9 M        return 1
    ' Y( p1 x+ {# `$ }1 Y0 H    # Recursive case! a1 p* R4 V4 Q4 e
        else:
    4 n. o  R7 c9 }. u, m7 C# D- G        return n * factorial(n - 1)
    & a& Y. M& o4 A4 a3 R% B0 R, Y9 I+ Z/ J5 f2 U
    # Example usage
    - @/ W; Y1 ~+ _1 T0 ^print(factorial(5))  # Output: 120
    0 L5 e- L- s1 G  q
    5 u7 d" U' M% N1 o2 d! xHow Recursion Works) n1 Q- j8 ]1 M2 J/ B" o

    - f- a3 }/ d* O    The function keeps calling itself with smaller inputs until it reaches the base case.
    $ w1 m4 O. j+ V% K6 F+ U& S& L4 z2 S/ d, e2 k; {. s# H. ?3 O
        Once the base case is reached, the function starts returning values back up the call stack.3 K4 r9 o3 n* Q- X5 I( X$ H. v- E
    / Y6 x+ N$ q5 K2 f) n
        These returned values are combined to produce the final result.
    : s9 A5 B" p! A2 p  c0 l9 d. ~& d# z
    For factorial(5):
    8 {3 u4 u3 B% H1 y/ r  w
    * `9 R, S, u4 w; j3 N& g
    . S6 S& ~6 k1 s$ q2 d- sfactorial(5) = 5 * factorial(4)
    5 p, X8 }/ C+ [# x, I2 `factorial(4) = 4 * factorial(3)
    ) }. I( b9 [( ]1 d) X  nfactorial(3) = 3 * factorial(2)
    ( V6 c" K0 O5 I3 ~2 P2 \  ffactorial(2) = 2 * factorial(1)
    ' O& `* x0 B$ o0 T( H  Vfactorial(1) = 1 * factorial(0)
    * e6 |, U% x8 c& Yfactorial(0) = 1  # Base case
    1 s7 @  Z  n9 M' i( D8 F% r5 J) ~' E7 ~  S
    Then, the results are combined:+ D1 Y6 J9 v: z6 N% g0 i8 G

    . r8 h5 o$ p! U+ O: X2 x9 L
    & G% g6 O* w. H2 `, Ffactorial(1) = 1 * 1 = 18 q5 t; l2 ~" c7 g
    factorial(2) = 2 * 1 = 2
    / c7 N& T8 s: @; l. ]factorial(3) = 3 * 2 = 6
    . O1 f/ p4 [6 Kfactorial(4) = 4 * 6 = 24
    9 D9 d/ d5 G/ V' mfactorial(5) = 5 * 24 = 120/ c2 Y0 l) E( I8 o3 p: o. i
    " k3 Q) o# w! A
    Advantages of Recursion
    # {! B2 C/ K5 d5 H) ]7 B7 \3 ?; L8 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).) R$ ?: s5 ]& P

      y" O: y- I+ c% o: G    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " U6 Z+ n2 z9 m. w
    1 M: B# d+ c" H* ?8 }% q, qDisadvantages of Recursion/ V# L) m/ E# E- A7 y6 L

    # s# k1 E, L% i% M& N9 E    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.
    ; \# H/ j  z/ v" @  c* [
    / t9 b$ Y& V2 I3 G) c! R$ E    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 H7 T1 R. ~5 y5 s; _
    ! J. w6 q( r3 i: c/ {1 E
    When to Use Recursion
    ; n" G4 y5 g8 u3 A
    - @  G1 V5 q: A! U6 j    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).1 Q) V  @* N% M6 P0 l& f# O
      S2 d# w  V- I& x6 @7 Q& G2 c
        Problems with a clear base case and recursive case.
      C1 z. K1 m! J- n- ~: s7 \$ a( R: C8 y! @2 _
    Example: Fibonacci Sequence. O6 [/ ?4 _; |4 \# m+ Z

    2 a7 c( }8 x$ X3 i" u: `The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:, O2 L6 ?( u  ]8 _2 F6 K8 y0 ?
    * Z. t- U3 h3 i  M# k% k" h0 F
        Base case: fib(0) = 0, fib(1) = 1
    + C7 _8 a1 H( B+ c$ a# G1 _* ^" h' k  L
        Recursive case: fib(n) = fib(n-1) + fib(n-2)9 u3 ^$ y, b. I! n% f, g  K, h% H7 n

    & x% w8 V9 S$ apython
    1 E, B/ [/ h) c- [0 B+ @, y" c
    4 F9 g' P+ L" G7 z
    * k. x4 e/ p9 S% a4 P( W, g) zdef fibonacci(n):0 v' V/ z8 ]2 C7 w
        # Base cases
    ( m1 L) G0 S7 |& i: c( X    if n == 0:1 Q$ _0 B3 e! p) e* j. i0 ]
            return 0
      F) F+ S# m. R& W    elif n == 1:
    ' h6 G! v% H1 H& I6 }        return 1  J( L- U9 D* _) f& D& c
        # Recursive case* o1 J) e% Y8 {- ~
        else:0 ~) L! V5 i& d0 q7 Y) p
            return fibonacci(n - 1) + fibonacci(n - 2)
    0 u# z9 C% {4 q9 z# ~( y/ Z- q" a& q" S) p3 U0 x$ I
    # Example usage& e7 @; W8 f+ \- U. A: ?% S
    print(fibonacci(6))  # Output: 8$ f1 b4 h' X; \
    ' Z' Y6 u0 F, z3 a5 b, ]
    Tail Recursion
    " u- T% X, k; D+ n  Y7 v" \
    / Q+ }) r* M7 W/ rTail 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 S- m+ M1 ^0 ^/ Y5 r
    ( f2 C" K' ]9 s' s
    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-4-7 07:15 , Processed in 0.055676 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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