设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 K/ z$ G& j& I) z8 t' r
    4 ?* [& Z6 |* V8 V8 [+ e; B解释的不错
    4 C7 U7 C( v+ D" P" ^- ]# m! K% ^1 {( i4 M! p  N' o4 n+ H
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。; [( k) T) S) S2 V

    4 Q2 t, G- C2 t: G2 X 关键要素
    ; l0 W0 b, i7 ]5 ?2 w, c& T8 @1. **基线条件(Base Case)**. z2 q+ D4 m% l* _/ T8 ^
       - 递归终止的条件,防止无限循环) [  Q, J' Z* s9 O2 p6 Z" X
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    % \3 N( E% a* _$ e& ]8 C: m
    3 Q* r* H, T# ~: z0 V2 ]  G. p4 f2. **递归条件(Recursive Case)**
    ) Z( i2 _' S2 n( y  q* R   - 将原问题分解为更小的子问题
    $ L* V3 Y  e0 Z1 z% j* N2 i: d. c   - 例如:n! = n × (n-1)!
    7 c4 I- Q$ ], X  t2 V0 C" M3 o" F" o. K$ p/ z
    经典示例:计算阶乘
    0 o; Z* l# y" x( P9 \python! _, X( s7 Z# b& L6 q7 i$ N
    def factorial(n):8 K$ a" c" s- E$ g
        if n == 0:        # 基线条件
    0 U2 O1 A; \* I8 W1 ^9 B! Z3 m: c  x        return 1- O& ], y. t/ ?! k/ g  h- h
        else:             # 递归条件
    6 A2 ^) p. L# g; a9 W        return n * factorial(n-1)
    & }# A3 _4 U0 U/ U! n2 q执行过程(以计算 3! 为例):
    & ?! m2 m3 G2 W! M+ `/ k" lfactorial(3)
    & }+ |+ |$ a* D& k9 C4 U3 * factorial(2)7 Z- }& |8 _/ e" e; [+ Y
    3 * (2 * factorial(1))
    - F- y9 k0 N" R" U3 * (2 * (1 * factorial(0)))
    ' f; V) c3 a! Y2 e5 t" @$ O3 * (2 * (1 * 1)) = 6
    9 S9 w3 _3 `1 n6 `0 m' |3 ~6 W3 `" Z; X! P5 a- c- }% y
    递归思维要点
    + a  s( T* v- F9 J% z1. **信任递归**:假设子问题已经解决,专注当前层逻辑: }! K2 K; B( Z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 m) V7 r' x: e9 {1 F$ l- m3. **递推过程**:不断向下分解问题(递)
    , s& H3 C7 C) A+ g- A4. **回溯过程**:组合子问题结果返回(归)
    : M  {6 s0 `& Z, R- Z" `
    7 y) V2 ?& q3 _ 注意事项: `9 u" l, \6 e7 v# L8 ~
    必须要有终止条件
    , J5 E. y7 y( d( Z6 S# J递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    4 U* L! M9 @5 z. v8 n某些问题用递归更直观(如树遍历),但效率可能不如迭代) E' H$ q" U6 s- Y
    尾递归优化可以提升效率(但Python不支持)
    # ^  }  k& V5 }4 j5 b
    * a/ ~, E/ O0 ~4 b5 C 递归 vs 迭代8 ^  w( q+ y3 @
    |          | 递归                          | 迭代               |
    - \  Q& M" u6 F3 A4 ]" m|----------|-----------------------------|------------------|
    # V/ X/ C( U/ ?& ~" t6 p4 }! c2 E| 实现方式    | 函数自调用                        | 循环结构            |# ^: }* i3 _2 n9 @7 P
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    * ]7 t9 A3 f7 s| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 m0 b. \. ]- h  G# p. b
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" D, h3 _* t' N7 t
    1 n1 s% h3 f: y" W2 \% u
    经典递归应用场景
    , e* X- `+ m& P2 ?" ?. ?; y1. 文件系统遍历(目录树结构)
    . h! Q$ I. l9 v! b* ]' k2. 快速排序/归并排序算法8 i7 l; t. g' y2 ]3 X
    3. 汉诺塔问题
    8 L2 f! Y$ m% z. p5 b" m8 ^4. 二叉树遍历(前序/中序/后序)0 m0 d, U7 _( l4 Z
    5. 生成所有可能的组合(回溯算法)# ?( V* |* P" y: D

    0 t2 `% m! O) R: z8 c2 N0 N试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    2 小时前
  • 签到天数: 3157 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,8 k0 c6 |) |) M3 P* y
    我推理机的核心算法应该是二叉树遍历的变种。- K+ G: a& C/ F. P& i  W
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:% [% m! t( W3 m$ b
    Key Idea of Recursion
    ( B6 p, A2 u1 e( t; L6 u- H0 z7 K9 u
    A recursive function solves a problem by:
    : v: C7 K. O9 M
    ! d/ B# R- L2 T) A( _, Q# C7 d$ s    Breaking the problem into smaller instances of the same problem.
    - ^% H* Q/ }% h4 i+ D3 N6 J8 h$ A& c* P) Z) a, L9 N% c; ~
        Solving the smallest instance directly (base case).* q8 c# N' [) Y6 N: z

    + k9 r. ^! k8 {% b9 `    Combining the results of smaller instances to solve the larger problem.
      x( u; q2 n1 G, }4 u
    : W+ p9 C  j4 Y: v  L2 u4 vComponents of a Recursive Function
    1 S  N" e5 Y% {! o, D( G5 s% n2 U7 j1 ?3 q6 q1 e
        Base Case:
    & i' Q# S) W5 m! f0 d, A+ h" n& U3 E
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# ~8 B; w8 o) F" Q

    ; o. v+ y" Z/ J% C' D# o        It acts as the stopping condition to prevent infinite recursion.+ X6 ^' u$ j. I# i5 W, C& v' Z& T0 s
    % `- R, V3 l4 E& Z& Q4 t% a) ]5 H
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; {/ h7 u- d, `7 q  \/ B/ ~! v
    6 T7 D$ S* \: z1 {- Y/ d, Q4 k, e8 J    Recursive Case:
    ' ^5 Z% F- @; m9 F
    % y. w2 d' P8 l! V        This is where the function calls itself with a smaller or simpler version of the problem.
    / }/ _( L% x- x! l4 ?) }* M* [) H% R7 j% H" y
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: z% f6 Y" K( _! t2 i9 x. ]7 Z  I

    - s: z# a" d; X  w. y( ^. LExample: Factorial Calculation
    & E5 W3 z+ F& j( p; e
    3 M' O& ^( g" v8 v$ {. mThe 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:
    - U% s3 G$ @0 N- P  ~/ C+ d# D+ _7 o( T; n3 J! X! a/ }
        Base case: 0! = 1
    . Q1 R2 A/ t+ C$ p4 j- w* ]
    . s  h5 p1 K5 r$ N    Recursive case: n! = n * (n-1)!
    ! n6 }" r/ R* t2 {: u
    0 K% M, y, `0 MHere’s how it looks in code (Python):
    4 W8 z: `% p& f6 hpython
    1 t; d: m! ?, D6 a% Y; D% u- ~
    ) E! N. I: h5 O3 q. ~  i  x( I" B, q
    def factorial(n):5 l7 c7 V% A" O: ]; {3 }
        # Base case
    - i4 z% ~- d  m7 ?% E9 |    if n == 0:3 S, S1 q+ ?6 n
            return 1" N  a+ m- S% x. S/ q2 M
        # Recursive case
    ' v+ _) X; g$ k; J    else:5 X9 m! f9 m. t! D6 W& q7 K, H- V
            return n * factorial(n - 1)
    * X& G% w: w1 |0 r
    7 |  s8 f5 w. f& ]+ j$ X# Example usage# |# B( z6 P) p( h9 |8 g- m1 t9 p
    print(factorial(5))  # Output: 120
    - ?& J4 W" y6 b0 I! g- `2 h' u5 Z; t/ k) j* g+ q0 e( \: h
    How Recursion Works, D8 Z) P/ l2 |
    - C  e. \( @' v+ f, j7 H8 m# Z
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ; G/ b" f' x% W7 a$ S. W+ z- j7 I7 f, d
        Once the base case is reached, the function starts returning values back up the call stack.2 G- C& k. d! r$ V4 c1 N1 H/ m

      T) y7 d% f- v" {+ x9 s# ]" i" V    These returned values are combined to produce the final result.
    9 k- y' I) Y( Z" p
    * l0 e3 k# T. _+ S$ GFor factorial(5):
    ! P: |6 A0 ?( \* y  E( w7 m; N, X1 B& u2 o% ?

    0 g" l- @# Z' \! h% }3 U5 Pfactorial(5) = 5 * factorial(4)* i9 n  x( }& g
    factorial(4) = 4 * factorial(3)
    ! m$ W" R# |/ y! U; {' A( Hfactorial(3) = 3 * factorial(2)* L- o2 @; E9 _- l: D3 [" L" n
    factorial(2) = 2 * factorial(1)
    3 N  O; M% _% K$ o4 G- Tfactorial(1) = 1 * factorial(0)7 U" {0 x/ q) A8 w# x6 x& `2 i
    factorial(0) = 1  # Base case
    6 D( e' r. r0 |6 e' Y, e( c+ A- A$ `# q" `# l/ @* c( E( G
    Then, the results are combined:: q) Z0 ~5 M8 O# M2 E0 B" I
    1 B1 t; h& a- }% e

    + y( c1 }; x5 ifactorial(1) = 1 * 1 = 1
    % @$ T: m2 O- P+ M! I* }, @factorial(2) = 2 * 1 = 29 ]5 ?1 _- A% v. ]. p; q
    factorial(3) = 3 * 2 = 6
      ]- s& [  Z: U7 ffactorial(4) = 4 * 6 = 24+ `! Z; W& Q: v. U7 ^
    factorial(5) = 5 * 24 = 1206 h) X" h; O4 @* c3 M8 y

    ' s! K* t) m! R% r  PAdvantages of Recursion
    - W2 l0 H! Q0 e2 A6 n; [+ E5 \$ d+ a& G! 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).
    . E6 o0 {6 C0 S: J
    - V8 ?( X& N, e    Readability: Recursive code can be more readable and concise compared to iterative solutions.& j& j  r6 g7 w0 j3 e2 b( z4 h
    7 F+ a& O( V; p* z9 q
    Disadvantages of Recursion
    ' C  t9 s5 z, F7 @8 g7 H4 Y7 @0 c6 ~! `: g* ?
        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.9 D# a2 A/ M+ ]! [& l) F
      \9 N, u1 |' K. L2 K. r( ]
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 A- T( ^( g& Z( i% N
    . G0 }3 o, r( m( }, JWhen to Use Recursion
    0 i: U6 V4 Y1 o# V$ C' j# F: Q
    2 K4 i4 `9 D" J9 k. l: H' S    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    5 |* X- N  ?9 k: p7 l3 `: D( L9 w8 G3 x3 P4 q/ M
        Problems with a clear base case and recursive case.
    - C4 d- M- u  h0 v3 z4 g' Q' R" b0 s6 `- n; J1 r  ?1 h" P
    Example: Fibonacci Sequence
    / a' r( N3 _! P- |9 x' ~& n
    ( D$ l6 ^! v. E" D/ HThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    . ~, D; d' z4 D7 J
    + `- s& }6 R% N$ P4 _; y    Base case: fib(0) = 0, fib(1) = 1  J% J  B6 o" K+ @/ O# K6 I3 U% B5 r

    % O; Y* e0 O% g. C/ e: \% F6 _5 R    Recursive case: fib(n) = fib(n-1) + fib(n-2)1 b# L! c! l; _/ e5 K8 k
    " _: v$ ^2 y% d: |, q  Z
    python
    8 w1 Y1 C* z# w* Z2 ^
    - U7 t! `+ g7 O' l4 \7 T
    2 j. C6 f9 b: |def fibonacci(n):2 @+ y6 m) n6 }% b* B, J
        # Base cases- e5 m" o. t0 s% i3 i% c6 p  h
        if n == 0:
    3 @# e3 W; o( r0 h8 M/ {* u        return 0
    * G* u. m' H! f    elif n == 1:$ O9 S% m% v: K4 @' @
            return 1* f5 h+ e3 U  l: o/ }- V3 j
        # Recursive case) e( v: F% i0 \! p8 b
        else:
    4 \, [% |1 n2 L5 `6 g. N7 d        return fibonacci(n - 1) + fibonacci(n - 2)
    9 \4 e" ]; O) _2 s2 p4 T5 k# i8 o7 G% ]
    # Example usage
    ' z8 P( D+ F9 ?9 Y' h7 \print(fibonacci(6))  # Output: 8. q) s: z7 O" X6 W% b3 a- ?

    * p& O8 d2 z% j5 M1 Z) _9 jTail Recursion& R: K  D. C! @; `  ~. C

    % }1 l0 E. W3 ~' P' z+ aTail 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).
    - x5 Q1 O* j; x
    . B) ]6 R9 M1 h; ^3 |+ b6 Y0 q! [/ QIn 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-29 07:51 , Processed in 0.062539 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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