设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 K7 R; M; t, Y( t$ I
    $ v: K4 b2 V+ L1 D- |% X3 d; l解释的不错
    $ e2 V' i1 h# T+ P' F8 y  U- N/ u- L
    ( g6 E; B' L: Z' h9 ?2 X5 V  ^( H递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。) @4 r( {  W9 H
    4 o" W- c7 U: f7 f
    关键要素
    ) k% a$ r, i5 [6 h6 Y$ W: |* m1. **基线条件(Base Case)**
    , D) g1 j# H) o, i8 l   - 递归终止的条件,防止无限循环
    2 q8 z7 D& @( X: [" A9 v/ a   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 y6 a3 \. ^, q& y$ [6 q

    * f* x2 e' s; x8 ~8 p2. **递归条件(Recursive Case)**8 t" g; ^* c" v5 D1 ?& D7 {
       - 将原问题分解为更小的子问题3 [* ~; F4 c- k0 H! L: E. P
       - 例如:n! = n × (n-1)!
    1 N6 Q6 |( }- v' m+ I0 @$ v) ?4 ~3 M/ w5 Y! j" u
    经典示例:计算阶乘
    ' ~" a4 B; F! n7 j) V& ~: mpython
    8 b, R! `6 a8 Z/ q1 K. a5 Pdef factorial(n):
    4 ^2 E# x; Y6 A: W- l. Q- g0 b    if n == 0:        # 基线条件: _# q9 J2 r5 {! I" k* ]. t7 q
            return 1
    % L  N9 k2 c+ B! w/ }" u2 e! d    else:             # 递归条件
    ) M2 x5 Q0 a6 G3 Y$ c        return n * factorial(n-1)9 U& f& r  I) y# W- S- ^. D- _/ s
    执行过程(以计算 3! 为例):
    2 E% J, @/ ]2 w1 T5 T4 J$ E6 W7 Wfactorial(3)" ~! }5 Z! n; g# k7 r3 x4 _* w  l8 F8 C
    3 * factorial(2)
    0 k8 i) B: r+ U; {6 d8 M7 z1 v3 * (2 * factorial(1)): Y. r0 N/ f4 q& ?2 Z3 v
    3 * (2 * (1 * factorial(0)))- G) o4 M/ k1 S# i( D
    3 * (2 * (1 * 1)) = 6' X2 \: o# T* e- f
    1 |; P- o# C9 S5 j
    递归思维要点7 U$ f& z8 ~0 ~. l" p  p8 K9 q1 D" N0 M
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑& j7 Z/ ~* @. B9 r4 D+ [
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)1 v  t& W6 j, f, b7 r
    3. **递推过程**:不断向下分解问题(递)
    2 F( @" |; |) v* w3 n+ O5 e4. **回溯过程**:组合子问题结果返回(归)
    * L. U) p9 O* W" ~9 w# E$ Y! U1 v# W. j. `& i" G2 n3 X! ?
    注意事项
    $ \3 }( A( B. f* ]+ \0 D- f3 ^必须要有终止条件
    9 K7 L* `6 D9 }递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & j9 n6 \* V. x' ]; N9 X/ C+ }某些问题用递归更直观(如树遍历),但效率可能不如迭代
    3 X0 o6 K- w0 t9 J尾递归优化可以提升效率(但Python不支持)' O( d* ~( ]8 m$ H# e
    # M4 o3 O: b1 \
    递归 vs 迭代
    ; H, m, J4 m6 g5 \3 ]|          | 递归                          | 迭代               |6 [4 M& m% d3 U
    |----------|-----------------------------|------------------|
    4 J$ z$ u  @, Q| 实现方式    | 函数自调用                        | 循环结构            |
    . n. I: O. c5 _9 g| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 E! \4 X" l4 D9 `9 P% S| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- V& \' G% B8 S* |5 ?
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    & Y7 s1 O$ A* R8 ?: N9 b% k4 ~+ a
    * \5 Z$ c- |3 S4 z/ X% w& w! b" R 经典递归应用场景' ^' M; c* t, n/ A; S9 F/ N1 {
    1. 文件系统遍历(目录树结构)8 h) b/ k# g; k* y) G8 {) [/ T: y
    2. 快速排序/归并排序算法* K# h- p" W1 t; c, k
    3. 汉诺塔问题5 c# D$ T$ u. w, ?' a  y
    4. 二叉树遍历(前序/中序/后序)
    , E) t1 U3 `/ J5. 生成所有可能的组合(回溯算法)3 ]& n% g; D: I8 E; [

    * d1 y: [, P- h+ j7 A3 T试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 08:56
  • 签到天数: 3281 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ; L: p6 k% a2 Z我推理机的核心算法应该是二叉树遍历的变种。& R3 B" F% f; I2 i1 c/ H( Z' ]/ P
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:' d* E# S% C9 I$ o1 ~: v
    Key Idea of Recursion9 k3 K' e1 V5 Q3 e
    2 q6 N; I" [! ]6 ?4 u$ D
    A recursive function solves a problem by:
    / R! L: v+ U$ T9 a, W! M: u  C0 p! z
        Breaking the problem into smaller instances of the same problem.0 ^. E: {" I# S% ^) c

    0 w* m9 O, J- h8 v" H    Solving the smallest instance directly (base case)./ ~- ?3 b! K2 G
    / G3 @3 ~- V7 o
        Combining the results of smaller instances to solve the larger problem.
    5 J9 k! N; s* o/ u$ ?) B
    % z& q, o" Q( q. F1 {! N1 iComponents of a Recursive Function% f6 _4 ~, ~  y
    7 h$ @9 g  w. g4 N8 Z
        Base Case:9 T5 [% U& q- X+ r" d
    5 m$ C* M0 R" d* u8 j0 N
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    + C# ~4 c% a6 J5 o' A; R1 \! c
    2 n, X  P4 E& n: g( N        It acts as the stopping condition to prevent infinite recursion.9 y2 L# ]3 f9 M& d

    " @" n0 Y2 P% y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    / M3 d1 _; G4 k+ \2 p. Y" H/ ?0 P) A8 \: W
        Recursive Case:* |$ @0 e& h/ [8 R- d

    1 x5 X, S. T% ]1 K- m8 W; i        This is where the function calls itself with a smaller or simpler version of the problem.
    ! T$ E3 v& v8 ?: o* ], T9 S# e) R5 p( K0 ^* b9 ^; j" Q
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    + A4 l) M' n& M: u% P4 M8 p, m# U8 w; k( ^8 u- ]6 `1 q; z
    Example: Factorial Calculation" S0 x6 C; {  p, u7 m  K

    : j* H3 _6 i* Q$ M- pThe 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 q' n( u5 }4 M' e/ K/ L/ y" R' p
    5 N2 K$ O% n% q    Base case: 0! = 1, h7 C+ C5 O8 X. E2 L

    5 m* U- |- v$ S& D    Recursive case: n! = n * (n-1)!
    6 S5 L' A. W  H4 E. S
    . [6 e6 p* B" b1 hHere’s how it looks in code (Python):
    3 K( F& q# a6 w( S3 _python! j2 E5 Y/ y4 C1 b8 @) y7 V) X! j& e6 _

    / ~! `, X2 `; \/ P% ]* A# M
    ) }$ ?  J1 B8 k+ S' D& y! G9 ~def factorial(n):  z7 o- i3 d# A" d- \
        # Base case
    ) Y0 A5 d. g- j8 X8 r1 E# D* A% ^9 k    if n == 0:
    . z0 A. f& \% z  z        return 1( y1 r5 _6 n1 w1 p/ h
        # Recursive case( T6 S  @9 t9 N* h
        else:
    ; C/ q" L; r9 B2 ~: S        return n * factorial(n - 1)
      F; q* E+ M4 t' d" h: g  l8 x6 b$ _2 q2 b2 `  q  c6 x
    # Example usage2 z4 P/ w- w+ g7 C) W
    print(factorial(5))  # Output: 120& i2 c" t5 e# z' c. y- a
    9 q+ d3 L1 N8 a3 _) F/ i/ `
    How Recursion Works; ?; q$ u. ?0 O( P' b: j

    5 f$ W* z1 t7 o4 W    The function keeps calling itself with smaller inputs until it reaches the base case.$ M; u  n" D/ Y7 T: e; u" z

    $ B4 V3 B% K! P8 K  b2 Q( F    Once the base case is reached, the function starts returning values back up the call stack.) f* n6 q: p: _$ q  c) V* }% [

    % u6 z+ z  I7 I0 ^% \# b5 x6 q    These returned values are combined to produce the final result.- Q. D  O0 O6 P8 h" R
    7 p( C! C# R! W) T( R7 Q
    For factorial(5):7 d; N. F7 @9 r# H3 @2 W* ?/ l" v

    6 f; D& ?: p$ C. E& G6 E2 }0 g( O$ D
    : ]! E- ]# R& F+ Sfactorial(5) = 5 * factorial(4)$ \0 e7 W' e6 {- M) m. U
    factorial(4) = 4 * factorial(3)  O* C4 ~% F* \- ]- N
    factorial(3) = 3 * factorial(2)
    ) `& z/ |+ Z5 d5 h/ G7 o* ~( |factorial(2) = 2 * factorial(1)" M6 E8 S5 j$ s* c: S% {
    factorial(1) = 1 * factorial(0)
    : l4 X) A- ^* m2 w+ Ifactorial(0) = 1  # Base case
    + u$ l  B! O( n0 }) E4 X6 X, G6 }  I' d6 W# F
    Then, the results are combined:; t4 `; D" l& _  ]; M) j! w/ ~

    : ^. l) F5 G5 B' Y. Q! Z8 G
    9 m2 w3 \( G& g+ ]factorial(1) = 1 * 1 = 1
    * r1 [- w1 P6 n7 T% |2 N5 Vfactorial(2) = 2 * 1 = 2
    6 q  z, v  @8 t  {/ Q! u$ E- Rfactorial(3) = 3 * 2 = 60 O) p2 a% \4 q% l/ ~. Z# b4 F
    factorial(4) = 4 * 6 = 24, M" L/ B/ Q9 k) v- `, s; g
    factorial(5) = 5 * 24 = 120
    8 I3 T3 o. s5 M6 x8 Z5 z0 _/ m4 V( |' f4 k: i1 X5 a6 a4 b
    Advantages of Recursion2 x2 |% j' \) s& D( k
    9 j/ Z" G. L. v# U3 {" p/ U# d
        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).# x6 {, R8 w1 o: O. |
    7 f$ e0 Y; w3 h$ y
        Readability: Recursive code can be more readable and concise compared to iterative solutions., }, t+ \! v- Z  a, S2 C3 A- A

    % h4 R: v, ]- [( l6 O' g: DDisadvantages of Recursion
    4 ^# B7 z) h7 Z8 H; v; Q" m- L! {+ I7 s, U
        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.( D5 b1 A4 a6 \& k" T9 o
    ' F8 Y5 d+ I" y9 l
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    0 d- Z( I" d3 q8 w6 ~  E! y1 [& F. ]
    / t9 j/ ^6 e3 {- A2 ^: oWhen to Use Recursion
    ( U- t+ Q! ^3 E  k* M1 z9 i  Q2 ^/ l( p* Z
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " |) G+ J% L( W) M  [
    9 v7 b' f( u( g2 B+ _# d    Problems with a clear base case and recursive case.
    / D* m& s; c' N! x- s' ^4 w0 H( D) r
    Example: Fibonacci Sequence
    . Q- q# e" @% @& o" y8 ~9 j! h" _. o) z( i
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. n0 B- W; g+ q) C8 G3 H- [9 b& l
    1 x0 }; p( A0 N5 T  d6 y* k+ @
        Base case: fib(0) = 0, fib(1) = 1
    # y2 l% z- h5 D8 k1 y% G2 P$ M
    4 i- ?0 M. J/ }3 }; s6 a" h& K. q    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    - j* L: `3 E5 U4 Z
    # H* f1 [0 }( A/ [2 P: }) Xpython$ h7 `. [# \7 m2 F0 w! ]
    6 |: T( G/ c1 \" W8 I: x# [7 H+ U

    # b( J! K) a1 A1 s- }def fibonacci(n):7 ^' w: I# V" I. G, l; E, W
        # Base cases
      @: _0 f# @1 u# H1 {4 y    if n == 0:
    ' W+ ]9 g* K& E: O+ U3 I7 P        return 05 V/ S5 P$ p) e1 C' B
        elif n == 1:" T; l; q/ `4 u- U
            return 1
    , o+ S3 s  J; `5 d: }5 K  P/ a- S& N  r    # Recursive case( m. S- _3 Z$ Z( ?$ G
        else:
    ' ~; v7 Q* J: K1 l2 w% ^! Q        return fibonacci(n - 1) + fibonacci(n - 2)6 w9 z  ]7 n, ^7 E% H3 U

      B# J# A9 [4 R  n' L6 E( |4 p1 z# Example usage# q5 d! R6 C3 T0 c2 ^) c; X
    print(fibonacci(6))  # Output: 8. T- y( o% C( l
    : c. Z- d" l+ k. D% G. D; D
    Tail Recursion
    2 u0 k. K1 U& `* `  j6 m7 c. K9 c8 k" S2 x/ k; i2 _% n
    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).
      U" x6 w4 s0 ~: I9 @1 s- f. z. C- H/ r5 s& m: ]
    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-6-28 02:39 , Processed in 0.059084 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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