设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 & R# o& @, q  [/ s* k0 \8 k0 c  I

    # E3 Z7 s* M5 i- d解释的不错
    1 _! |) l# W6 A: u8 m+ U' d5 D# Z) R. u* k+ p& T+ O% h, t/ j6 ?* S( |
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    9 O' b% k& E* I7 p
    + g' j2 k# Y; w  {+ U 关键要素; ]! u8 I8 m, V2 x3 }
    1. **基线条件(Base Case)**5 U; F; S2 p, r6 R& R  a
       - 递归终止的条件,防止无限循环: f8 t2 C& V9 _7 K" }5 f# l6 W# Y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1- U! D) X2 @0 U- ]2 h

    ; y& r7 G: n. a$ o7 }, u. [2. **递归条件(Recursive Case)**6 D% t0 A) y  ~
       - 将原问题分解为更小的子问题
    4 r8 _2 U9 @7 s) F  F   - 例如:n! = n × (n-1)!
    9 K+ f4 M$ B/ K  S+ n, Z  d: C+ K: f
    经典示例:计算阶乘3 ?( O5 o5 O4 C! q3 I+ y" J* k1 z: {
    python
    ( U/ Z) j  ~4 X6 }  sdef factorial(n):( m% e5 H+ l0 l. W; j' p; [
        if n == 0:        # 基线条件
    5 r# r" q$ E  V# e3 n1 N        return 1
      t! I4 s5 Y+ O- n& z( d    else:             # 递归条件
    6 ]9 }' W) i# d+ U        return n * factorial(n-1): m* U$ Z/ `0 G1 I. F+ @
    执行过程(以计算 3! 为例):# |2 x$ M1 T( k7 f+ f
    factorial(3)
    6 F$ d! ]) J6 }# Y3 * factorial(2)
    1 H% _& s3 o2 p3 * (2 * factorial(1)); J: t. G$ P6 w9 T/ t$ A3 _) g
    3 * (2 * (1 * factorial(0)))
    , A  A% N5 S5 B3 * (2 * (1 * 1)) = 6
    8 U8 ~4 G4 S) ~! _9 s$ K' X3 b3 y5 y- G$ R" e# ^
    递归思维要点! {  t4 q" V3 S
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑/ K1 V" g1 }- r8 Y% r! w
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间): n+ D) i# v/ U( v) \
    3. **递推过程**:不断向下分解问题(递)& c; n0 j, c) [6 v
    4. **回溯过程**:组合子问题结果返回(归): U5 o/ N8 _# b9 R

    $ b: K  P& Z7 x4 G& r) L 注意事项, \! w: Z4 H; N* q& `0 W
    必须要有终止条件
    % O/ F* n9 x  C, @* ]) ?: ~递归深度过大可能导致栈溢出(Python默认递归深度约1000层)( P" S* e2 M$ @
    某些问题用递归更直观(如树遍历),但效率可能不如迭代# X4 G; C4 ]9 s, D2 F9 r
    尾递归优化可以提升效率(但Python不支持)
    * f! q* O2 t' i% n3 |! H% T4 o/ r/ a# B! r, S5 {
    递归 vs 迭代4 ]" E: q8 y: a: _4 ^% V$ |  G
    |          | 递归                          | 迭代               |+ ^. h3 |! o( l' M6 @
    |----------|-----------------------------|------------------|
    ! X5 N, |+ W& Q& \5 G& W- Q- _| 实现方式    | 函数自调用                        | 循环结构            |8 J7 @0 v. D" X4 Y
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |3 k) b# T% t' f, b
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |. v$ m' s2 V4 |
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 G$ U3 \2 @$ Z5 X6 P* `& K

    0 L1 l0 M7 G/ L) G- p 经典递归应用场景, x* S$ m1 D2 u2 C( A
    1. 文件系统遍历(目录树结构)
    , B2 ^" O& @) y' l9 g3 N' s+ e2. 快速排序/归并排序算法* X! W7 ?) z+ N2 S
    3. 汉诺塔问题1 t* i2 W6 L# P
    4. 二叉树遍历(前序/中序/后序)
    3 l* n& q  U, `& d5. 生成所有可能的组合(回溯算法)6 J" Y  a& w, V/ k3 _4 l" H

    ! o0 _% D# h/ h- I& W" H, N试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    11 小时前
  • 签到天数: 3211 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 q7 X+ a& b  ?) G- j! q  q我推理机的核心算法应该是二叉树遍历的变种。9 m% O' X  P5 W7 V% G
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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$ d+ s' w. T- ~
    Key Idea of Recursion6 D- d. U( U' {2 k* R& [) X0 W* W& S
    * [2 n4 t; ?: F% j
    A recursive function solves a problem by:
    0 M  y1 {  W; y0 ^" n- ~* m7 T( \' ?9 P: r
        Breaking the problem into smaller instances of the same problem.: Q+ Y5 G: O3 M! F; Y: R
    : A5 [- B5 J' T4 ?
        Solving the smallest instance directly (base case)." C8 s4 A. }/ s: t! Z# ^9 `
    # z/ e. {7 e9 Z  }4 |9 K/ Y- z8 i
        Combining the results of smaller instances to solve the larger problem.% ]4 w6 }! K6 r$ t

    ) `  d4 r/ K5 E+ ~Components of a Recursive Function
    ) g! q4 L6 c# O5 j$ \; s8 l
    ( }6 _4 N. n& v) a+ y    Base Case:
    5 g) l) O0 h! R4 K4 l" V
      z7 }! i3 Z. i: b        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 }/ Q1 _7 Z# h2 r; |% g3 X- k9 y7 g' m' `
            It acts as the stopping condition to prevent infinite recursion.& w$ ~0 Z2 o4 _
    ; ]3 Q* f; g0 e3 ?
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * q' |$ S7 H7 ?+ N& e8 }" Y* ~7 o% K6 \0 T; t+ x
        Recursive Case:( w4 w: O1 ~* n7 T; ?' B) z

    ! ~( z( E, a0 q$ \0 `& h3 Z        This is where the function calls itself with a smaller or simpler version of the problem.
    9 Q+ ^9 S/ s! P! @& z* a6 _$ K1 m
    & H3 {$ `* T9 s" {$ H6 d+ |# `: Q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).5 _: {+ n( p" O
    7 ?0 h: g6 }  Y9 R% }8 e4 q
    Example: Factorial Calculation: x1 ~: [! t. F2 j* h

    2 G) k( u* G0 e0 ~  _: d6 L9 d8 DThe 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:
    8 U5 e' v" j7 D" l3 W. d
    1 r  z- b: Y# d' @7 G7 |    Base case: 0! = 1) h% L" |$ s* K! W0 G! A; B( ?( B

      K7 Y+ o  D6 j+ T; Z    Recursive case: n! = n * (n-1)!* d( e$ E1 R$ [8 b+ Y5 E9 c
    8 W1 I2 Y3 K" n3 A; A
    Here’s how it looks in code (Python):
    6 |* `: E6 }2 H" k3 Fpython) ?- d9 E- i" U+ F9 B4 ]

    5 Z$ y/ B3 [4 w7 U$ {( \4 o( J6 t: s5 ]/ B
    def factorial(n):
    9 T# X+ w4 f4 A" I9 }( s    # Base case
    ' H0 y4 b+ p0 f    if n == 0:0 ~4 _  s* I4 y/ e# l1 `
            return 1( J% m' l4 X* x% k
        # Recursive case
    % S8 {/ I' r' Z0 E$ `7 n* G# [    else:# y% v# k/ T$ @! |% m+ t
            return n * factorial(n - 1)
    / q8 R# g3 P' U; S: R9 i' c: X" n9 j& j( g6 T
    # Example usage
    8 {: b! a0 t8 J. ?print(factorial(5))  # Output: 120* s- c- g# n3 _9 w% Q( f2 L: v

      n! X/ n" p2 \; ~" K  \0 jHow Recursion Works
    $ L$ _/ \2 c  `! G7 Z: |4 R0 h5 h
    1 r8 k  [  H# X( I# Z    The function keeps calling itself with smaller inputs until it reaches the base case.8 T, W0 O( H: R, I7 u$ R* k8 q) d8 \
    - y$ B. Z( X5 |" \* y- J0 f9 |1 j6 y+ Y
        Once the base case is reached, the function starts returning values back up the call stack.
    2 N( P% R& ]0 |+ G4 g5 v
    ! t( p0 X6 j, M0 _; j    These returned values are combined to produce the final result.
    / t5 R. @9 t# S& y( h2 C- Y1 W, Z2 a3 a  e" d: C9 M
    For factorial(5):/ t1 k: D. F  C: d3 E8 E

    3 Z+ C( G1 O9 ~  s  J
    9 i& {; |- P. }factorial(5) = 5 * factorial(4)& w8 d+ i5 l2 M( R
    factorial(4) = 4 * factorial(3)6 ~4 n' g3 P) T; {) t
    factorial(3) = 3 * factorial(2)
      _, C8 U8 z7 N* {factorial(2) = 2 * factorial(1)" F8 I' Y7 g" Y
    factorial(1) = 1 * factorial(0), [2 w* A4 G$ b
    factorial(0) = 1  # Base case
    + b! N  n& d  |* o: p
    * s$ M. r4 V+ |8 HThen, the results are combined:& I8 Q: Y. V. t. Z# R
    # U' T4 m* h5 p% {
    , ^# ?' q/ c0 G0 G
    factorial(1) = 1 * 1 = 1
    / @, G! w9 R: e6 E! [% lfactorial(2) = 2 * 1 = 24 {4 T. |' ]" p) Q% \' s
    factorial(3) = 3 * 2 = 6% {3 S0 \/ a7 [6 g% p
    factorial(4) = 4 * 6 = 24/ [: c( H4 z/ f
    factorial(5) = 5 * 24 = 120  M9 X1 u2 L* o3 Q! H# G$ [8 l

    + a/ w2 F  T* v3 lAdvantages of Recursion4 |! n& o0 {* ^$ P+ r

    3 a9 B( f: C& [2 g' L6 B    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).5 k# m1 c: r5 q. y& E6 L; b4 ?

    " l; Y; z8 [# E    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ' u% K2 m  d+ d: d5 H
    3 Q2 {1 v5 [6 }$ ]# E9 dDisadvantages of Recursion
    ; i! E" v6 l9 A% E6 ~. k  ~
    7 e7 e; D3 B+ e$ W5 _% k! g: L8 D: C    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.& X" S, }8 H( P1 I; U& i3 J2 [- @

    1 R5 |9 J) k; A5 z& u* n" A    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& W' t, f% l% d" p$ }( G  H

    ( ]; k1 W$ b/ a, B0 R# vWhen to Use Recursion
      T( Q4 C" G5 l% d
    ! H0 j4 e/ r8 N/ z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # V8 {) O; C4 ^2 U' G' L
    / ~" i; y8 ?" R* s( b$ [5 ]) x    Problems with a clear base case and recursive case.- K) O( C' Z" f6 Y1 f
    ! n1 u4 ?5 y$ s
    Example: Fibonacci Sequence
    - c9 d; h8 {% A% n. h( w  r8 V  \% \# R/ h: j* ]4 I
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    4 _2 O( r5 p. L* V# a9 v5 {- A) o
    & m( P7 l9 [7 x: P6 O7 y    Base case: fib(0) = 0, fib(1) = 1; K; a( R! F7 W# _/ N

    + E9 n; a- ^. S+ D7 `, n- r# t    Recursive case: fib(n) = fib(n-1) + fib(n-2)( j+ F# B% j) X; G  r

    & c0 N, J5 z: Gpython! n" k1 H  e  @# D5 B

    - ]- r. r3 b/ n4 q/ o$ E+ C4 X9 G$ r9 g
    def fibonacci(n):
    * p! ?+ N$ K; Z% `3 A    # Base cases
    ( H4 A$ z7 Y: R# I    if n == 0:
    1 b( H2 G$ Y( l: R  {0 D        return 0
    # G4 D5 L* v% Z: M  R$ W6 p    elif n == 1:. i- d) _( Z  O" `
            return 1) K; W6 y! t5 c7 ?" |0 `0 b0 Z
        # Recursive case- F* G1 _0 Z3 }+ ]; ?; ~; @- I
        else:
    , K' C6 O- z$ t$ [: z  u        return fibonacci(n - 1) + fibonacci(n - 2)
    3 M! p9 }' f$ ^7 e" p3 b: _6 k5 F5 q) i6 \% D' B
    # Example usage  x  I" T& w- y4 t5 q
    print(fibonacci(6))  # Output: 8
    + ^3 r; G4 n, x
    1 l" m; b2 {# }. L- H4 k3 b# ?: }. z; HTail Recursion
    5 m/ i  B- ~5 ?9 T$ h$ |( }9 T" |- {; M( t3 x
    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)." R2 O5 M! O6 L8 b4 |- s
    ; d7 d0 [; k# B* @/ [- t' a# 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-4-3 18:10 , Processed in 0.062778 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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