设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 8 J; m/ w: h; J# H

    ( e& Y7 |5 ^4 D; @3 k" d' d* r解释的不错
      e7 {2 U; `: Y% @* n
    + F! P6 y8 z% Y6 n) A递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ r2 j+ e4 g' L5 ?5 A+ K8 b- F2 e& C3 v
    关键要素! b* R5 l: V! V' ?3 j8 N
    1. **基线条件(Base Case)**3 `2 O2 ]9 w+ N( Z' o4 `; L
       - 递归终止的条件,防止无限循环
    ' j  c: R0 [( U; H% {  N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    $ K  K& C9 ^- i& Q
    * k4 o  M+ Y7 J+ u2. **递归条件(Recursive Case)**
    1 D6 t  G( G4 s+ F: l2 E% R   - 将原问题分解为更小的子问题
    3 L- ?: X, ~2 l8 Z* J; l" C5 z( A7 z   - 例如:n! = n × (n-1)!
    7 R+ K2 u, Q' Q; K& e' E) e
    & F8 z: j3 t. \& |; Q 经典示例:计算阶乘9 B3 f6 L! j' S0 g$ s
    python6 b1 a: U" w6 h6 F$ v
    def factorial(n):& f" J" L! g3 B( z2 |
        if n == 0:        # 基线条件; ~6 p: K+ s4 k& j5 y
            return 1
    5 ?( Y3 t& ?3 j    else:             # 递归条件/ j4 ?2 U, d: X. _% H  }0 W  @
            return n * factorial(n-1)
    $ L- D7 Y# n% C/ r5 G执行过程(以计算 3! 为例):
    5 ~) D7 t* S$ Q! ifactorial(3)/ n" _/ k: g* I5 I8 U! ?+ f
    3 * factorial(2)
    ( Q. Z6 q# K$ U' R3 * (2 * factorial(1))
    - V# D9 Q9 X; S3 * (2 * (1 * factorial(0)))7 K& O$ r  @7 O
    3 * (2 * (1 * 1)) = 6
    ) U+ r: o% s7 I0 d" w3 s* \1 Q* V- F: a4 i9 S6 x- F$ q
    递归思维要点
    , n" B: r! T" S( s' _' K$ `( J1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    1 j4 k' U8 L; h0 z2. **栈结构**:每次调用都会创建新的栈帧(内存空间); e3 L% N$ A9 b: r- V  N1 ~
    3. **递推过程**:不断向下分解问题(递)$ d! ]/ a0 I9 o$ G2 C5 v
    4. **回溯过程**:组合子问题结果返回(归)
    ) ]! J' h1 z1 a0 h3 [
    # G  z& m. _: B; z* [; O 注意事项6 X3 C8 E& v: s- N# H1 W3 A
    必须要有终止条件5 i$ F# |4 a2 H4 A+ H! n
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    4 I4 ?* i* E' i5 s9 N& m某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 \: M" r' [4 j8 W尾递归优化可以提升效率(但Python不支持)4 @; _# x; }7 f/ i# D- Y
    & I: i2 M# l, I  C* L8 @
    递归 vs 迭代
    " T/ ?1 w2 f( F+ J) Q3 s|          | 递归                          | 迭代               |
    7 T7 Y( O, n. G# i' T|----------|-----------------------------|------------------|& I. F% h* T. F& f$ S  G
    | 实现方式    | 函数自调用                        | 循环结构            |
    # Y7 c7 `' Y4 p  N0 a/ r# |, t1 y| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ; D2 `/ X8 m- b, T( w( I| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |* A7 A* a  q# B$ g
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 z3 R& C) d9 `: t& z' {& y
    9 J. m* t  o' s$ M. L 经典递归应用场景
    6 A; n) B! Q7 H0 U/ }+ \1. 文件系统遍历(目录树结构)- d5 i5 |% @6 U" E  z
    2. 快速排序/归并排序算法
    2 Z) y9 j+ x7 t" k! Q7 b3. 汉诺塔问题
    & p$ R  C& n* x. v4. 二叉树遍历(前序/中序/后序)
    5 G5 ^2 {& A4 [5. 生成所有可能的组合(回溯算法)- [4 @- Q+ C6 ~6 k5 d7 v0 U
    - z" u8 `1 \5 I. H. q8 ^
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 07:20
  • 签到天数: 3247 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,8 i+ g: q  P6 S. w! L: ]
    我推理机的核心算法应该是二叉树遍历的变种。$ C( V; s1 f1 k" D- ?- `" {: i4 t
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ( @  o; g: l" U- `- cKey Idea of Recursion2 U2 P/ z) c5 N* W2 h1 X  |! p
    * c/ [4 I/ _1 g+ b; ]9 @( q9 b
    A recursive function solves a problem by:+ i1 O4 `0 T0 V+ e$ F; `) F0 ?

    ) O5 x% T" ?, h- |5 b" q3 J9 j    Breaking the problem into smaller instances of the same problem.& q/ |1 m: D0 h5 z  V, J7 f

    - D# l2 J% `1 k1 q    Solving the smallest instance directly (base case).
    7 ~" M4 }! f, U0 _( l
    3 @: M9 k" O  ^* w' n8 N, o    Combining the results of smaller instances to solve the larger problem.3 y. j4 |6 [: ?5 f- z" I* g( S
    ; |: ~8 `* a' i5 ]0 J! N/ ^- ~" |% e
    Components of a Recursive Function
    & a# a6 n) N+ M: F0 T9 v5 O% |+ x4 H, V9 q0 Y" f* B
        Base Case:: I  V% o1 M6 k. J! x- h2 u# k
    9 E* H3 t( d) J+ ~. I7 X" E
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / n3 q" E4 G8 Z2 l6 N3 ]
    8 E+ {" R4 e, _        It acts as the stopping condition to prevent infinite recursion.
    5 P" n/ f8 e# d2 V5 T5 ^7 P/ M/ N, H5 D$ r
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # X0 f! o8 E2 i* `7 S/ P, R$ D6 N# D$ V  A+ Z% l
        Recursive Case:$ ?1 s6 X$ m5 y1 \% n; O/ Z' t" `

    2 X! L$ D& u1 C        This is where the function calls itself with a smaller or simpler version of the problem.
    1 K$ r, k; b+ |, Q; F* F/ G: W
    0 O( u  G9 ?& a  d        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 ]& }  g" L# d0 _  {. m% p
    5 O- g: o8 c* g7 i6 h, O) L; M
    Example: Factorial Calculation
    ( s9 ~# {8 q% `1 |
    ! p/ B- l2 _6 _) d* rThe 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:6 [1 B( x) o( p4 ~$ a
    * h" @+ [! `- p+ S4 U; A3 n6 m
        Base case: 0! = 1
      d4 s6 P- ]" p1 J) J# T. x/ |6 A( D( W; t# E+ u' K7 v5 X
        Recursive case: n! = n * (n-1)!; t$ S! _. f7 B+ c6 o& F

    + R- l3 a( H  M+ HHere’s how it looks in code (Python):- M- I2 r$ W" e( w" ^. N) j
    python
    4 W4 O" _+ p& |9 t( c7 {3 l8 Y* F/ Q1 `

    ! M- i1 K) I. M, a/ ]9 l2 G5 z5 k1 m0 Fdef factorial(n):4 Z5 j$ q: L7 o* |0 X9 Z) C, \' H
        # Base case9 O6 E) K' I/ ~( ~. f8 T6 g. f( _
        if n == 0:
    & ^: y( N7 @7 T5 v) R( O% ^        return 1! s1 e8 V0 r: W4 ?) A/ c7 t+ o
        # Recursive case
    8 E' z9 R* e. J. {4 U& F    else:
    # c+ n4 T* C8 O! U, O+ ?1 M        return n * factorial(n - 1)( A$ w4 M; j( l( L/ n1 _/ x
    & v7 L1 p0 b' n
    # Example usage
    & s% M* H& O3 Q8 ^+ Eprint(factorial(5))  # Output: 120
    # F- A' k3 |( t) K" o
    . w/ W  G, q/ q' i& [6 E. v  IHow Recursion Works5 D1 F! s! q7 W+ `/ i
    5 l  t1 V2 i0 C3 s
        The function keeps calling itself with smaller inputs until it reaches the base case.
      ]; \5 _' n: G/ d% _' k
    ' i: h' y4 f, q; k. {$ |7 U    Once the base case is reached, the function starts returning values back up the call stack.
    + V9 v$ X# B: i. ^; Y  t# t& Z) D; w0 l; E
        These returned values are combined to produce the final result.4 Q1 P2 N; T$ l% P: n4 i5 c

    / J+ T2 ?4 b: A! E2 p8 n% vFor factorial(5):
    7 A3 M: M8 I' a6 @1 T4 p" j' B  w4 c& i

    , {9 X& j0 r# H' Sfactorial(5) = 5 * factorial(4)! F, H7 b$ w# ^4 |3 k  P
    factorial(4) = 4 * factorial(3)
    + g" x$ z/ X( {9 h8 I" V  ofactorial(3) = 3 * factorial(2)
    2 B2 G9 r7 R0 U- S( Y9 Gfactorial(2) = 2 * factorial(1)
      B  j5 i) g% h9 Q( T3 O  ]factorial(1) = 1 * factorial(0)) b( s/ W" W# {1 x5 d$ v; p
    factorial(0) = 1  # Base case
    9 P: z0 G8 Q0 Y3 o7 E( [9 [6 i  n0 a. |
    Then, the results are combined:
    6 f$ L# n# j% D. c0 S' J8 A$ S* [8 p- t- N# t% r' [

    * P7 j* P+ W$ G" |+ pfactorial(1) = 1 * 1 = 1& G. N  ]+ S+ |* Z0 o+ A
    factorial(2) = 2 * 1 = 2
    ; b3 k" X4 _- a: t  a, `4 Mfactorial(3) = 3 * 2 = 6, y& E+ U1 n2 M7 H2 b6 i+ l
    factorial(4) = 4 * 6 = 24
    7 S3 B9 a  l( h: G* n% Q3 {1 ~1 @factorial(5) = 5 * 24 = 120
    5 {6 T- r* e. T7 E3 D, \" Z' \' |2 n( @" t9 w/ ^! L3 r
    Advantages of Recursion
    2 H/ Z+ Z. D' a+ b) [% b2 E; ]- ?5 J: N, }. _, d$ c* q0 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).
    0 X. s' H! A/ G, S9 j
    . ^3 ~" K) D, s+ B+ ~2 t5 c1 h: m    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; W; B% m6 E( b% p
    , L& r% e1 d9 V+ Y3 o; m1 Z) @Disadvantages of Recursion
    0 f7 Z7 r! Q+ }0 n
    : D; F2 I; C5 j2 d7 M4 T1 q# 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." E+ R% _) o4 E1 W
    8 G! w# R: ~  {/ Z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    3 D  n& A' z& z& G* \$ b
    : o8 F/ w3 i1 A9 F/ Q8 nWhen to Use Recursion3 d  p5 @7 ~; R- m+ g
    ' a8 h) _) h, E6 ]  J& a
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    - S# F/ L& i2 q! F
    # C# l/ d; N. V! E! H2 X' m0 [2 G( ]    Problems with a clear base case and recursive case.6 r- R) B+ r. o( U2 R7 j6 L9 y! A

    / t- v8 N. y6 s+ W2 Y7 D6 }Example: Fibonacci Sequence7 _7 W4 p9 M$ t& z0 E5 q

    * I- l) V* {) |8 i& RThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    9 f# K- ?" Z/ J" ?
    8 J5 y0 Z3 J* g( d- @    Base case: fib(0) = 0, fib(1) = 1
    % t/ y: W% R4 D; x
    6 t8 _' `3 X5 W) X8 Y) V    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    2 \# [  d8 ~, v/ u# f5 x5 ^% E6 ?' c. j1 ^
    python, R# h# {* j4 `3 L3 w* `
    ( W' n! U+ O( }- \$ Q

    7 u& t! i* F( c0 T) \6 k, ydef fibonacci(n):+ x, d& z! }1 t) ]7 v. M  j2 W
        # Base cases; T5 K& J9 X8 n4 e
        if n == 0:
    ! F# v! S# z' E. N$ t        return 0$ l* _1 ]7 v/ H/ }% |* A
        elif n == 1:6 m6 P4 r# E; e2 R
            return 1/ e- Q$ z5 z' i6 k" o# b
        # Recursive case
    3 P' a/ M) Q2 ^3 g. Q( Z% ^    else:% `& y0 Z+ `8 j% H7 b
            return fibonacci(n - 1) + fibonacci(n - 2)6 X, s' F1 H% d* ]. R* u
    ( |: l( b9 L7 j
    # Example usage
    + P+ {3 E) e4 B! ]1 W3 uprint(fibonacci(6))  # Output: 8
    $ T! Y+ [  l) B9 S0 L, ~7 w. _$ y' H
    Tail Recursion8 P& g2 P! r- L1 R
    % U, _0 o3 X9 _- R+ p0 R; Z1 p
    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).; m* p9 O* [2 ?0 t4 C8 ?# W* k0 Y
    ) |9 V( t, X) V% Z8 o8 p# ^
    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-5-24 01:54 , Processed in 0.056355 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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