设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 " e3 q5 Z/ W( c8 V& G( A* e7 n% v

    & X" t, K: j& o2 H1 y( S解释的不错
    9 D' f! J0 V5 G
    2 |) N4 q; Y# H! m4 }递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : Y# v3 n& ]6 K9 b$ _% g7 ~* j0 K, ]; z% @( }" L2 e9 A
    关键要素( s' i  A6 n2 F; a
    1. **基线条件(Base Case)**# N  T+ u- f' S, l* N! w$ }
       - 递归终止的条件,防止无限循环3 A& }( \& ?4 K8 Q( c) Z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 A2 n7 L: b4 C) L; x% A& ?7 s( y; l" i
    2. **递归条件(Recursive Case)**
    7 `- m- s- @: O' L+ k* z5 R   - 将原问题分解为更小的子问题0 V1 W, F* Z+ A( a0 l+ q! r
       - 例如:n! = n × (n-1)!  D; ?! a/ o  R; q

    * N' y* g9 H" d) k* D4 V 经典示例:计算阶乘2 r& c- T3 Z  ^& {
    python
    $ [2 q/ }- b) E' C- ]def factorial(n):) @7 G$ f; w/ K2 K% A
        if n == 0:        # 基线条件
    9 {8 a8 @! g$ _9 A4 y        return 1
    ' ^+ R+ F4 P' Y3 \& z% c* b    else:             # 递归条件
    0 _0 B% ]1 ?- c7 M; H+ n: z2 _        return n * factorial(n-1)
    * O- e  ^- P: r; R执行过程(以计算 3! 为例):- y. x7 E: e3 D; X! X$ v
    factorial(3), x+ V" g+ s! {/ m
    3 * factorial(2), x. m- ?" P5 J
    3 * (2 * factorial(1))
    ( D9 ~0 I8 f) O* ~1 F3 w3 * (2 * (1 * factorial(0)))' Y$ d0 g" B. k, m5 t
    3 * (2 * (1 * 1)) = 6
    5 C$ O2 V6 @5 \) ~0 A3 K9 I! }' \5 O! h  e3 z( h
    递归思维要点
    & t/ u5 u4 G& _3 j1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # O4 f# |  N/ M9 R. i& `, V2. **栈结构**:每次调用都会创建新的栈帧(内存空间)1 E4 e, S1 @  F( q
    3. **递推过程**:不断向下分解问题(递)% j+ k' b$ m5 x9 _6 K
    4. **回溯过程**:组合子问题结果返回(归)( j" T! j& Q3 R

    0 C* C+ [1 G2 y6 D3 ?5 v& \  W, |: ^ 注意事项) C0 B2 w& V/ e; [; a! y
    必须要有终止条件
    4 c1 n9 u- z, P0 H递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! m- i( D3 Y! j6 U1 H0 @某些问题用递归更直观(如树遍历),但效率可能不如迭代0 p3 _# ^) M1 E' G
    尾递归优化可以提升效率(但Python不支持)
    & _4 Q* t" x+ y9 G$ v
    $ h# j2 |6 N& d" b. X& P 递归 vs 迭代% J& [& c( J* a+ @
    |          | 递归                          | 迭代               |. r, F) h- ]& g9 n- ]
    |----------|-----------------------------|------------------|
    1 {. ?/ M0 H+ E* @/ j% D) Z' ]| 实现方式    | 函数自调用                        | 循环结构            |
    6 u. H7 m5 K/ E. L, T/ u| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , }& R/ O' U9 L  h; s| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 k+ [# s% n- [) O' E1 F| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 u3 g0 N8 |# p4 q4 j; g, u0 t8 }$ H5 C+ A; u
    经典递归应用场景+ m' p. t7 y9 w6 Y
    1. 文件系统遍历(目录树结构)
    ! E" p" j/ X' S9 U" U5 l1 H2. 快速排序/归并排序算法
    ! T7 k1 o( ^8 s( C0 |8 v3. 汉诺塔问题
    * H" X0 o, ]9 f: n, R2 m5 v* d% U* E4 W4. 二叉树遍历(前序/中序/后序)
    3 T$ q* T2 {2 o% @7 S( n5. 生成所有可能的组合(回溯算法)
    4 d9 x  q+ X  m6 y7 }% V/ B+ \" c
    5 T* }3 z8 B2 ^* Q5 l/ f2 Q试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    14 小时前
  • 签到天数: 3114 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    - `/ Z, p: ]& y- e. t" a6 R我推理机的核心算法应该是二叉树遍历的变种。3 c, ~& ?8 y3 T( H- A' d/ L% s! b
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 w/ m$ C9 E* x1 x5 C+ D% L$ ]- iKey Idea of Recursion
    0 k+ E. C( K8 U8 g$ m, s/ B1 ?1 z" l7 q
    A recursive function solves a problem by:  {, R6 u# b6 ]

    1 e' X' d: ~% b5 V: y9 i    Breaking the problem into smaller instances of the same problem.7 u1 j1 _2 f4 g* C  `& N2 X& g

    4 N7 u' {2 H: I9 H    Solving the smallest instance directly (base case).5 i# f4 g2 X& t6 |5 @/ X( q

    0 m1 b# i$ _' I- d0 S    Combining the results of smaller instances to solve the larger problem.
    ; R( r  }$ g) A1 \1 u* T
    . D( t( j  m# P' QComponents of a Recursive Function
    & A8 M6 z4 O: [8 N4 {8 q/ D, b2 h8 X! o1 [! E
        Base Case:& ]6 }  @; P8 C+ q  j
    ! K3 t0 s3 ~4 W9 n
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    1 p2 t2 |5 H1 x. Q! Y8 N; W9 F: O5 U8 L
            It acts as the stopping condition to prevent infinite recursion.
    ' y$ g! W/ M+ g) H
    2 }# I6 y4 U- e+ r* {- e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' M/ V/ A2 H( u6 T
    + |- |: ^# c& l) _0 |
        Recursive Case:
    9 v5 u1 i- e  _! ~* e& P6 [4 O; u4 Y! I; Z( `6 ~4 y) K
            This is where the function calls itself with a smaller or simpler version of the problem.
    " E6 m4 A% b# w$ R( A: z3 W% P4 q0 c6 t0 h. W
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 {; k7 x1 @$ J1 L  s6 N" k; I
    : ^# j9 s$ ?8 L5 {6 o8 d+ B8 PExample: Factorial Calculation, A1 n; E/ w4 q0 t4 x, w. _: a- Q
    : F# o: d# ?" I" N
    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:
      V) T& p  d; r7 H' P8 a6 S
    0 R. X( @9 P) s7 w, g1 R/ G    Base case: 0! = 1
    / ?3 r+ _" V( a$ @( S
    / E5 H) R. I( {# s; _    Recursive case: n! = n * (n-1)!
    ) K* Q' }6 w5 [. V# |- l1 i1 `; F! s  F5 I! {% k7 ^
    Here’s how it looks in code (Python):( C( v, \$ E/ T9 Y/ Z& Q- C' h
    python
    2 ?( k. ^8 P* K6 d( ?# E2 P1 A) b/ u( R# S8 ^) |, ~0 t) s

    + s0 S" A, m  ?! ddef factorial(n):
    ! i' ^. C! Y2 g' S. H( t* H    # Base case) E4 l3 h; d) ?
        if n == 0:
    3 ]' U# O$ [- M9 x$ ]$ {( A4 o0 q        return 1, g/ I+ y) h$ W. q. [7 [& R7 }
        # Recursive case9 L$ l4 c. O  s$ S
        else:# b& s) n* R! K# M; Y8 m
            return n * factorial(n - 1)8 v5 X  j3 O, E6 Y
    ' ^+ A$ u. D/ x+ i# V7 I
    # Example usage9 O( L6 \$ L+ v) G: ~3 P, I
    print(factorial(5))  # Output: 1200 T& v) J& H; b/ k! |1 v% h

    , y: z! M+ l5 [5 H* V  h$ vHow Recursion Works' j. y/ z6 ^+ {+ N

    / F: o2 h8 o* D) J3 N; U& D4 e( h. K7 s    The function keeps calling itself with smaller inputs until it reaches the base case.2 `$ {; ?7 ~4 ^. t

    9 ~! O" N  E) q$ K7 i, T) y    Once the base case is reached, the function starts returning values back up the call stack.
    * f' _" j% S- f# G. i0 a7 b: d% M( u2 ^, t# T
        These returned values are combined to produce the final result.
    # \. S% N( m8 j: A* Y+ F9 A5 a2 y8 H# K7 C# t4 F1 L
    For factorial(5):1 ?. Z8 W9 u+ \' T/ M3 w$ L

    + G; k5 f  C6 p+ ?. q  h; D: L
    : f* i- u% y, K8 ~0 sfactorial(5) = 5 * factorial(4)/ f3 {( A$ Z: D2 v5 I& s
    factorial(4) = 4 * factorial(3)
    7 r: g/ C2 o$ y2 S8 v) Xfactorial(3) = 3 * factorial(2)
    % t7 v" i/ P" S1 Cfactorial(2) = 2 * factorial(1)
    ) C6 {3 X0 j8 m  Ifactorial(1) = 1 * factorial(0)
    " l- X' w4 z6 W' D" Q! D/ gfactorial(0) = 1  # Base case
    $ {" u2 O5 C' T! z- @! B) s' d: ^  Y! \8 b: |
    Then, the results are combined:  U0 {2 t$ _+ V
    ' ?' O; n  K- x. U8 _
    " r( e3 N: g2 y4 a1 `
    factorial(1) = 1 * 1 = 1) j. f6 a9 f: m; G8 w
    factorial(2) = 2 * 1 = 2- h' v4 ^- G! Y% T6 p& [9 [& y: L
    factorial(3) = 3 * 2 = 6. z' {0 ^  |; d
    factorial(4) = 4 * 6 = 24' Z$ |6 {* _2 ~7 Z9 x. e
    factorial(5) = 5 * 24 = 120, B/ X" L9 ?5 Y4 R; r
    " |" X+ T/ e% D# p& k
    Advantages of Recursion9 d6 O' j. q* l7 r
    # {+ @7 J1 Q# D6 ]' n! H, 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).2 ^0 ?$ ?: h+ t# h. n1 w
    $ x" M! g' L: S! _8 J& ^# C6 F6 d
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - S  C& K5 s& _4 _+ L+ x$ `: x$ I" G
    Disadvantages of Recursion
    4 d6 n( q1 @) E8 O: G  `. H) b7 d6 f7 {/ k6 b4 n0 i+ F' o
        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.0 ~& ?- Y" t3 w+ F3 z/ q. g

    , B) Q! x- R- c( |/ d    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    : u1 B6 G8 k: a. A7 ~, P$ M" C/ N! ~# \% `
    When to Use Recursion- j, |. r6 ?$ j, q3 @3 h

    " m' L" E/ \: B. j. ?7 c    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % ?. |6 g1 u/ |5 Q/ S
    4 i* D. j6 Y3 y: G% X, `0 f3 Y    Problems with a clear base case and recursive case.( \6 E5 V! H7 L5 O9 q- `3 u1 k
    3 g% z3 X4 K) d
    Example: Fibonacci Sequence% N% u0 B" P5 q  z3 H- ~

    2 b4 q9 k( s( h5 g, ^0 f8 t* BThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:! o" O- E- Z4 l" E* y/ N1 v

    . }) `' \4 ]' y# ]    Base case: fib(0) = 0, fib(1) = 1: b% q. B2 @+ H( C+ |! ^/ |3 e
    + ~( u2 s' k  b7 C5 u7 N& O( {8 }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    , l5 N4 \4 i2 |0 d/ d/ F8 G* P) R' I9 B* _% E* Y; `
    python0 e, s) Q, S; |$ ^$ b

    ' k0 c1 r3 R4 N1 J) U
    * W, p0 u, W8 @def fibonacci(n):
    ; L, N  M2 c7 R9 y. K2 o0 r! b& v    # Base cases
    & O, j! c5 x1 P5 j5 f5 N    if n == 0:
    - l. \2 i8 p9 n+ \# [: y        return 0" o* Q! C1 R; y# r# ~! x# }/ A2 t
        elif n == 1:" F: `2 b" r& R2 ^
            return 1; N- o, c8 M5 b8 T4 @: M! ]
        # Recursive case, W- B- @8 i  G1 F5 }$ y$ V' h
        else:6 F1 d/ e3 [" c2 M" k6 o9 E
            return fibonacci(n - 1) + fibonacci(n - 2)
    " J& |4 v* [9 }1 x/ \7 B
    , ^4 a9 N8 c" R4 ^1 Y6 T# Example usage0 s* d2 H* j! o. f
    print(fibonacci(6))  # Output: 8( @% g7 @0 p. Y/ s6 o1 a! T
    ! o% K; K% I6 k$ F# B
    Tail Recursion( M" b3 a1 o% ~( r( v& S/ A
    / w2 i/ Y! k  z1 v3 ~2 p8 A2 e
    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).- m1 T3 z# M$ f2 b1 V% T
    ! H: I0 L9 S: D9 |( r& b
    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, 2025-12-10 22:01 , Processed in 0.029100 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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