设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 `3 O' @  L! P8 b- s
    9 @- I# K6 P/ R  w& b0 t解释的不错
    + E: U% B* g: X7 Y% |- s- o8 H. g3 K& z9 w. A
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, y& d7 z' H" c6 s9 n
    $ e3 p8 r. K2 }1 ?$ M
    关键要素
    $ j, ^0 L: ^1 c, \( O0 A1. **基线条件(Base Case)**
    ' H) J& U5 q9 ~3 ?1 W& x   - 递归终止的条件,防止无限循环/ W, t! n# G, N( ?8 S
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    # `* \. u" k7 X# C" n! U8 R5 v
    / b! O+ k5 d- p5 X. ~: l2. **递归条件(Recursive Case)**: G. f& C% O% M
       - 将原问题分解为更小的子问题1 z+ X4 {! A0 t. N/ C# p$ Q# b
       - 例如:n! = n × (n-1)!
    9 {2 X3 ?# |  W& A. d% F/ p# Q) {# B5 R( V6 p
    经典示例:计算阶乘( `8 E7 I: T+ {. G' F
    python
    8 P! k; M0 X2 B4 d* {" Odef factorial(n):. g* K8 m3 x7 @( o; I' A2 m
        if n == 0:        # 基线条件
    9 e) B/ w0 V/ {* O) c        return 1
    * R' ?+ y8 m  A7 {5 ^" J4 }9 }    else:             # 递归条件
    ; a( ~) v# l$ x5 `" ^        return n * factorial(n-1)
    1 t3 A0 T; P% X* p/ e" t. c1 e执行过程(以计算 3! 为例):( l% m1 v% i1 y
    factorial(3)  ~! f4 U/ q$ v& h
    3 * factorial(2)
    * O& _8 g6 q0 L' m: j, u' S3 * (2 * factorial(1))1 r" ~, L7 F; n/ M! _: I' \
    3 * (2 * (1 * factorial(0)))* `- c  ]* g8 A
    3 * (2 * (1 * 1)) = 6
    ! Z  |# L% k/ c
    * g5 ~# F" R- D( z) d* k* x$ z, e0 ] 递归思维要点
    ' D$ F. W+ {+ ?: r; U2 ^1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    0 A3 ?0 s! T/ D. C; J6 X( _2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ Z: J" O8 M3 U' v: Z
    3. **递推过程**:不断向下分解问题(递)
    ' g; F  C* [/ A* @6 u& o" l4. **回溯过程**:组合子问题结果返回(归)
    $ z+ j- o6 r% v7 m/ r0 |/ H
    5 H6 T( n1 |' {- @1 [- C 注意事项
    4 F) j6 m# m3 m$ g+ [$ W必须要有终止条件4 Q" q' M* W5 s, L- e' {' @
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ; T5 h; b8 t9 ~, _) A某些问题用递归更直观(如树遍历),但效率可能不如迭代
    " D' p2 R% C% ?) \. {尾递归优化可以提升效率(但Python不支持)
    ' h% Y3 a; K' a' h/ R' I: e
    + a3 n5 O, l  i% y3 _ 递归 vs 迭代. j( V& r# j2 V0 i8 Q# S
    |          | 递归                          | 迭代               |
    % Y/ E# p& R! e( o|----------|-----------------------------|------------------|8 \& [5 Z& p* W' o% S+ h0 r! v
    | 实现方式    | 函数自调用                        | 循环结构            |9 `0 L& P: {8 E
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    4 V0 |8 \( [0 j, X| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& B7 f2 r5 M7 @, n
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ Y  f( q# @/ \- ]! t

    ' }# v0 w- i1 P; g& K3 _ 经典递归应用场景
    6 ~; k, v6 A$ d  }) d8 }8 N1. 文件系统遍历(目录树结构)  D% C; Q, n- V/ p' F1 L
    2. 快速排序/归并排序算法3 v; u; z- u6 v% ]
    3. 汉诺塔问题
    / J: l8 K5 c# b$ n  o" h2 U4. 二叉树遍历(前序/中序/后序)
      \. X) ]/ v% W1 ?8 G5. 生成所有可能的组合(回溯算法)
    ( Q3 P8 X3 u* w/ o! |* x* ^4 q8 W' g( P4 r' ~+ t
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    14 小时前
  • 签到天数: 3205 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, r, R7 S) p1 f8 x% d
    我推理机的核心算法应该是二叉树遍历的变种。+ A. v: s) C) b6 ]
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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" h& ?. @/ J) \- Y- W3 N
    Key Idea of Recursion/ I! g/ h7 X+ F; R1 x* f
    & O) h+ H8 }: I3 A" e6 Y
    A recursive function solves a problem by:/ n' i* h9 `8 p3 e. S" ~( S+ t5 x0 ^8 X4 q
    9 t" v6 h4 \; F7 ?
        Breaking the problem into smaller instances of the same problem.' f9 {9 R+ q! z. w7 w! h; L

    3 m# K2 x- b* y) H: X! z    Solving the smallest instance directly (base case).! o. p" q# K. d5 D7 e* D

    3 z0 E/ P" a# l& H3 n    Combining the results of smaller instances to solve the larger problem.
    9 S6 \' r" {" N% e; Y! ^
    / s' F' Y+ ^0 n/ _! kComponents of a Recursive Function
    ( z% U: c/ g; l" ]" B0 p* F; j8 Y" r  G. g! s6 {
        Base Case:
    # O# t; h, b4 @" T: A: T+ Z& B+ W, Z- e3 J
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.9 Y2 w( G. D) |
    " f- x7 D% O  A9 U1 Q8 F
            It acts as the stopping condition to prevent infinite recursion.
    6 ]* V, n5 }& k! ^$ e( U
    & [% X0 S; I( Q  S; i+ n& x/ r        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 p% n6 u$ Y% K( z. f# ]
    4 W* C7 j, l+ z: m% F) t7 @/ g
        Recursive Case:
    4 ~( ^9 \8 p$ S8 j
    ' u" u+ L( t: k5 s( L+ U        This is where the function calls itself with a smaller or simpler version of the problem.
    1 q" ~6 w6 V1 O& ^3 {4 l+ B9 A* Q. J
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 r( ^5 V7 f& J! _3 E5 r/ U% C7 @/ O/ L, i
    Example: Factorial Calculation8 s; P! t' ^2 t4 r2 X
    / A6 P1 q8 P3 q! _. ^8 Q: Q
    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:* o% ]6 b8 a1 F3 g

    6 [; ]) F# V  o. O! I1 F    Base case: 0! = 1
    ! s: ]% b6 k' h; Z5 s( T2 {3 O5 d8 e7 ?4 \0 Y
        Recursive case: n! = n * (n-1)!
    0 S( n5 R" z# k! G3 D3 G+ e' ]! J7 [! U) X
    Here’s how it looks in code (Python):, I7 a& j7 r6 P: ?7 \* f
    python% n+ H- Q8 N' P+ O  H; O
    ' H8 _- B3 X! O6 g( s; B
    6 T/ R8 E. d+ t5 K. m
    def factorial(n):
    9 ]) x' m/ T  o$ p  _* E8 |    # Base case6 U3 m; L) b- N- ?$ I
        if n == 0:
    3 M6 f5 q* V0 a5 N7 K8 p        return 1
    5 p% }' j. W! j" T    # Recursive case( g; q, y+ C3 |5 V; v
        else:
      O1 b" M8 |, ]% X- W% ~5 L9 C        return n * factorial(n - 1)
    $ r% l& ?" M$ |& z, b
    ! W$ z( d+ v2 w# L# Example usage
    ( m, I) B7 T4 A6 mprint(factorial(5))  # Output: 120
    4 F. U1 g, `& S1 r% n0 u0 e6 _4 y" Q* ]: a. M* b
    How Recursion Works5 O& i# c, @) `5 T. a# R
    ' e. G  p. `" y  ~
        The function keeps calling itself with smaller inputs until it reaches the base case.2 p* X4 F3 y, K/ X4 t

    9 Q3 `* E& [' t& ~& x+ B4 y  g    Once the base case is reached, the function starts returning values back up the call stack.
    8 g! w2 ]8 X9 m/ x2 d& g& [
    ; N0 w  z# n# A+ p$ J    These returned values are combined to produce the final result.
    5 ^9 S# `# m, `0 M9 R1 j; B6 x1 P5 p  j4 i/ \$ b1 X1 A% F0 u
    For factorial(5):
    $ s; g7 W$ q* ^* a6 J# |/ c' k  M/ B: l- @" t# ?# s, N

    ( f9 n; {3 S* c: Q; Ffactorial(5) = 5 * factorial(4)
    5 W: F4 P/ ^+ S2 e1 r9 I* Mfactorial(4) = 4 * factorial(3)
    ! e2 V! H' |/ n. zfactorial(3) = 3 * factorial(2)" E/ s. _6 l; K/ B
    factorial(2) = 2 * factorial(1)' c% _/ L2 m8 [; I- j
    factorial(1) = 1 * factorial(0)7 e4 R% W/ Y1 l, S
    factorial(0) = 1  # Base case( m5 V$ A1 y( M6 A
    , Z& s' t2 Z( R6 Y3 Z. k
    Then, the results are combined:
    ( v. y- T! ]' s1 U* m; ]
    $ R0 j7 M) D: b9 x! F1 b* Z5 k7 d/ Y, M& N8 P0 ^) ^, f$ [, D1 F
    factorial(1) = 1 * 1 = 1
    7 A6 u! w7 Y3 V2 n# cfactorial(2) = 2 * 1 = 2; [# D' s6 t8 {5 C/ n& \
    factorial(3) = 3 * 2 = 6
    2 J$ ]3 i4 B; |. Hfactorial(4) = 4 * 6 = 247 ^" o( g7 p7 x+ {9 j2 U
    factorial(5) = 5 * 24 = 120& J& u5 h) q7 q
    1 j% z* K, p8 K  z4 M& ?6 D5 O
    Advantages of Recursion/ F3 B2 g5 p: S
    : U' L8 \' ]; M
        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).; l* L- }; |" H2 t1 f# W6 n+ p

    : p% T/ r* w& ~- `- g4 I/ r    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    3 S2 W, Q0 l, u8 t" U6 j4 e: w3 x$ r& Q
    Disadvantages of Recursion
    ) u: ?* \8 p9 E9 g
    + c! s1 X- G8 N+ O( j    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.
    : L% z3 L# d/ `% n% O- j- @8 C$ R7 a! Y  b! G. k; u
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ! v' J5 Y4 i  J: n1 W( t
    ! P  c4 m* ?2 `) pWhen to Use Recursion
    9 \- w+ a2 _  [# |. a
    8 N; {2 S) p* a0 U    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 [$ X5 j/ |( |$ o; i

    7 r  |" H- S9 R& ~3 Z    Problems with a clear base case and recursive case.  C( c) v/ p  \  j: H$ a

    & m0 E" v4 C6 o) i% u2 {Example: Fibonacci Sequence+ Y" f! M+ G* v) v# \" j
    / I  T2 H9 @1 {8 y6 ^4 |6 S! n) F
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    2 W/ E6 m  S- d6 o8 S* t5 l+ n% J/ ?  q" l7 L
        Base case: fib(0) = 0, fib(1) = 1, O2 l% r, N8 [
    2 l1 i4 R. ~  t, |! |6 o% C
        Recursive case: fib(n) = fib(n-1) + fib(n-2)  H2 _# J7 j4 H4 `. H, i/ ~

    : ~5 Z! C- C* C7 j5 Zpython' c0 X) n0 g+ ]

    ' o9 s, G, ]' d' [
    / C/ Y" R5 y" W, U3 Zdef fibonacci(n):
    % w# J& ^, V' `5 [8 t" P$ g/ T) F9 _: H    # Base cases, h! {- y' J7 d3 K, o( O
        if n == 0:
    ; t% G2 A0 f- T, a: {$ g8 V        return 05 Q+ [* R7 B- S6 I7 V3 l
        elif n == 1:7 X: ^, g! \$ k
            return 1
    ( s# u& |# `( a5 F    # Recursive case$ f" C" ?; n, x) p( C2 ?
        else:
    7 b( @) ^; @" h7 P  c        return fibonacci(n - 1) + fibonacci(n - 2)
    4 o/ V3 U  q: ?6 }' G/ U+ s. \1 j
    ( I* n" E1 v! e' Y3 P& i- d1 g: n# Example usage- ?  E' A$ w1 V( |
    print(fibonacci(6))  # Output: 8
    % Q8 ~$ Z- _) g  y. c. u5 U1 h
    9 a0 Q6 P" @7 `  w, cTail Recursion
    4 g% r3 p/ W" K
    6 g% D+ E/ M- w+ xTail 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).
    9 w1 S* p- |0 [7 b# _0 f5 k' M3 q% i
    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-3-20 20:04 , Processed in 0.063896 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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