设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ) B$ O% r: e, U, ]( r7 V8 P" C

    3 y: P  x% M5 t* n9 t  N8 @0 p解释的不错
    & [1 F8 F' A& I3 {# f' K6 g* L. L/ i. Z5 ^  ^
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ' S/ r6 K5 Z2 A# f: |7 H# B/ e& V# F2 [8 Q' J
    关键要素
    ; Y7 f# i9 `5 j/ B# H1. **基线条件(Base Case)**
    4 ]) W/ w' p' Q3 H5 E- A1 u' q$ |   - 递归终止的条件,防止无限循环
    6 W; I. d% d  m- q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1* q5 H7 m9 R' i8 Q0 C$ u

      @; t" e, L( e2 N- C% m2. **递归条件(Recursive Case)**
    6 Q2 @7 @. \2 \9 @& v% A   - 将原问题分解为更小的子问题8 p. c2 v6 U) d: A) b
       - 例如:n! = n × (n-1)!  H! B+ [8 s. x4 S1 `

    ; a) v* C% x4 A, G' p4 Z 经典示例:计算阶乘
    ; Z0 d: t. _! ~& v  Wpython
    " t/ B. W5 Y% V* D& Q  Edef factorial(n):8 H" n: J, I2 q! N/ j
        if n == 0:        # 基线条件8 Z1 W, [2 S& T7 H' l  _, {) w
            return 1
    . |- H* x+ g- I  l1 O    else:             # 递归条件
    0 L6 `3 I6 L2 E' c6 ^" n3 T. l3 H        return n * factorial(n-1)5 R% v' B' n+ {6 l) H
    执行过程(以计算 3! 为例):8 V  N& R& D5 M7 f/ u% g
    factorial(3), v" K6 ^* T, K# Y
    3 * factorial(2)
    9 C, B3 u  a( c. U- L* \  q! \3 * (2 * factorial(1))1 }* q, W. c6 O% R& O
    3 * (2 * (1 * factorial(0))); S: Q3 }) D9 q8 V! O# w
    3 * (2 * (1 * 1)) = 6
    ) f8 G* o* K/ @$ N2 ^; l& K1 h
    & ]8 N& f$ W8 g3 G9 Z6 E 递归思维要点5 D  [8 ?+ \% |  q: {. Y/ o
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
      c# t0 g3 X; i8 P$ {+ A! }2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    . r# i% ]4 w9 m3 e; m. R! r  ]3. **递推过程**:不断向下分解问题(递)" R% F5 z( P7 V; a; _+ m/ H
    4. **回溯过程**:组合子问题结果返回(归)6 A2 Q+ `2 R& |" O* l1 `$ H
    3 L* n5 \" P. G$ ]" H& b
    注意事项
    ) B8 D- b( |6 [; I2 }% y/ O" l必须要有终止条件  J" b! n) q0 z, b4 Q
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)  r1 ?7 x6 ?0 y9 G, ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代: R: U* [+ G# L" W/ t
    尾递归优化可以提升效率(但Python不支持)- C; i6 @4 I- i

    . @7 i& Y. [- v5 m& v/ V 递归 vs 迭代* @) q* {! \! p  a
    |          | 递归                          | 迭代               |
    8 a/ g/ U1 c  [/ ?& `, r|----------|-----------------------------|------------------|8 j, M: [% V$ A; T" g( ~" F- T
    | 实现方式    | 函数自调用                        | 循环结构            |
    ; t& t  D+ t2 i$ r9 }! \| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ; D) h  l+ n2 W1 B. \3 S| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |4 U) A- T* k( J1 o
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    , C& d: W0 p  h& [9 {' D% |% H  l! }" X4 E( B0 x
    经典递归应用场景
    + j; \! N' M0 d& U1. 文件系统遍历(目录树结构)
    / u0 w( R0 f2 U1 B# ^, w7 |2. 快速排序/归并排序算法
    3 h: T6 H, Z; q# @3. 汉诺塔问题  D* S) A7 J" c& N0 E
    4. 二叉树遍历(前序/中序/后序)
    ( Y+ u! s! b2 R  ^* C) ]9 J5. 生成所有可能的组合(回溯算法)
    7 j; N9 q+ p4 p; u
    . Q/ y- t& h$ G# O试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    1 小时前
  • 签到天数: 3120 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ L4 R8 }' k& B6 a  W( D: f  i8 T
    我推理机的核心算法应该是二叉树遍历的变种。0 G- e5 s# W. C1 N9 C! E
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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# F. d7 Z' bKey Idea of Recursion! z$ b0 B; e9 h! g/ {/ d8 t4 F3 q2 h
    5 P( Q+ M( F0 E6 f" d$ y
    A recursive function solves a problem by:( Z6 f3 I, z& \
    6 ~. z! w& K2 [$ X# ?9 l# y& j
        Breaking the problem into smaller instances of the same problem.8 b1 [. q; e( B  K; V

    ' R  d0 X$ I7 n+ y9 j( d    Solving the smallest instance directly (base case).
    - p+ F& `% x2 {% {+ ^. L
    " c' `: x4 |( Y/ B* s! c    Combining the results of smaller instances to solve the larger problem.
    * l/ x. K, r6 R7 y5 d
    - N; A" r" U$ ^9 K* wComponents of a Recursive Function
    * p$ [5 p+ W3 J$ F3 w! `
    3 J* @) A! K) v7 X: Q- P    Base Case:2 e+ }  G, t5 z2 b6 z9 y
    # y9 G7 ^; ~' Z5 {, G
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    4 h4 u  p2 r  s  n
    " d6 x0 c1 f- l% w* O9 c        It acts as the stopping condition to prevent infinite recursion.
    1 }* a: L+ I$ B/ U+ t/ s+ q' _( ]1 Y# |! r
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; @& O' U) v. d$ U) M( h1 w7 |9 [9 u! z- }
        Recursive Case:4 G* _3 n# C; S1 \( W( G) Z6 R+ G" y+ T
    , r& x5 w2 i7 [7 m* y
            This is where the function calls itself with a smaller or simpler version of the problem., j' z+ D4 ?4 Q: K
    & V! i$ h4 _5 ]- o' o2 p4 V
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 T9 b1 Z! C# P# D0 \) z
    ; a8 d: ^- B! |  R
    Example: Factorial Calculation
    3 ~% p' |6 |4 K# {  e
    3 y" @1 v' s4 V3 SThe 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:/ |. }8 n+ ^- R

    # B! O/ D( k; K$ d& L    Base case: 0! = 1
    . r) b" j: e4 k5 M" c# H
    6 K' f9 d2 I: {: \( j# F4 q& j1 P    Recursive case: n! = n * (n-1)!# \/ N' v$ f8 J6 Y

    * I0 g  @4 N# m3 SHere’s how it looks in code (Python):
    : y3 S" D3 z) H) t# ?- Z& B8 Hpython
    " k: ?. d, ]& I  A- H, Q. M7 ]8 i. V' j% s- C# n
    ; A$ w) C1 K# C2 ~" d* u$ V8 x3 g& r
    def factorial(n):
    5 m9 C) I, k( m    # Base case0 b$ L3 b1 ]* |$ L
        if n == 0:9 S7 I/ S( X2 d6 G7 ]
            return 1
    8 S. ]4 W# ?2 r/ x9 X$ F    # Recursive case
    1 n; b8 m5 `  W( z/ t+ \$ g# f    else:
    9 b7 g! ~( z" t        return n * factorial(n - 1)
    3 f5 Z7 d( ?) L' U( l+ k, |' c# L9 S2 R
    # Example usage/ Q% T9 W' Z, ?$ K/ i8 W# J
    print(factorial(5))  # Output: 120' v- z, u1 i- b1 C5 v

    4 a% h( E3 U$ W! e9 ~, yHow Recursion Works7 P% _, j" ^/ [8 P! P0 X$ c) P

    & g1 s) Q5 Z  J+ v7 I. q+ E+ T: s    The function keeps calling itself with smaller inputs until it reaches the base case.! B6 m' e+ S' X/ ~. f6 v4 [# K8 S  X
    ( f0 `2 K1 h+ ?$ i
        Once the base case is reached, the function starts returning values back up the call stack.6 N" y, s- R( f: X  r

    , y2 P0 z2 s5 P: m* d% R    These returned values are combined to produce the final result.
    . K# w, |/ p+ P
    4 R- l- @& g$ V4 V; E& p# |For factorial(5):/ C# Z# w& r% B4 Z. T& k5 T

    ' \/ i. Q, w) s/ p6 m' \, F
    ( w% p) s$ ]7 xfactorial(5) = 5 * factorial(4)" Q; o4 I# a8 R
    factorial(4) = 4 * factorial(3)
    ) E  H3 Q: m7 F: C' yfactorial(3) = 3 * factorial(2)
    4 T0 v) J3 j! s+ jfactorial(2) = 2 * factorial(1)
    & e4 J9 _8 I& L2 Ifactorial(1) = 1 * factorial(0)% z6 j* b0 p; g1 `: a
    factorial(0) = 1  # Base case2 d, m& M  y& M: G4 K
    . Y9 u4 m6 E! G$ r3 O
    Then, the results are combined:
    . J6 v$ g# z6 Y4 ^
    ' k, p1 |2 D) b
    9 m/ a. ^3 ~3 Y6 O+ Afactorial(1) = 1 * 1 = 10 Z7 E% p( h6 O( b& s9 ~; E- m
    factorial(2) = 2 * 1 = 2
    3 {# c# Y9 Y9 m* N: z: mfactorial(3) = 3 * 2 = 6+ L, Q8 ?) i. z, F
    factorial(4) = 4 * 6 = 24
    5 w; j0 g, Q; r: i! R  F1 Nfactorial(5) = 5 * 24 = 120$ A2 s% d7 M6 g8 d& f4 ^) a' D9 d

    $ t' y0 M& x6 `% G$ x+ L) @. LAdvantages of Recursion; X6 K" H5 _+ o* C( g2 K
    . ]% d' b( k+ F  }( W
        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).6 {5 t) z6 I( x5 @  A) p3 W$ W
    4 X. V; P$ H0 O. H
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    0 D* J. |3 B, x2 o+ _$ F) u. A+ m8 N
    3 a  D8 [0 V8 G8 f2 WDisadvantages of Recursion8 e% S8 ~/ I' _& M4 r& M9 z
    : _  j) u  W! N( @' l6 z- U
        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.
    ( G  g& \) u5 E& ?, I: g* P7 W) A" e9 r
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    4 _* L, i" B) \2 v/ y) H1 @0 f/ G  N" F1 Z, v) @  v
    When to Use Recursion. T! r: X( H2 T6 W; w! Q1 ]
    3 M& x& Z5 f- y  K- p( D6 s
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    0 C5 f- x. h: y" F( q! b
    6 G+ I% a8 e* a" N/ Y  X* o    Problems with a clear base case and recursive case.
    / h( a7 y- C1 e  b7 e
    0 e# z7 p5 E: T  _' yExample: Fibonacci Sequence$ X$ k5 B- W3 U, a- H
    / U- L: `8 ~$ U" V! M& o
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 m1 a6 m3 {* y, p- B
    0 y. \$ e# o; U    Base case: fib(0) = 0, fib(1) = 1
    1 s' [. _" p$ e3 h; z7 m1 W+ v" t5 k/ Y' s, {* P" f. W: q( j
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    0 o3 g6 R1 J% c$ ^2 l
    * R  m2 U2 [/ L7 U: d% w! N' h- ppython
    ' T  M, |# ~: }$ ^3 f; b! D, g* B3 n2 ^8 ]! O6 v1 X, I8 q

    ( Z8 q4 u$ e  x( fdef fibonacci(n):
    ' E6 _# ?# J: n$ P( E. n, Q    # Base cases0 F$ @# }' N; w+ I
        if n == 0:( I+ Q$ P( y0 v5 ?4 I" [
            return 0/ N) L' C# y: t' Q2 j6 F/ h0 g* m$ ?
        elif n == 1:' X. `& L; V4 y# ?) Y1 a2 E
            return 1
    2 c& |7 b7 v* d/ A: L8 |+ C/ l# D1 P    # Recursive case3 y6 l3 x9 U( y5 X
        else:  |8 K; v$ ?5 J% H2 h
            return fibonacci(n - 1) + fibonacci(n - 2)
    3 |( w" a/ Y, W9 M! R/ l
    2 x1 L& r: H" Q% v& H( D; O# Example usage/ F* x( u! p4 f) P7 G, a
    print(fibonacci(6))  # Output: 86 n' @! U4 l, }5 U1 e

    % v( s8 a% S+ ~  q+ kTail Recursion$ G: a( G8 m& j; }7 I0 n
    7 u$ W* d8 N# U: f9 `8 s" C* v: \
    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).6 A# R/ v4 G/ C8 i& l0 w9 Q

    9 F( m3 w9 u$ P6 R3 K" [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-12-17 08:58 , Processed in 0.033958 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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