设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 % R0 ]: Y* Q$ Z# x% G  a3 r
    2 \& O# S5 q( n# [# l0 x9 r
    解释的不错7 J( V4 a! ], l$ _
    4 c/ F) g: X* I+ L
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 j, U2 d$ I$ h+ |; F' T# |" L
    ! ^/ Y6 R4 v5 ^! G( i, | 关键要素
    4 M/ t, O+ X" y/ V1. **基线条件(Base Case)**5 B$ F1 W- d0 B2 V! c- l; H  \
       - 递归终止的条件,防止无限循环" e: _, }+ [& B4 A
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ! s5 ~8 U) L. d  {3 _( I7 J( G* o! \6 v0 O- ?+ X1 M/ u
    2. **递归条件(Recursive Case)**% Z) B& X4 i# q) X* k) E  Z
       - 将原问题分解为更小的子问题& j! h% ?4 Y& p+ R- o: @3 d: `- v
       - 例如:n! = n × (n-1)!
    & K2 v7 p& @; Y; d5 Z5 _$ [. S
    经典示例:计算阶乘6 k( Q' N- p' F- u/ b$ c; s
    python. N: f' f  p+ x5 u
    def factorial(n):5 Q5 b! |0 v  C* R% d
        if n == 0:        # 基线条件
    + D$ _3 O, ^- j8 Y" t        return 1! M- S( M# q6 T
        else:             # 递归条件
    # V8 F8 J" |0 B        return n * factorial(n-1)) j% \+ M" K  Z2 F% }: @
    执行过程(以计算 3! 为例):
    " p" u! t4 |, ^1 z: `factorial(3)
    " g8 }2 z6 c* v! U' W6 d- N3 * factorial(2)' Q) r( k+ x9 ~2 ^3 B0 H" F. R
    3 * (2 * factorial(1))
    ; Q! K; F. g$ Q7 ^9 p. v+ F' U3 * (2 * (1 * factorial(0)))
    0 i" f6 j& Q: o+ C9 o1 ?3 * (2 * (1 * 1)) = 6
    " F7 ~# c% I" P' Z
    ! m* ~- s3 k5 R& y" D+ g8 D  J 递归思维要点
    9 g+ h1 h7 t+ X; p# E, T1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 ~: D3 `- Q1 u2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    * i! Q6 A. [* q# g2 d3 }1 S* K2 |, M# _3. **递推过程**:不断向下分解问题(递)' y& z; z& Y5 J! Z0 c4 H
    4. **回溯过程**:组合子问题结果返回(归)
    7 E3 ]7 ?. h3 M& `5 m) l0 U' L& u8 F" Y
    注意事项" R3 k) d# g$ q  \, G) A
    必须要有终止条件
    * j0 a0 X6 M) @递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    , k# P8 ^" x. v. D! A6 l某些问题用递归更直观(如树遍历),但效率可能不如迭代1 G7 k- i  W$ r7 ^" t; T' `
    尾递归优化可以提升效率(但Python不支持)0 Q5 k& B( o& h

    8 _: Z4 a$ f- A9 Z5 B6 Q 递归 vs 迭代
    $ Z" Q7 ]( g7 M! Y( s|          | 递归                          | 迭代               |; k& u2 e4 x( M, ~+ H2 e
    |----------|-----------------------------|------------------|
    2 U3 m4 M0 T: T$ j0 K5 H2 O; T| 实现方式    | 函数自调用                        | 循环结构            |* M0 ~% a! i3 f& ]- c/ f
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ P: |  p/ N+ `1 s& g# g
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 U; Z5 j! v' e1 z5 w* P; q3 l7 K
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! {7 [& J  I5 a4 I" J/ w0 {+ m5 f* J
      z$ T$ ^9 y! ~$ f7 ^+ U7 _. G
    经典递归应用场景2 q$ b9 u, w; W( B+ N
    1. 文件系统遍历(目录树结构)
      Z8 W# @! A& Q9 Z  \8 O2. 快速排序/归并排序算法! s+ Z- b$ L3 ^+ f: e1 ]1 a2 d# f0 B
    3. 汉诺塔问题6 O# T8 Y  \  W
    4. 二叉树遍历(前序/中序/后序)  Q& Z, W. G$ B- u! J1 V8 C3 }5 D
    5. 生成所有可能的组合(回溯算法). m6 w, I3 I* a/ C$ P4 R
    % f4 i( k8 W$ s+ H2 w
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 07:45
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    6 V2 P$ _1 H4 ^7 K我推理机的核心算法应该是二叉树遍历的变种。
    % c+ h  A# [, b! |5 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:$ O0 i( U8 \5 T
    Key Idea of Recursion; [5 x+ J) d* C5 U$ ^8 T( B; A
    # p7 z+ y0 E& d8 W( G, Y, L+ b8 H
    A recursive function solves a problem by:
    / i* ~& \" P4 ^6 R8 G* b! j2 V% ?9 W' a+ i1 a1 o( D9 c
        Breaking the problem into smaller instances of the same problem.
    . x* F  o6 a, B6 z: X  P
      \9 |6 M8 k) @    Solving the smallest instance directly (base case).
    ' W  f; |! S* D1 F& l& E0 S- V) a) r3 O. m1 N! H; s
        Combining the results of smaller instances to solve the larger problem.
    * g& W1 g4 r& O1 E, J1 m/ t$ P6 `! }
    . c3 ?1 T  H* E( y9 b. H4 D9 w; mComponents of a Recursive Function& b+ A# H$ x& S9 O( s7 H1 T% b
    ' y4 P% R2 d( C5 k
        Base Case:
    ( |! Q" Q& Y0 [6 l* p& Z# W
    " S* Y) j9 v$ J& n9 C        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.& c- D  J& D# v4 {* h+ B1 `

    6 |  g" u2 F/ p        It acts as the stopping condition to prevent infinite recursion.0 u/ W% }7 E  v$ D
    - p9 v; L- l- v' I
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' n& `4 l) J% @2 ]

    " A. s9 g9 P3 E" u$ v    Recursive Case:) r( x1 Y; C" i1 [1 U: f

    $ s8 p" [9 z5 `* [        This is where the function calls itself with a smaller or simpler version of the problem.
    1 X! i' m( U' {; ~0 x2 [3 x5 s, A5 v7 n
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    / v; P9 H+ i+ z+ U8 V$ B7 L) M, l. F! e2 K' X& x6 C
    Example: Factorial Calculation
    4 f! p4 K4 W. P$ t3 H
    # G  Q1 c7 w' nThe 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:5 n0 h8 _6 W( E5 t5 n

    6 v) `6 U7 d8 d* M* v: m% H7 E    Base case: 0! = 1
    - R0 w3 S/ E( Q. t8 \" b# G* E' V
        Recursive case: n! = n * (n-1)!$ @. q" Z8 d$ t
    : m$ D8 o. f. T; W: _& r# M3 M" Q
    Here’s how it looks in code (Python):
    - _  C/ U, v1 @2 C+ {& J7 Fpython4 E2 F5 v9 O/ H/ ?* F6 C' l9 o
    8 ^; N1 G4 v5 p8 K
    ; C; z9 E1 Q% R3 r
    def factorial(n):' z5 `. {; u0 L. r/ F( S
        # Base case
    ( R; T% Z: `& C9 k8 b7 d    if n == 0:4 ~& `0 l8 X3 ^7 W. q5 r* {+ [! g
            return 14 Z3 `2 i! q& n7 z1 `
        # Recursive case0 t3 D1 ^. Y, q# ]& Y1 D
        else:
    ! B- }: P, m  V+ a% R        return n * factorial(n - 1)0 z: O' y5 C* i9 O5 N6 S8 [, n

      }! B. H& e& m3 i- ]. L# Example usage! b& R( R+ s- Z7 W
    print(factorial(5))  # Output: 120
    6 A# i5 R8 e9 z: R
    & N  s' h' V# l9 XHow Recursion Works# e. _, v. z" P

    ! X+ ]$ w' ]" i$ F' [' W; [    The function keeps calling itself with smaller inputs until it reaches the base case.+ i4 Q) F2 z: w% c# ?4 I/ C

    9 |8 ~( e4 z( A! u    Once the base case is reached, the function starts returning values back up the call stack.
    * c- M3 o* p  J8 \8 N  d: w7 Q; k6 W: X7 B, W/ {4 x$ F9 E
        These returned values are combined to produce the final result.
    ; N' G0 B9 _7 p' v- d( _3 f' A4 F; N: M! P" p( l9 u8 v, @. s8 e
    For factorial(5):
    ) z% ?' F; d: N2 I) J
    9 A% s* C4 R- u- G4 ]$ m/ k
    : K% F$ V2 u) W1 Q/ E+ }factorial(5) = 5 * factorial(4)( X- P* ~% `" B. J) E$ e' w
    factorial(4) = 4 * factorial(3)1 t$ H5 V9 f% a. Z8 U* L) Q- B' P
    factorial(3) = 3 * factorial(2)" G3 q( \$ p- w0 Y. l/ U. s
    factorial(2) = 2 * factorial(1)
    - g! K' ^+ B# Q  ]1 ?0 F; |+ Ofactorial(1) = 1 * factorial(0)
    7 y; }& F. h: x' b9 x+ j# C7 F4 pfactorial(0) = 1  # Base case
    4 K( h$ D( `: v, _1 d  z! f) L0 d+ n) E
    Then, the results are combined:
    5 [0 x, A  S/ o* q1 X" y. t1 q* y1 [/ ?5 T" ^7 V1 @

    ) h4 \* M% z0 n4 sfactorial(1) = 1 * 1 = 1
    ) P1 o1 b; Q. L! }. {  A) @: Rfactorial(2) = 2 * 1 = 2# J  L6 u2 R  m
    factorial(3) = 3 * 2 = 6
    8 ~0 w3 t* Q3 {" r, `) @& t% cfactorial(4) = 4 * 6 = 24( a  @5 r/ X$ _$ I
    factorial(5) = 5 * 24 = 120
    6 K5 W) i7 j1 ~8 P+ y1 m2 L7 t3 b1 j# {) x5 \) f
    Advantages of Recursion3 P: p! D; q+ u) z7 m! ]

      B5 e; J( e' Z: S    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).
    # u% M8 k( }$ i3 W, |! w  E" l( a; x+ I6 W8 C) q) [) ~
        Readability: Recursive code can be more readable and concise compared to iterative solutions.: o) D% R; Z) S& K- i

    ) C% C0 c3 @' s1 X% ~' ADisadvantages of Recursion+ R* d, ~6 Q! D

    4 {" ^' G' w: A+ F4 Z8 |& W, _/ s    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.
    * ]1 e2 C/ g; }/ E6 d8 V) q- q) f. m9 `: h* D/ a; H7 x* S
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ( u* D+ T& L2 P. H% _0 f& g( F5 m2 @, H& G
    When to Use Recursion
    ) a( J- \8 y6 W- h6 e8 b* b( U( G( ^* E
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).1 k! m! a9 Y, z6 m4 \4 X
    , Q# n# L6 _& p+ U6 K" V( _$ ]- }7 i
        Problems with a clear base case and recursive case.) U/ {; s% S1 A: F' ~7 d) ~! T
    & d5 A, F" w6 X/ e" Y
    Example: Fibonacci Sequence
    0 b8 _# C7 y. r5 @0 K" t% g+ ~4 Q& l0 }  ~+ V
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 P" @9 T- a) H( T( f9 O6 i" F
    1 N  e6 ]# q1 b0 _" p/ l5 u8 w' h    Base case: fib(0) = 0, fib(1) = 1
    ; O) G+ J' G) G, }, m7 |7 z/ A# C' U0 q: b& }3 w; W
        Recursive case: fib(n) = fib(n-1) + fib(n-2); N8 w( h( U1 [8 ], m' O
    " _2 g8 s1 y2 }- D' s6 {
    python& s0 x0 p) A- v- n

      q2 O% l( s& t5 \, p9 }1 S; O/ A
    * ?/ _6 B& b& @: }% q+ {def fibonacci(n):. N( u' j' i9 L+ r. [0 f8 \" t1 _5 |) G
        # Base cases% P- e+ F2 s/ \
        if n == 0:+ |. Z# Q9 c4 ]& A; n& l  t& h
            return 0: U+ H; T: W8 k* v' E  r! f- S/ C6 D
        elif n == 1:
    # i3 A* Z, M: S# o  J! L0 d        return 1' Q% L7 u$ w7 S5 v" i
        # Recursive case
    - v: p' ?0 d# l- n    else:
    4 Z1 A% ]+ X0 d3 E* e        return fibonacci(n - 1) + fibonacci(n - 2)
    # ?2 k/ f4 Q) z+ _3 r" D' `
    7 k/ Q0 u1 U' o# Example usage
    . t' _) p+ ?; S- B" ]5 _print(fibonacci(6))  # Output: 8" S6 [% r! r4 N/ i* {0 n2 e
    , Q  r1 w6 A- \9 b; \
    Tail Recursion# U3 H& `$ k5 \9 S: N

    . b% I( C8 A1 g: TTail 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).
    7 W1 L0 r& _: A' z& H# v# N+ r8 j% @# ^4 m( n7 e: [1 C
    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-10 07:05 , Processed in 0.057968 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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