设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ' e' V* W; Z; s# _, V0 F$ w2 s3 c1 n) r/ D3 B' U3 ~7 F3 }$ x, J6 X
    解释的不错
    5 F2 N9 d5 ~7 m
    ; [( z+ |- a1 O/ x, c, j7 t* M递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' n' c( k6 z* F% R* I

    $ ~& l/ D/ A4 F 关键要素7 Q, X4 }4 `. g; x) ]
    1. **基线条件(Base Case)**
    ; V' @$ x+ g# j1 I. d$ k& q4 H. J   - 递归终止的条件,防止无限循环
    6 v* D% A& E% u' g   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 17 q* J! u: t. v' B5 w
    2 ~7 l. m$ ^* c1 X7 C
    2. **递归条件(Recursive Case)**& D% \) _, S. i9 S  r' b$ j4 q, n
       - 将原问题分解为更小的子问题4 N  u  J7 n2 w! K9 s% x
       - 例如:n! = n × (n-1)!
    7 `& S1 L, v9 f9 S
    ! O8 l) U4 c8 E6 s 经典示例:计算阶乘
    * w( E+ B- y6 I7 gpython. ^# k3 Z9 r$ t8 E1 E& `, _" ]
    def factorial(n):
    & P" v+ n( x  y9 r3 _    if n == 0:        # 基线条件5 A4 l3 F  N3 L
            return 1
    ; [; ?% I% A0 I, g5 a6 p5 |- L    else:             # 递归条件6 s9 M9 Y. N' d' c+ |8 U3 E
            return n * factorial(n-1)
    ! l& }$ A" |( q9 G- v/ `& f2 q执行过程(以计算 3! 为例):
    6 E- f3 C* q% C. x. m2 j1 nfactorial(3)1 P$ m: t# m& P4 P% S8 X- v. J
    3 * factorial(2)' M* r$ Z! I1 T3 Y; Z
    3 * (2 * factorial(1))3 ?) b3 S1 ?% e9 f
    3 * (2 * (1 * factorial(0)))
    4 z( k0 b2 o" N+ |# ~5 U& c0 Q* v, g* a3 * (2 * (1 * 1)) = 6
    . t; Z' T/ o, J) S% p, ^8 }& X1 U( v9 i. \/ @; I  p
    递归思维要点* e0 g3 M# r3 k6 ~: ?
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 X, u# x; B  c5 [2 w$ u2. **栈结构**:每次调用都会创建新的栈帧(内存空间)0 r/ i8 Y" d5 @8 m3 q# O4 c
    3. **递推过程**:不断向下分解问题(递)
    % U! R  P" K, ^& e4 S4. **回溯过程**:组合子问题结果返回(归)8 T  H* S1 \; ~3 j- |7 @7 A& P
    * g7 {. D9 C7 c, T5 E
    注意事项
    + n* R, K% }: R* B! ~必须要有终止条件
    ! b: O5 I2 z/ L9 ^" P& C递归深度过大可能导致栈溢出(Python默认递归深度约1000层)( T( i4 J: Q8 i: I. n
    某些问题用递归更直观(如树遍历),但效率可能不如迭代6 O* Z( A# ]& h3 G
    尾递归优化可以提升效率(但Python不支持)
    & s6 l9 `- s/ _9 c, g
    9 e" s+ E: W. u 递归 vs 迭代
    6 e; ]  B6 X7 l8 P|          | 递归                          | 迭代               |
    $ E; C2 o& K& Q. e|----------|-----------------------------|------------------|
    " `- g& Q! B' W( K| 实现方式    | 函数自调用                        | 循环结构            |
    , M: {) |9 K! G2 w| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    + O, M: t  n2 h1 {" _| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    8 q4 {( n8 o4 U5 R# [+ l. || 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |6 R: D3 [0 I% w' |5 Y

    2 n6 W, y5 y2 ~5 k& Y( t 经典递归应用场景5 m: f6 Y1 N1 R- x5 C7 s" l2 h& E
    1. 文件系统遍历(目录树结构)
    - _# Q- [( C7 W5 ]; Y* p4 x2. 快速排序/归并排序算法
    $ m6 m; L* [  S3. 汉诺塔问题- b6 m$ w% |( v8 c( m. b) a* H
    4. 二叉树遍历(前序/中序/后序)
    4 q5 l' W5 _! M! |' w5 p9 V5. 生成所有可能的组合(回溯算法)
      ^8 F- v5 n6 z1 ?( J: h0 V, E9 R$ R" _6 M
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( H- g9 Q, T  }- h我推理机的核心算法应该是二叉树遍历的变种。! y: p  C8 L; {3 C2 d$ A- j
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    2 h" c/ O; n0 h3 @' ^Key Idea of Recursion$ j; Y4 _4 L" S& v& V! h

    ) C( x( S2 J( \/ S1 t1 ~& \2 [& g: LA recursive function solves a problem by:
    3 Q, `/ O" t3 ]& A& I- ]% H
    0 Z' [. S: r1 U    Breaking the problem into smaller instances of the same problem.: t7 @" ?9 s# {1 f/ Z7 l# x2 Q) z

    ) j, F1 I# h0 g% }& a' r% i% r# ~    Solving the smallest instance directly (base case).: M) A5 U$ ]! \7 ?: y
    % s4 ^6 f1 P$ ?, z1 L" O6 H
        Combining the results of smaller instances to solve the larger problem.
    1 b0 n/ V6 x: ~8 o$ n6 @
    * C! M5 W& b+ d2 w/ Z- ZComponents of a Recursive Function$ b4 L7 N+ B1 ]% h0 k! V
    7 ~% X( E& `  m) f" U; C8 I
        Base Case:8 _8 ~2 ?+ I0 r  K& [

    0 M& I& u) }) I; Y( R* p        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 |; [; o/ O5 Q# N; v* }5 ?/ s" R: ]) K8 \  a4 @
            It acts as the stopping condition to prevent infinite recursion.* @" c4 k, F" y  E- ^, k+ n

    2 |; p% v( z) t( u$ L; Q        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    & r$ ]$ g( a3 O
    5 B) G7 x. s$ L5 [5 V% I    Recursive Case:8 n3 L1 x) z* D5 F8 m" F2 ?

    - m+ m$ \4 x3 B. L% B7 X! i        This is where the function calls itself with a smaller or simpler version of the problem.6 ~  A  R! P, v
    % I: _& I% B9 `
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 _# g, U4 i9 {) c1 ^- h
    , Z4 Z* x0 r& B1 O: o- z8 z; ]
    Example: Factorial Calculation) O& ?5 N# a9 k) Y

    $ p/ U6 z* A* z. s! L7 d% L; \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:: C8 S/ J. s  c6 B9 U
    2 O4 e0 w8 S1 G. o  P, ]
        Base case: 0! = 1, H$ v9 T4 C# d6 d
    ! f- e0 f7 {% Z" u
        Recursive case: n! = n * (n-1)!7 i7 M( W( u6 L8 ]8 u8 w6 H( `! _

    + J, T/ i7 Y/ e: ZHere’s how it looks in code (Python):
    % y7 ?9 m+ T% g  A; s, R2 O( Kpython+ R4 E9 ^7 t+ S& n! @( W. J
    ! f3 r# x, B% ^- K( ^) g4 m' f. V4 G
    9 L/ h  J# W, @& R/ |
    def factorial(n):' A7 D/ B5 ^0 w8 S' S; _
        # Base case# v( f1 B4 l3 D: A. {* U, j
        if n == 0:
    ) Y5 h' i0 _9 T6 u0 n1 ~" ~        return 1
    4 T) a/ q, A- `+ k  D# O" l* K    # Recursive case
    - Y8 x2 x% U, C9 d    else:* L8 o; S. s3 C+ x) ?8 _
            return n * factorial(n - 1)
    $ E- ]' m: O6 N8 B  C7 Z8 I6 u; f6 [) @( }0 {/ w
    # Example usage
    , ?4 [9 V9 e: s. o; Vprint(factorial(5))  # Output: 120# o1 C9 _: j, L2 I" G0 v2 Z' U$ J

    ( d6 A0 ]1 p+ K! ^9 p- QHow Recursion Works1 N/ W) h% c, g" z$ `( u: W

    1 Z5 y8 V0 ~' m- e3 @! T* l, Z    The function keeps calling itself with smaller inputs until it reaches the base case.& \4 q2 f/ t$ i( Y8 Z0 e

    2 Z- A4 r  G1 N- k7 H4 ]) S    Once the base case is reached, the function starts returning values back up the call stack.1 T5 C6 X7 q% ^

    $ x+ v$ r  D& L: G; m    These returned values are combined to produce the final result.
    9 T# m2 O+ f: f8 E
    2 X7 j2 C" K0 c4 p/ \: e; e4 lFor factorial(5):
    4 h8 q0 K4 t* [3 k& o1 e" s' f0 u6 y( E/ ~

    9 B' @5 C5 H# _! Bfactorial(5) = 5 * factorial(4)! r7 L8 M5 n" s
    factorial(4) = 4 * factorial(3)! ~. J$ Y, S) h$ x& t
    factorial(3) = 3 * factorial(2)  f( |+ Z' G  {+ T
    factorial(2) = 2 * factorial(1)( n0 S" O; _5 d( m0 O8 T
    factorial(1) = 1 * factorial(0)
    5 t0 [$ h* O( f& U3 w5 X  Lfactorial(0) = 1  # Base case
    $ G9 i7 N- o- B6 c
    : x. P$ o2 j! [4 W" {Then, the results are combined:
    : Z! `1 T" f% L: W. H0 B8 T- g& F6 f3 [, {: {: J; h

    ) m3 m0 c- M+ V/ L9 ?; M  jfactorial(1) = 1 * 1 = 1
    5 O7 ?( i" R& G; c" ?# Hfactorial(2) = 2 * 1 = 2
    " x$ P/ a& F) I' M6 ^. k$ Pfactorial(3) = 3 * 2 = 6
    2 I- z6 O0 ?9 a9 x0 ufactorial(4) = 4 * 6 = 24' n" a2 N$ `' K3 L. p& q- z  |
    factorial(5) = 5 * 24 = 120" t' A/ N% h' `  H$ x5 w5 P& C
    5 t2 @- Y# G( u" D
    Advantages of Recursion& j- K0 Y8 \- E4 s) {  t
    3 q, a9 }2 M" G8 X
        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)." ^# ~. X( e4 l4 t: T$ S1 ?% V6 f
    " b% a* ^& r5 U5 Q$ X
        Readability: Recursive code can be more readable and concise compared to iterative solutions.8 S% z5 v; O: T. B7 N" `; t
    8 T' |- x; t1 F
    Disadvantages of Recursion
    / Z, D+ T; `4 W7 _, }
      b2 d# ?9 n9 V* p/ x. Z    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.$ d8 D2 }; E. l2 _+ T2 ~$ j( y
    5 `- h2 m) H& A$ L" u5 h2 P' B3 _
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: n& d. v/ ?7 g8 S3 _- K" a' T
    ( N- K+ Q" f) P4 P
    When to Use Recursion7 A9 `) r; d8 S7 D

    ) r9 I: v$ w% [9 I    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).; d: U  I( v% }# E, I2 q" O$ {

    8 w: D' b4 W1 }3 j- X    Problems with a clear base case and recursive case.% A0 f! Y6 x' W* t/ P) l
    6 y9 i3 u$ G# w6 l6 a* g& G
    Example: Fibonacci Sequence
    ; K' I! C6 D$ K; W. a- H* ?0 ]! y& C7 c8 ~2 \1 n7 `  k* {9 N! [
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    0 J( Q2 j) \* H+ f8 g( V7 B! y$ V% k- a
        Base case: fib(0) = 0, fib(1) = 1) Q  W/ \1 F9 A! Y: T% o

    " x! v( }0 K7 {/ E2 p) S    Recursive case: fib(n) = fib(n-1) + fib(n-2). |( c3 D3 R- M) o* m* `% O

    2 a( H  f( l5 v; Kpython
    1 x$ h0 g5 Z6 e! L! s4 d1 y; ~$ c' p+ L. v' ^4 ~8 C0 I

    % }  S+ e: h, @& hdef fibonacci(n):
    ( i6 x9 |1 o: L6 {: N+ [( M8 Y    # Base cases
    + I6 Y4 [% T( ]  J/ \1 C8 ^* \( q    if n == 0:. ?5 @2 H7 o* s
            return 0
    6 Q& k2 Q/ J0 ~0 M    elif n == 1:* D/ Z9 I3 }, v- ?; F% V
            return 1) Y, f8 T1 _9 }1 a% ?, ^& P# ~3 {( h
        # Recursive case
    1 E: @" U# n/ x+ D    else:' \0 V' v- P* M  F6 m
            return fibonacci(n - 1) + fibonacci(n - 2)
    . ]1 ^# z; s' g) d6 S% W5 G% B3 r: f, o; g+ o2 M
    # Example usage
    / d6 S& {8 @% u  H3 hprint(fibonacci(6))  # Output: 8
    ( ]. o/ }1 O3 r/ e2 @3 `. M! T+ E1 z; h% e. @
    Tail Recursion. {+ {4 N0 d- c' P/ O2 d9 i

    : c7 `' w0 e( L1 [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).$ h1 Q6 e' R% B& L9 [
    ; O4 j( N" S* S' h2 l
    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-8-24 01:27 , Processed in 0.044255 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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