设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 1 ~, [! B0 }, u

    ( e/ j- ?. I8 p3 Y( {1 C解释的不错
    + C8 V% g. F& M( J4 a- G# r* P
    8 A" Q; ?: M4 u1 V) U$ H( C递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ V6 V1 P$ x, ^
    ! q; F! L. d4 L* ~+ M3 ], o
    关键要素
    1 a( `5 k) y9 X3 Y! y1. **基线条件(Base Case)**3 U  y9 z2 F( b3 b
       - 递归终止的条件,防止无限循环
    1 c8 j% `" f8 e( k( O   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    * G; n6 ]6 d- R8 M3 o6 Z5 l1 e% I- `
    2. **递归条件(Recursive Case)**
    - `5 U  o6 [( `, q( b" _7 T1 M  S   - 将原问题分解为更小的子问题& m# [9 h& x3 H; t, c+ F) b
       - 例如:n! = n × (n-1)!
    0 x; ~" G$ p& `, Z  e6 w3 G$ a% [0 W! a! X. M
    经典示例:计算阶乘
    , v9 A! _2 z+ P" I6 wpython9 m7 d2 J' H% t3 O
    def factorial(n):
    / B, U6 J+ j0 E. \    if n == 0:        # 基线条件' N( Q$ O; i! K
            return 1
    . ~$ Y6 e! }4 D9 \0 T( y* {    else:             # 递归条件
    " I) j* `# b! E1 L, N        return n * factorial(n-1)
    # o/ U2 M9 l9 e# M8 e执行过程(以计算 3! 为例):8 F, R; |4 }7 f
    factorial(3)
    ( _4 g' U4 P$ \3 ]' [1 {, c3 * factorial(2)
    : J& ?9 C! I4 ]. h) b' f/ C/ o% r3 * (2 * factorial(1))
    8 t2 t  M' y- }. A$ r! T0 X# R) j3 * (2 * (1 * factorial(0)))7 a8 z8 s& V8 @0 `
    3 * (2 * (1 * 1)) = 6
    4 g1 c( u+ I5 x: X
    & Z7 J1 _* F1 l8 F 递归思维要点+ O5 P$ c' W9 {9 Y0 j
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & _" X0 u# J# X' l: h8 H2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - z" q( x8 ]! ]  r3. **递推过程**:不断向下分解问题(递)
    % d+ O  N% u2 C4 ~+ [/ W7 i7 p4. **回溯过程**:组合子问题结果返回(归)+ Y6 j5 S* P& @7 h, M4 T

    ' z4 n3 v7 t; X! r4 A0 N 注意事项5 @7 Q' d% Q2 g2 L
    必须要有终止条件
    9 G( E# N' |! M) K8 v' h) S1 }递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! k  F1 q' h) d6 ~7 b9 Y& y某些问题用递归更直观(如树遍历),但效率可能不如迭代8 E- A8 C  ?$ D
    尾递归优化可以提升效率(但Python不支持)
    # c/ b! v; Q, b1 A3 y( d1 `7 x
    7 x" r1 M9 r; ` 递归 vs 迭代
    & A! t( }8 J" ]8 j7 `+ C2 F5 R|          | 递归                          | 迭代               |
    / l" {4 y) M$ d# g5 W7 V|----------|-----------------------------|------------------|
    2 r/ e' Z3 E% C$ u' o| 实现方式    | 函数自调用                        | 循环结构            |
    0 I7 J: ~0 t: m% X! E5 f| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    3 P/ r5 `. l' |7 f| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ' N% L: K! M+ J$ Q9 q; L| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    , s. i, I/ X" i# t; E* ]4 x9 ?. n) }
    经典递归应用场景
      e& G1 G  r/ t1. 文件系统遍历(目录树结构)- x! D/ @, x! H: [/ T
    2. 快速排序/归并排序算法
    1 ~7 t6 ~, ?4 ]9 ]0 k3. 汉诺塔问题
    ' }# X, P' j6 r4. 二叉树遍历(前序/中序/后序)
    ; e/ e# y; C- k' y5. 生成所有可能的组合(回溯算法)8 w  k. P" z! X8 Q/ d

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

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    5 天前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 w5 c! |4 d1 |& w- g4 G" |
    我推理机的核心算法应该是二叉树遍历的变种。& p3 z7 `9 ?# f; W
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:# G2 P- X0 `8 k+ V# ?. l
    Key Idea of Recursion, i# V' n; u5 W9 m0 K1 `
    8 o2 e7 N5 Q8 |' D5 B
    A recursive function solves a problem by:- `' h( x5 ~& M2 |- |
    9 l! e2 K* D: N0 b6 Z' o% s* ^
        Breaking the problem into smaller instances of the same problem.
    & i  |. }' p. Z! Q" C! V! n; u" c3 P
        Solving the smallest instance directly (base case).
    # o" F: r! `" `. V, e6 B; l# l1 i7 h) d6 }
        Combining the results of smaller instances to solve the larger problem.1 _4 o6 k1 i1 h+ I$ N/ V3 y) P) {
    ) |( k( O: m" \8 r' J* g
    Components of a Recursive Function! r9 M- W9 q( |

    & D8 w4 ^5 v8 M: t& n( d1 o" m    Base Case:9 O. S4 a4 W) H  N$ o/ v

    # H: `# w% E: q; ]        This is the simplest, smallest instance of the problem that can be solved directly without further recursion., T/ a5 @: Y6 f/ V& S5 n+ r/ h" F
    / v5 b, z# \  k- N6 `) y/ y
            It acts as the stopping condition to prevent infinite recursion.
    1 V! b8 {5 E, D: y! E
    / y7 X. Z. n# i6 G. G        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # Y2 j$ n' r4 }/ F+ I1 J" o$ g
    ) p5 T' x7 }6 l8 }    Recursive Case:
    0 o# e( J: g; a+ B  e: e9 C. l! ^; u0 h8 j! ?$ ]
            This is where the function calls itself with a smaller or simpler version of the problem.
    3 G5 j* m* Z. \, N7 d6 A/ N  w9 E+ [: ?
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    / e; e& S  y- ]
    $ ^2 m% b+ d- Z! s, ^9 V1 EExample: Factorial Calculation
    3 K* E( `( }) m2 n; D5 W( J" a
    ! P/ N1 \& ^7 g& I- p1 T7 o% QThe 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:
    6 B  ~+ N: {3 `# ?' G3 p# U0 D- S$ y
        Base case: 0! = 1$ ^" Z, h3 K) ~2 C, \8 ~

    % D  a, I& h# _: ^3 ^. g    Recursive case: n! = n * (n-1)!2 Y6 t( t! c9 L; p8 P& f
      N. O3 x3 |+ ?1 [: x. x) c% i* C
    Here’s how it looks in code (Python):
    ! ~5 c$ a& ?  Z+ g$ N8 P4 F7 bpython
    / _( `$ J+ K( m7 w* i  c6 r# v- y7 ^8 n5 ]
    - B& o4 q# Y- |" e9 t
    def factorial(n):$ v! v1 h, g% J. s
        # Base case" h" g( f0 `. R# r; ?. o
        if n == 0:
    6 \2 l4 C1 m9 `5 v6 O( [; r6 V+ t        return 1- O: a7 A) E, h$ s, T
        # Recursive case
    1 W' D; h8 }$ t0 n3 M    else:
    : K2 I" k9 N/ v8 z: t8 P; I        return n * factorial(n - 1)& K4 a$ G+ g- e( s' _

    3 \5 s; o# Z' Z; Z3 E  t4 ?# Example usage
    ) x; U# {6 U" x4 p0 H: xprint(factorial(5))  # Output: 120
    / L- x" j/ s- l% e2 ^( _. I* ?6 d" k  R/ }% Y/ u5 j
    How Recursion Works
    $ o1 n/ e. a1 T$ F# u
    : M. A# F! k7 k, L$ N    The function keeps calling itself with smaller inputs until it reaches the base case.- ^* T2 j/ U* J/ }
    4 z+ r3 Q! F! I: |9 g, ?: P
        Once the base case is reached, the function starts returning values back up the call stack.
    9 k! p8 F  e" H2 M0 U1 q- x  d% N4 `* `2 V2 o  ~4 n
        These returned values are combined to produce the final result.: j. R  Y0 w5 n7 x! ]0 _
    ( _" S# Y$ [, F' _7 z4 ~/ Q
    For factorial(5):3 Z  G- k' q2 ~0 B% x
    # A; r' V+ \# k& L" f( |- y
    % C- @3 K# d9 ?. }2 T
    factorial(5) = 5 * factorial(4)5 h, o$ o) S' q2 ^
    factorial(4) = 4 * factorial(3)' r0 q& H5 T% d* G3 o4 b1 Z
    factorial(3) = 3 * factorial(2)
    & w5 C9 E- s$ d& I/ m, _  N$ tfactorial(2) = 2 * factorial(1)
    ; ?4 x$ ~0 p. w% Qfactorial(1) = 1 * factorial(0)# v' y# T5 L8 l) ~! b
    factorial(0) = 1  # Base case
    2 [1 t$ i, U4 K
    9 _" E4 z& S0 `# bThen, the results are combined:
    / z# c9 B1 L3 X
    0 F5 o$ w) J! P* {# s
    + m6 R1 Q1 l+ P. F, {factorial(1) = 1 * 1 = 1
    " G! P$ H" s8 h$ _; [7 l3 qfactorial(2) = 2 * 1 = 2
    ) C* @- y$ u$ }" e+ P# Sfactorial(3) = 3 * 2 = 6
    $ g3 d0 I. E& B- O% kfactorial(4) = 4 * 6 = 245 B8 o4 ~1 x$ V  S2 q; r
    factorial(5) = 5 * 24 = 120; v% a5 m. `" X; [
    # N& k% y3 [4 z: p8 A6 l& P! e" S
    Advantages of Recursion/ A3 q" T. x4 Y- V5 v( f

    + ]% [0 ^7 Q$ C, P    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)., ^9 n$ l; c4 ?3 F1 W  t$ t# e
    + }% _. W* C" R/ o1 C
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / L: i  ]1 M- _1 a  ~
    + n( l! J! f" p+ ?$ O" eDisadvantages of Recursion
      w" R, Y3 S/ c& A4 D; A; J! ?. v& s; P
        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.. i3 ^2 A) `( w: X

    ! L/ g/ c) m9 D4 \3 ^    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: o- _$ ^% s2 l, s4 s  k0 m- l
    " c8 _' w1 q2 e9 I' y
    When to Use Recursion8 _$ m) N: w* g3 q0 E- p

    : F/ b0 p7 S7 l' Y1 `. K    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! i0 n: H" Q2 ~. u$ U
    2 L1 ~% ?6 a% H; N- C8 S+ j1 B
        Problems with a clear base case and recursive case.' n8 q7 U( E) O9 r3 Y7 p' Z

    ! W2 }0 c: e0 }Example: Fibonacci Sequence- o9 v& T2 W" D( l+ `+ g# O

      |. _9 X- f6 Q! Y: z# UThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ; e# i, w( b0 Z' `7 w: ]2 f: O6 i: n# i- P7 H7 o- K
        Base case: fib(0) = 0, fib(1) = 1
    2 l6 J+ O) p  O1 T6 r" [$ t7 U0 O6 o- w( p' o# t2 g7 G: }
        Recursive case: fib(n) = fib(n-1) + fib(n-2)2 D8 \0 J% c5 Q2 x# l( S

    . d* _. R, w% `8 a0 l; M4 Xpython
    . T/ D1 @8 F, K& d
    5 c4 U& E! H$ x- j
    0 k  z3 |& t7 u, ?% W8 qdef fibonacci(n):
    8 _" g0 V1 v9 U& f6 I' V3 a* j, H    # Base cases
    2 ?7 s( c$ d5 x- o/ X- p) ~; J    if n == 0:
    0 H" ]2 r. x6 w8 P- E1 A        return 0
    4 y/ x, V+ Y7 @$ Y4 |: E, ]    elif n == 1:! V9 G3 J/ [, i* a/ d6 k3 X& P) I
            return 1
    3 o+ M) {6 d8 @4 b( E! n' M    # Recursive case
    , R0 v3 R8 U( w9 }3 Y    else:
    7 |# ~( m- U' F+ k        return fibonacci(n - 1) + fibonacci(n - 2)2 v+ h) p" M6 E1 _7 U/ g' I" E
    0 x  P( k/ x' h# A4 V9 Y( ?1 c
    # Example usage: M5 q+ n% t, p
    print(fibonacci(6))  # Output: 8! W7 e9 j% a3 @# p# A; c
    ( @2 b: O/ j! l" `" I+ Y3 n  b
    Tail Recursion9 u; }. ], x. ?

    6 J. J$ }: S6 T8 `  f* M2 \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).
    ! G' S% n& G& [% I( V5 w
    8 U5 m/ f3 m  A/ ~' f6 _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-3-28 18:55 , Processed in 0.056755 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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