设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    5 n& a! Z% ]' a; ]3 v/ I
    ! d4 x# Y5 h, v% t解释的不错
    . ~$ j5 j) Y9 z9 |; Q0 R6 M& I
    4 X  O1 \' k3 D' O  i% e& I* b递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    % V0 a, K3 Z$ G
    * F; M* X) q/ ~1 x 关键要素
    & Y6 V( `; d% H  s' u* l1 k) C) v1. **基线条件(Base Case)**
    6 E7 v6 H8 B3 c   - 递归终止的条件,防止无限循环
    " B+ `* M8 ~2 I3 r   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ( Y: _) ?$ `8 ^" P7 z8 b, B$ h( a4 I% T( _' F
    2. **递归条件(Recursive Case)**2 G4 i6 n* e6 B5 H
       - 将原问题分解为更小的子问题+ a' Q/ W+ o3 R( U' G) a: k" e1 \
       - 例如:n! = n × (n-1)!7 H# N* p2 y* E" N5 `% {3 e
    . k& _5 o' u5 O& C( I
    经典示例:计算阶乘/ S- {2 I6 u) W: p/ v. l; C
    python
    & o6 m" r3 _$ l2 {* [def factorial(n):
    3 x, p3 f' I% X    if n == 0:        # 基线条件, [" n- @( O# C  v4 X
            return 14 w- f7 V! V, V* f
        else:             # 递归条件& V: A. K* B# N+ Y5 }. [/ W! k4 K
            return n * factorial(n-1)& {1 `7 y( @/ s  ~
    执行过程(以计算 3! 为例):
    / Y; e! F1 s2 Z# q$ x& vfactorial(3)$ t4 p. H, a2 @+ ~5 f" A* ]
    3 * factorial(2)
    & y* z7 O  J- N4 U8 u% m1 J, R3 * (2 * factorial(1))
      O( [) \7 C: l. o* E! D3 * (2 * (1 * factorial(0))). {( B3 \7 Y  B- D/ d1 V2 o' F
    3 * (2 * (1 * 1)) = 6
    / m7 `: W7 e6 B' A+ y6 g0 ?: {( x$ b% N( n
    递归思维要点
    . k: ~0 q# M- W1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 {1 \+ J8 ~: a8 w9 d9 l2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " X5 y8 B, @# X" c7 K3. **递推过程**:不断向下分解问题(递)
    8 [5 i2 }% z$ P8 O$ v4. **回溯过程**:组合子问题结果返回(归)7 f2 a4 O4 I. _/ ]7 u4 F% F
    " a8 s5 y+ b/ }! Y/ y$ J9 j- @" S3 o
    注意事项; O: N' S4 ^, c
    必须要有终止条件
    8 y, o. H$ T1 y' [' S递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    - b7 K" ^7 E/ I8 _+ m6 s! @某些问题用递归更直观(如树遍历),但效率可能不如迭代" n- S; n* l$ r" u
    尾递归优化可以提升效率(但Python不支持)
    0 \) B5 c. g, _* G& C. }8 F
    . n: ?7 {; x8 t6 V5 i 递归 vs 迭代% F. x- Z5 C/ \( m9 E. f4 l
    |          | 递归                          | 迭代               |
    3 R( `* N" B' p. k* Z|----------|-----------------------------|------------------|
    $ I. A8 n+ p$ V7 T5 |& _| 实现方式    | 函数自调用                        | 循环结构            |
    , k8 W' @) p( s8 d0 C$ s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 E. t4 ]) ?0 o# q
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      |1 H6 |+ S" L4 J8 c3 c% U| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ N$ q6 d0 m' E0 g0 w7 [% x

    ' V4 b" u; Z" y  }; }: g" k1 v! J 经典递归应用场景) `0 T, e4 w  }  s: U2 D; }
    1. 文件系统遍历(目录树结构)
    % p. p# @& s5 k# C' x" s2 S8 _2. 快速排序/归并排序算法
    7 R! ?) O% ~0 \$ f* d2 P+ O3. 汉诺塔问题6 N) u  C- P& m: s! g
    4. 二叉树遍历(前序/中序/后序)) T2 t. K( @2 s7 i0 C* S
    5. 生成所有可能的组合(回溯算法)8 b& ?; A. l* Q0 C6 E
    1 |+ a; ]+ n; P5 b& g
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    1 p5 N% e6 n7 l" @1 P: A我推理机的核心算法应该是二叉树遍历的变种。
    ! F, \5 Q# F- F另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    1 ~/ S9 F' z2 W! |5 U+ @6 ^Key Idea of Recursion
    * k1 T; U6 d. |- D( X9 P# r) ~0 x: p+ a3 p$ G# Y
    A recursive function solves a problem by:
    1 q4 Q! k" o. k7 Y" l' R
    : H/ G( W* R! ]8 p# X  h    Breaking the problem into smaller instances of the same problem.
      B% s% y3 _, y/ P$ P: u4 E5 W9 x  y8 |/ [- c1 A
        Solving the smallest instance directly (base case).2 F" O' {# ?4 D4 K

    + e4 Z. t' P/ e5 _& {    Combining the results of smaller instances to solve the larger problem.
    $ a. l" U+ R; |* \/ n1 @, k+ q4 ?! ~, Z! P7 H$ K
    Components of a Recursive Function
    1 ?- A% F' _3 x# g- E2 Y  I
    + |+ q( c4 W" v7 c; W/ q    Base Case:
    2 ]7 {: ^( R! m* x1 b  L
    ! i; z( h/ I& n1 `) S        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 N- b2 G9 G+ m# p& w
    ) H7 K/ u! p! t        It acts as the stopping condition to prevent infinite recursion.- [/ Y2 u* T- n! X& l

    ) k* B' J- a( n- j: P        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ! b7 t, n( u# m9 d% u( ]/ z" \3 S$ P
    & I0 H9 {2 i: W1 ~. z    Recursive Case:/ S( l9 k  @# \+ a3 M
    / w9 B# I, ~& V4 P( C
            This is where the function calls itself with a smaller or simpler version of the problem.
    ; b2 V" `) l9 f2 b) Z3 k. ~0 x$ \$ p( Z0 j* e
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).. u$ w0 c2 V- G/ r& G% G" O

    6 s$ Z( l; g$ R' P2 ^Example: Factorial Calculation: ?( p" w2 G7 {$ J+ t/ {9 o$ ]

    5 f/ i# W- }9 S; e% N- 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:! b5 N# J9 w' j6 f
    $ G, p- z- M7 U
        Base case: 0! = 1) P, t! Q3 N. s* k4 _; ]

    + ?+ E6 A7 |2 D7 L" `( N    Recursive case: n! = n * (n-1)!2 a6 d, g6 H' c! e
    ' v1 d  j& P' v! G+ T1 f
    Here’s how it looks in code (Python):6 ~; _. M" ]* }
    python6 o6 Q: E" k; e' x6 t1 T
    - g6 a- S6 _; \9 v( w

    9 G2 l# i2 q, U. L6 idef factorial(n):
    $ ?3 k9 M& E$ `5 T- v) v. ~    # Base case
    % O2 p7 x* J' f8 o: F9 Z4 l$ ?! U6 w    if n == 0:+ _) O5 {% k, B5 A+ P
            return 1
    - H6 c. Y* D2 l3 m0 ~; G% p& y# p# b/ ]    # Recursive case. \4 O* s( o5 `& ?* A
        else:
    - ?) n. i  v6 P; c1 C        return n * factorial(n - 1). h, Z! S+ k8 w( B
    5 W: Q0 l: _/ x! }9 t  x
    # Example usage
    , X' l5 {/ X& o$ X1 Aprint(factorial(5))  # Output: 1205 |0 o* p& }2 f% X. ~: q
    8 O' a. t" [- r1 S6 r: u) S
    How Recursion Works
    . S+ s8 E% k6 J* N) x
    2 L9 y% ]3 {' R    The function keeps calling itself with smaller inputs until it reaches the base case.0 y3 p5 ]; H% u
    , S+ |4 r& T6 Y/ e
        Once the base case is reached, the function starts returning values back up the call stack.4 }+ s7 o. a  L$ ]9 x
    7 s4 u0 u. a( k' ?. }1 e3 w3 n
        These returned values are combined to produce the final result.
    , m4 H# H: b# d6 G) T, R' {- {! A) y1 C. W: P. i* L
    For factorial(5):
      M7 M1 t, t) P5 P* r, c2 g  q6 h' i* [0 ~: S8 G1 \0 m+ P

    ! P4 F: j5 j' nfactorial(5) = 5 * factorial(4)
    - ~. p& @' e8 N8 L' y4 ffactorial(4) = 4 * factorial(3): v7 R9 `# }$ L! l6 q
    factorial(3) = 3 * factorial(2)5 F) u! @1 h) [; z
    factorial(2) = 2 * factorial(1)
    : x. u1 O- q7 J* }; h1 @, `factorial(1) = 1 * factorial(0)
    ) o. D7 A( {5 W$ qfactorial(0) = 1  # Base case' {) k$ {0 f" v( ~' a/ N
    + P1 \+ o, t0 S0 q: P
    Then, the results are combined:7 J4 G6 p) ?4 q0 z

    ( E+ b0 q- ]* o9 e. ?3 d+ y  q# P  B9 g& A/ d- A
    factorial(1) = 1 * 1 = 1
    ; ^5 h' ^" Y* p  l; s" N' Ofactorial(2) = 2 * 1 = 2
    % g6 {; Y. i" k1 Gfactorial(3) = 3 * 2 = 6
    5 a9 j1 D0 B4 \6 Efactorial(4) = 4 * 6 = 24  X! m) N8 i/ P/ e
    factorial(5) = 5 * 24 = 120
    * ]# _% }1 ~$ q* [  q/ U2 ?( D  C2 l
    Advantages of Recursion2 {# I1 c+ T5 B
    / ]4 V5 _! ~# h9 d4 s+ S; z1 N
        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).; Q# Z2 z- u* p1 M1 e7 t  M

    ' L$ Q6 J$ @6 R$ y    Readability: Recursive code can be more readable and concise compared to iterative solutions./ X! s) Z4 O! o8 W
    + O! h+ i: N: f0 M# i
    Disadvantages of Recursion. ^0 g0 H4 X7 p( b2 [6 L! I' ~
    9 ]) n& d3 G; M" X0 F  A9 R; t
        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.) y2 b  z! _3 w. U/ F2 `

    ; e$ O! U* |+ |0 `    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    2 g! S7 V- p+ h4 x& v. M7 B' i
    # \4 Q4 Z! S9 e( @When to Use Recursion
    8 k' l! Q4 X- @' G6 q- y& C! _) |1 \1 ~7 f7 n
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).  x# Z6 u2 V; z

    7 x5 j( \- m. D    Problems with a clear base case and recursive case.* M& Q) E' L8 s/ |7 F( y
    , `! Y5 g, |4 s2 j1 M
    Example: Fibonacci Sequence
    : j. n) G* d4 m( Y! o* _" D% o
    . E5 U& P/ q- `2 jThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 A! o/ ]+ S7 b1 T% A& B
    # M( u/ L6 A; p1 O; G
        Base case: fib(0) = 0, fib(1) = 1. R" s' d# E3 j# y1 P' J
    ! R, s  o. B; {, f$ F5 v6 `
        Recursive case: fib(n) = fib(n-1) + fib(n-2)) T2 ]2 K' J* J2 n  h6 u
    ) Z8 O% _! `- `3 v4 H
    python
    3 [1 R: f; R9 g2 N6 c6 [* T" i1 C4 e- T6 ^; ~  o

    ; K$ `$ j+ f1 T9 S  B% S& F5 O5 y- edef fibonacci(n):3 @1 e; Z$ u" a: O+ S, U& R) ?/ A
        # Base cases9 z% \  X' k0 D2 M
        if n == 0:
    4 @# ^7 \  F7 i        return 0& a* S+ J$ h1 {( g1 o$ _: s
        elif n == 1:4 h: R0 K4 l: j0 }  `- H4 b. \
            return 1
    / Z  c% r8 H9 ~- Y    # Recursive case
    5 J& B6 X3 B5 m. Z$ b" Y0 Y    else:1 ~4 I9 r$ z1 U1 m$ F
            return fibonacci(n - 1) + fibonacci(n - 2)
    8 I1 |  N" _; x- q; N  i/ z
    ( r6 O2 K9 x% v# Example usage% |, C+ e, g5 v7 m
    print(fibonacci(6))  # Output: 8; i! S9 }/ e& l7 U5 k0 V
    4 \) ]/ r7 Q) g& q6 ~$ A& }: |
    Tail Recursion& F, I3 @- c. e) a0 H
    5 ^( d; t2 H- 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).
    6 |9 @- E( Q0 f8 s( y
    0 R/ N5 g/ t* t9 \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-1-19 11:45 , Processed in 0.028900 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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