设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    4 o% `' w$ n% k! G% W# g. t2 O1 N
    解释的不错
    ' j0 U& q7 o+ x0 ?4 w! C+ Y1 G5 }; _# }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& Z! t: i: F6 S  V( l5 H' q9 N  X* E
    % m  w+ s- q/ i8 p% D
    关键要素$ L5 o( Q5 G1 J4 |$ S8 n
    1. **基线条件(Base Case)**8 h* S" o+ \* ^; [/ `% j7 l
       - 递归终止的条件,防止无限循环" C, ?7 x7 R0 j$ @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1+ z9 J- k& Q& |' N( u0 g# Q3 N

    6 B* s9 ~8 B6 R7 E$ O2. **递归条件(Recursive Case)**
    ! ~6 ]; J, J$ x! V   - 将原问题分解为更小的子问题& l7 E( L! U' |( G3 f- A6 k
       - 例如:n! = n × (n-1)!5 Q- v6 E4 K2 ^- u& ]

    7 J' p  ~& {( {; v, m  {9 s 经典示例:计算阶乘! N% o" {# F( a0 X
    python
    9 _' s# E! T# O$ D. \def factorial(n):
    7 _& V# M9 P9 L9 I# ~    if n == 0:        # 基线条件* [4 {. k+ p0 G0 k$ j7 H
            return 1
    % [# r, x2 e$ `( n$ d+ R    else:             # 递归条件5 {( u2 e  A7 Q& B( s
            return n * factorial(n-1)
    ! A" z) O8 L# t; x" D6 u& O/ K7 T执行过程(以计算 3! 为例):) X8 w" q8 p$ J/ U9 s" v' Z
    factorial(3)
    # j0 V( z  W7 W) c3 * factorial(2)3 D: b7 n  ^- Y$ \( J- Z0 D. @3 w
    3 * (2 * factorial(1))
    4 O; u  Y( p# R% z+ c8 h3 i# k( k3 * (2 * (1 * factorial(0)))% Z+ v! {9 m$ u* g1 V
    3 * (2 * (1 * 1)) = 6# V6 v! L/ e. K) Q- L
    2 W. r2 ?; O" w" W9 Y
    递归思维要点
    & s8 Q) R6 `& _& z1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : W6 x0 u  i# R8 ~2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    0 `- U' L. l: i& v; p" ~' v# C+ b9 }3. **递推过程**:不断向下分解问题(递)9 E1 f2 d. _4 I9 O+ d
    4. **回溯过程**:组合子问题结果返回(归)( y" |" b! W+ U1 L5 J' X6 x, Z# A& O

    1 V% Z, U9 {: I8 m8 r) a2 |. L2 o  z 注意事项
    ) [6 b% T# G' ^: U必须要有终止条件( A4 G: V' d% g/ o& P# ~; B
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    # o: b' A& Y9 b$ N9 f( o某些问题用递归更直观(如树遍历),但效率可能不如迭代
    7 Y% V& K4 }' g# a* v' d尾递归优化可以提升效率(但Python不支持)
    2 p6 B5 J' i# Q# B
    " f# D3 Q& p( `/ F2 E 递归 vs 迭代
    # m& A$ \& \8 s: Y|          | 递归                          | 迭代               |
    - r! ^8 d/ @2 }% f  }7 w7 M1 J|----------|-----------------------------|------------------|- V, t9 S. G8 ?- Q- y+ Q$ d$ O9 r: R
    | 实现方式    | 函数自调用                        | 循环结构            |
    - E/ k2 P/ k6 ?: n| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    3 K3 |& i' Y. \; L, i| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; O; Y. b% t7 S9 U. K, ~| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) D) [9 H1 I6 k# u# l3 J+ r

    / X3 [; L, }, G2 a/ ~ 经典递归应用场景1 M9 P+ l2 e1 R/ O. p
    1. 文件系统遍历(目录树结构): u) N' G& l$ I0 |5 @; R
    2. 快速排序/归并排序算法9 P5 m4 h0 A& C8 X# E
    3. 汉诺塔问题+ c+ R9 Z/ G& o2 r, n
    4. 二叉树遍历(前序/中序/后序)' i, l+ _: D, K4 t
    5. 生成所有可能的组合(回溯算法)4 }( {0 i' \' y3 Z$ c

    / H) v  }2 F2 d试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    7 小时前
  • 签到天数: 2930 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    3 R; ^% d) N% L我推理机的核心算法应该是二叉树遍历的变种。) ]+ |3 |8 b0 S" E
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 L1 n+ t( ^5 {7 q) G/ CKey Idea of Recursion
    ' G9 i8 c7 b. F1 r: ^$ l+ L/ ~: p9 H/ P  \0 ~9 h3 k- Q1 @
    A recursive function solves a problem by:
    # I1 z" T8 |4 h, x8 e9 N/ E* K, d$ x8 q  k6 I
        Breaking the problem into smaller instances of the same problem.
    5 x  D" H* K0 {0 ?2 c
    + `- q0 x  `" W; S    Solving the smallest instance directly (base case).
    9 i% Z1 ~+ D! h  n  `" P% c( B" ^) Y# P4 q
        Combining the results of smaller instances to solve the larger problem.' k2 w  n" \+ ~$ n1 p

      |: c+ k  R% Y( Q# h6 BComponents of a Recursive Function
    2 {, P2 h1 y! q! d( f, [6 {9 k
    ! g( w3 y9 c( v" I    Base Case:
      k" w$ p; X' V4 j8 P# e- x: C
    * V! z9 M6 u! ]3 f$ D        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 h5 |+ W+ v9 N# B7 t; }
    - w. s) s& Z) B1 U        It acts as the stopping condition to prevent infinite recursion.2 O5 q. Q) c+ _- o) u
    ) E! N% l* I: B6 {0 r1 [+ v1 C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.6 C. ]9 y# \  W, {# f! U0 N* j  w
    ( w' E) v3 `4 Y7 b; }; O5 G$ W" `
        Recursive Case:0 Z6 z4 y3 n5 j& o7 q+ M& {

    * U. T7 Y# ?* v. O4 v2 l- c        This is where the function calls itself with a smaller or simpler version of the problem.
    $ q6 y' E8 t! a2 B& |( M( i4 S8 N# `! l3 e% Q
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! Q& b3 J$ B0 K9 f" Z, t" G* K
    - N+ U! \& q- _0 V, d, e$ N5 o
    Example: Factorial Calculation
    4 o6 A7 U, w% |
    9 z; R# ?7 a5 w; iThe 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:9 j6 n. p; l& B& L& S5 P0 e

    ; S( ^8 k7 w4 M# m    Base case: 0! = 1+ {! Q' J% }) K$ S+ I5 {- n' A8 m

    8 _6 q) M( C# M; m    Recursive case: n! = n * (n-1)!
    " w/ \/ y9 L8 E9 {. X$ m! z$ R0 \
    Here’s how it looks in code (Python):7 ?) t6 y0 d9 p/ t
    python
    " o' v4 V$ e4 h, ^5 N; F8 e2 L- l3 ?2 y

    0 ^9 E0 A+ x/ A2 Q6 B: b( Q( S: tdef factorial(n):
    : g" p# |3 I# }- R/ x0 s. u- J    # Base case
    - O% j$ z5 _6 R1 \5 y    if n == 0:
    / y+ V6 p) G) m) W        return 19 a8 {/ \$ q( b/ C1 e" P6 r( a
        # Recursive case
    4 r& t; q! O& g! [% V+ L/ s    else:- i4 t1 o4 ?& V( C
            return n * factorial(n - 1)
    2 q3 c! ]0 `. ?# d
    ! O8 \' Q, l( U) r+ H/ V4 f3 b# Example usage1 J# v' V* q: M" z, @
    print(factorial(5))  # Output: 120: C% z! U; U, @) o" m9 V
    : h, c4 c7 f" j* e) f7 @
    How Recursion Works
    0 K( \4 E7 d/ {/ @3 D6 Z1 l7 D! ^6 c' w3 h. l
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ( _2 @: i6 t+ o4 h4 {. \. R: ]2 G4 y) y. e, O# @
        Once the base case is reached, the function starts returning values back up the call stack., s# u' c0 n8 ]2 f7 C; w; i

    ( B8 v1 k$ y% }! H# m7 n# [, i) i    These returned values are combined to produce the final result.: Y5 J' e+ {" I5 S" i2 x

    + G7 i0 C4 ?' @9 {% R- r( H/ {6 a# mFor factorial(5):
    $ e  F) y$ ?) y3 K; F
    3 W+ S' X, u9 N' y
    : B5 [1 S& ^6 V7 o6 ~) S' w+ ^factorial(5) = 5 * factorial(4)9 u) |* v5 V+ @% R- [; }" Y, ~
    factorial(4) = 4 * factorial(3)4 V& J" D3 \7 i5 y: T
    factorial(3) = 3 * factorial(2)0 y8 L/ D! n5 U. ?
    factorial(2) = 2 * factorial(1)
    + T$ D  x8 p% ^9 Tfactorial(1) = 1 * factorial(0)
    : d: A) m# x; q7 j9 Qfactorial(0) = 1  # Base case
    # E! _; L$ b8 P# j+ f8 f9 c" m& g) l
    Then, the results are combined:
    ) _9 ?3 U- e) D8 Y* u# U2 b6 O- c5 ]9 e* Q( v6 h

    4 p  H# T5 t% U/ q3 @) Z" V$ Afactorial(1) = 1 * 1 = 13 U" D  x# d( Q9 N. S) F1 s8 w4 y# {
    factorial(2) = 2 * 1 = 2
    ; q3 Y3 v" N2 a" ]; N$ B2 Mfactorial(3) = 3 * 2 = 69 v7 Y$ Y( {0 d$ j! T/ p2 |
    factorial(4) = 4 * 6 = 24
    5 R$ P; c+ w4 |* r' N$ Ffactorial(5) = 5 * 24 = 120
    5 C0 v8 B' Z# U) Z4 n
    9 T: o5 }# ?' PAdvantages of Recursion
    " ~( `9 K5 R. f' L4 H. _$ l% Q2 Q0 c1 X  ?: e* 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).
    7 ?6 a! |) J8 d  ]8 B& E% Q) R9 n& g
    2 T: ~+ b5 i+ p% g  h7 N% x' e, f    Readability: Recursive code can be more readable and concise compared to iterative solutions.6 J1 \" k2 Z0 u! t3 F5 w: D

    3 `- }% i$ J7 p% MDisadvantages of Recursion, _7 Z) e3 E7 W
    6 a' Y1 T8 ?7 F! k/ J  u% d
        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.4 a( k8 H" j9 n1 P# ~$ n: N: P

    3 m+ J6 |4 \! G/ S    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).3 t6 V; |, C' ]4 x0 b# z4 P
    9 e# x+ U5 i$ l% O' t- G  l" b
    When to Use Recursion
    9 ^7 c* r4 h# s  X1 |" L, N! q$ z+ E9 i* w1 |* X
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    / j8 U+ y  U  c/ ~* _6 b5 v2 Y
        Problems with a clear base case and recursive case.
      B; |7 U  F7 K& k5 n4 d5 o8 ~. v: I, N5 W; \  O: ~6 |
    Example: Fibonacci Sequence0 s% w; b) X" a. a
    7 A5 p7 p2 \' p9 F! P4 t' I$ W
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:  C: J7 q$ S. q
    % ~, v* ]+ l% b1 X9 h( H
        Base case: fib(0) = 0, fib(1) = 1
    : U8 n# _) |3 k
    7 U8 U' @5 T$ u    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      F* }: B1 s$ \
    ( j5 E' N, Z2 H8 S! o1 `  }9 Rpython
    6 ]0 n4 R: q( g9 N+ l9 t3 U: V  W# l9 z/ o! f6 W0 ~
    " Z5 a/ i; p5 A* N, W+ X' d
    def fibonacci(n):
    1 s; v7 q/ l8 b7 Q/ R) z/ V    # Base cases
    2 {: u' U8 H: }; _$ X    if n == 0:
    2 ~* G# a" Z$ K: U" h& V        return 08 Z8 R! u1 e& T) I
        elif n == 1:
    6 ^* j* Z% p; \% d        return 1
    * V* }, }2 H, U: m( M( c& Q7 @    # Recursive case
    . c$ e4 ~7 ^% \) b    else:
    6 n- s1 d- o  ^0 i& e6 i        return fibonacci(n - 1) + fibonacci(n - 2)
    / P( `3 x5 J$ x2 i/ p7 L
    ( N% q* N! j4 r# H2 m3 G1 `# Example usage
    6 z0 M' N2 n9 ~; w6 G8 v5 xprint(fibonacci(6))  # Output: 8+ i6 E5 _( z2 ^* S7 ^8 L
    . i, [* q' p% \
    Tail Recursion
    / U" e4 u6 d! X& r2 }% h* {) i
    ; n4 I8 K5 K/ |8 F5 u' h# tTail 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).
    8 S* `9 L; N5 j1 o  r' r& D9 I+ m0 K& X; S0 q9 X9 }- _
    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-5-24 08:08 , Processed in 0.045125 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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