设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ; N* N* D2 ~: `- i1 {8 Y1 ~* u! N  x6 W
    " V- j3 l& j- ~; D( [
    解释的不错
    2 y' Q0 a) ^& N% D8 K2 p' [1 x# t* [9 }, a, s* G
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。8 D4 \: @- ]$ a' _/ b
    2 B4 |. P7 H8 e- z- I
    关键要素
    9 t( m- \: Q8 G5 K) R# S1. **基线条件(Base Case)**% E3 n# [# O3 a
       - 递归终止的条件,防止无限循环" t  r/ d, O: l9 t& S7 q) l! {( r: E
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    0 }2 Z8 n& V9 s# x( s5 z5 N/ I2 N/ n9 b. V
    2. **递归条件(Recursive Case)**
    4 v- V6 m9 S- X. U) s" L   - 将原问题分解为更小的子问题
    5 }# l+ U& N$ H1 `9 x. j5 |   - 例如:n! = n × (n-1)!
      S! C8 G3 M& w  z
    8 s! E) A$ X& H6 S( C) i6 X 经典示例:计算阶乘
    % X$ c: o: @; r* G( \" _python  @, f  f  q) ?; M/ q% i  R6 c
    def factorial(n):' P$ u' Z2 N- ^& \( @- N
        if n == 0:        # 基线条件! ?: M( P0 M" ?' H0 z7 C8 m
            return 18 f7 v/ {5 C  |- S) f) d7 g6 t
        else:             # 递归条件
    ! d5 ~: w9 S, B        return n * factorial(n-1)
    4 o. r3 J! j) g执行过程(以计算 3! 为例):& B; X. d, c# |4 |' L
    factorial(3)
    # ^# @6 d+ k) W; r' `3 * factorial(2)
    * v  A2 c$ G7 T% Z3 * (2 * factorial(1))# j/ I! ~' _* o; w& h
    3 * (2 * (1 * factorial(0)))
    0 }9 N& g3 S8 v7 f& m3 * (2 * (1 * 1)) = 6
    $ W' h- o: k, b5 A/ u& U1 m
    * s0 _) I( t/ }, D( K( s 递归思维要点
    / a+ h& h# R3 Y' h1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    " A5 w& c! E/ N9 C/ B$ f4 y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 o) n( R; C/ o( L- [  Z" r3. **递推过程**:不断向下分解问题(递)
    $ v( I7 |5 `8 L& K5 H) C- H/ ~: D4. **回溯过程**:组合子问题结果返回(归)
    . y# R9 N9 z( i2 q# v* m
    , ~7 o5 i# n$ `1 R5 E 注意事项+ w) W8 L. e0 o/ M$ @
    必须要有终止条件
    ! ^4 `! `  E0 u; S+ \递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! ^5 v: i4 _" w# a某些问题用递归更直观(如树遍历),但效率可能不如迭代
    5 N* _+ X7 I/ y5 r尾递归优化可以提升效率(但Python不支持)
    7 K1 u. I, G# |- t
    8 E: ]1 V) Y1 v 递归 vs 迭代4 X5 l+ k% C- j$ }5 }+ H
    |          | 递归                          | 迭代               |
    9 {( s' O" S0 C  i" e2 i- v3 D|----------|-----------------------------|------------------|
    ; ^4 y7 X( [& S| 实现方式    | 函数自调用                        | 循环结构            |* ?) \5 p: O, O$ ?+ `
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! J+ j) g9 [5 D+ ?/ o| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |+ p- L6 g( D4 q( I  Z% E& S
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 G  W* g7 ?8 ?+ p
    0 A  y: S" ]' }1 z 经典递归应用场景* {& c; @; o! I
    1. 文件系统遍历(目录树结构)) A+ b; }6 o+ Q* i* Q
    2. 快速排序/归并排序算法$ a7 t$ e: M" d% Q
    3. 汉诺塔问题* y# y0 e( r+ N9 {# y  U
    4. 二叉树遍历(前序/中序/后序)+ H' m7 h, E! {: {
    5. 生成所有可能的组合(回溯算法)
    0 M) Z+ r- V" z- D: d6 @% K8 j
    8 F$ A/ k! I: i: c试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    15 小时前
  • 签到天数: 2977 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 H% i1 x6 H& P9 O1 _. t; C
    我推理机的核心算法应该是二叉树遍历的变种。3 i% `) A! M) E0 v1 b- c1 c  ?
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    5 S8 H2 s! s  f/ [Key Idea of Recursion
    1 V8 r; J0 h: N% h& ?7 q. r( D# [- e: W7 e  G- k3 r
    A recursive function solves a problem by:- Y& j. R: f9 f1 K6 `

    - Z! ?6 X- U& |: {% h8 a9 R* E    Breaking the problem into smaller instances of the same problem.7 m3 b1 q0 s  @0 `8 q
    ; q3 e2 u, {8 Q6 \4 R4 T
        Solving the smallest instance directly (base case).
    - }- V, p9 \4 `! B( r; ^4 ^3 a9 z" F5 S9 _) C3 ~  G% z$ Z
        Combining the results of smaller instances to solve the larger problem.; |8 w7 u" O! s2 y- F" V

      v5 d* c- _' q3 f# D. w$ v( vComponents of a Recursive Function- A7 S  l" l. y3 G3 N$ l2 j

    % `& _" c/ t+ n8 r7 c    Base Case:
    ( l2 ?& g: O' w/ X  O. U/ {% P, k- w, s! g: f" G& @% @
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & A2 I. y! X7 o8 c
    + i7 F) D' u; i        It acts as the stopping condition to prevent infinite recursion.
    ! r6 @& M* d, i( u" \
    % \- j  ?- \2 z; h4 d        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 {0 J8 K) U3 U  x7 Q3 R
    * k/ a6 \4 @5 T0 V9 n1 z! `3 l" x8 n    Recursive Case:
    & D  ^6 ^7 S, z+ v3 V( m1 B1 `8 [2 ~" @
            This is where the function calls itself with a smaller or simpler version of the problem.
    . F" s* D% H2 v: O; M" c4 }: M0 m& O7 z: q+ r- }1 P8 E: P
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).$ [2 G9 E* S/ @' N/ A) V

    6 @' X. L* D9 A( s! h0 {Example: Factorial Calculation# [, z9 e4 i; E1 g! ]7 b* [' _
    & T, a' f: v( M; b; c4 ]* q6 b: E
    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:" k3 j% N5 ]1 }* P/ D5 Z- c/ T( u
    ) V3 W9 N4 q  y$ x+ K! C" A
        Base case: 0! = 1. F) i, u8 P! \3 y
    * V, r0 h: W4 |5 f( }3 `* g
        Recursive case: n! = n * (n-1)!
    2 ~& U  K( g; I# v$ N+ e, E" w
    % \) _# _. \* A- _% N$ p: IHere’s how it looks in code (Python):7 E; Q& \% p. I& s' c% p
    python
    ' j6 y/ E8 @# j7 j4 E! u* [/ n* I7 h# _/ ^( z
    / \( |/ h" [( n1 Q
    def factorial(n):4 t8 ~- B, z  s
        # Base case
    & ?8 W3 J8 o5 Z. L! A5 v    if n == 0:1 d( j0 k6 k2 [0 g+ w* G
            return 13 G2 r, N8 A" [# t: f" S$ w& e
        # Recursive case5 G. Y* X& N: G- l4 Y3 g* N9 }
        else:- V/ e4 l5 g  `" |0 }
            return n * factorial(n - 1)
    0 }7 `# n; [8 d, |( L% v2 Z. V; i, g. P( O+ T: C, u/ q
    # Example usage
    ! |% C, b/ }* y+ [! Z: t( cprint(factorial(5))  # Output: 120- S; ?! v3 l2 P2 R! l

    8 c! W( d) c1 i2 U* EHow Recursion Works/ D: u  v6 t" ]; Q3 _  j4 X

    ' M0 I& R& D+ O; q$ {( o% Y4 ~    The function keeps calling itself with smaller inputs until it reaches the base case.& V9 K9 }6 }  z
    0 Z# i$ F" _9 z5 H7 j; R
        Once the base case is reached, the function starts returning values back up the call stack.! G! @+ q0 ^8 w  q/ @# Q
    ' v3 X# P) h0 }" n) Z5 K8 D
        These returned values are combined to produce the final result.* z% W  j9 h" C, q$ f+ [7 c
    ) }& C4 s; d. L. o) Y4 l0 n
    For factorial(5):
    6 T' [' s/ Q" p4 ?9 R4 k
    0 C. m# w, E% D8 T  |0 C5 P5 l
    ' s7 Y) l: P0 t! k; ]factorial(5) = 5 * factorial(4)
    8 k+ }$ d7 h# W. T3 `( U3 Sfactorial(4) = 4 * factorial(3)( S3 E. i& f. R5 v$ P6 g: D
    factorial(3) = 3 * factorial(2)
    & Q& ]8 b. M* vfactorial(2) = 2 * factorial(1)
    ! b' i* u; Q# P) M4 H" Tfactorial(1) = 1 * factorial(0)
    " G2 j" R6 C$ E( `factorial(0) = 1  # Base case, V' \5 C" K% f. y" @2 P7 ~

    5 c" i- o, S9 ~  ^* t9 ]Then, the results are combined:, B3 c; s9 r  i& ?; q; Z% J

    ' W4 `6 z; c9 B1 l% c, u: w
    " \! o' i0 k% @& U/ v& Ifactorial(1) = 1 * 1 = 1$ m2 v7 q6 h5 J/ Q* D) V/ F" V
    factorial(2) = 2 * 1 = 27 x1 g! G- y# ^3 @& ?  X* D' R
    factorial(3) = 3 * 2 = 6; z* K8 e. U( \1 i
    factorial(4) = 4 * 6 = 24
    5 K7 o* c5 R; Tfactorial(5) = 5 * 24 = 120& b8 ^" ^. ?; K% q' B
    / k2 l7 [: ]5 m9 t* C1 N3 _( \( @
    Advantages of Recursion
    ! r" q, \/ `  L- a& E; h2 b2 R1 x* o
        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).
    " H6 W9 |2 c0 g. ~. h3 E( |& [5 h' H* e. L. Z
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ _3 \9 V5 U6 ~: s" ^$ V4 X
    ! G: j4 d& T% i* N4 t: i
    Disadvantages of Recursion
    8 z4 m+ z* h5 G% b6 Z* ^& I, K( r; Y: S9 k6 b" }& i
        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.
    ' _6 l, T1 I7 W0 ?# T) C8 I( b% Q8 ]
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    7 b7 f: b% H/ E  m" h8 V5 y( N  C7 I. Q! O9 m. l) c
    When to Use Recursion
    : k# {4 G$ L! e0 ~5 h6 n2 e: D7 P) @4 k& s; N
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    / t2 o- Y0 k  z" k3 F+ \4 g
    * S2 r; s: T0 |; o! l    Problems with a clear base case and recursive case.  @1 `) F4 n; K& c7 P! B. W
    ' v$ b  v1 e, `" J
    Example: Fibonacci Sequence
    * M! t, C+ Q# b- }  u' C& U( G% g  A' Q8 H3 g5 e. c/ B4 V
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    7 i' j  r* L0 p3 N9 F) v  s1 v5 Y$ K" s
        Base case: fib(0) = 0, fib(1) = 1
    ( F2 x' E. C& H# H3 F
    " }& F: ]1 r" y2 k0 |5 y% {    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    6 r8 c8 ?' Y" q' C4 S3 i; l. g2 ]) M
    python' L2 u# R- C8 \! ~$ G+ T4 `! s

    ! Q. E, c1 m2 n6 ?- n1 s4 S) L
    4 k* s" b+ J# \, q9 t, w! Tdef fibonacci(n):
    8 V7 o: o4 C% c' e6 J# ]1 Y" I7 {    # Base cases
    + S+ r1 m: q7 W) W( X- J4 F  ~: k    if n == 0:+ w! z( F. \2 v0 i
            return 0, K% m: o1 C; L" U" g- `8 l* C& t
        elif n == 1:
    # |: H: C. S+ R        return 1
    0 `6 O+ ]1 U0 t7 K' a    # Recursive case4 U) a% @2 ?' e, w; T9 U$ U" a
        else:
    * Z3 |" C# L4 ^6 V+ n1 Y        return fibonacci(n - 1) + fibonacci(n - 2)
    + {$ ?8 t( B# I3 J( Q
    9 J( A8 G) R5 E$ p# Example usage
    * F' n: f  h0 c. ]# mprint(fibonacci(6))  # Output: 8$ b+ T+ m) y# D$ h0 G0 W. t# |% W
    " w6 G9 G. E7 e; i4 C" M8 z
    Tail Recursion
    ! E0 s: Z& e1 \. r1 V, B* @
    + B' [: d0 J, p& Y  j, r; g7 k8 m: P% 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).
    ) a, Z7 \/ V8 y* e/ b
    5 i& a6 S  E& j9 z9 Z0 {4 TIn 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-7-12 16:05 , Processed in 0.038356 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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