设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 X( `1 z. ~$ u$ p

    + h6 O4 N2 E" q" U解释的不错
    ' y8 l. _" Y+ S  l1 ^& l& }4 w( ?# L& H; s" u
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ; I" f& g1 R. \4 V1 I2 Z, s: c" {3 O, h' n, @3 @+ F
    关键要素+ \4 p, C1 n3 {( z4 e: \( e
    1. **基线条件(Base Case)**
    / ?& Q# d# ^' t( w  y$ d" u   - 递归终止的条件,防止无限循环
    1 G: Z' T3 P# e, \2 m2 u% L   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    # v% v6 o. G5 j( m2 W
      Y3 j! K3 Y1 v7 P1 m2. **递归条件(Recursive Case)**% J9 C( o2 h# C. {: Q- l4 o
       - 将原问题分解为更小的子问题
    # _7 ?: |0 s: R9 `   - 例如:n! = n × (n-1)!. C2 g* M% Z" t/ i9 u! w
    - B. {; ]( H% K$ h8 j
    经典示例:计算阶乘
    5 @# ]- v& U% d. Rpython
    * A, t9 S5 E- V% b! cdef factorial(n):6 k3 i; w5 j0 A
        if n == 0:        # 基线条件
    " A0 p8 H" H" c0 h  M! P6 m2 e        return 1; E) D, V. O1 V: e: B
        else:             # 递归条件4 `" z6 T( s; n
            return n * factorial(n-1)
    9 I5 r8 C0 X/ f& v5 a" H, h执行过程(以计算 3! 为例):. g4 Y  p% E1 a& ]3 M0 c$ [
    factorial(3)
    ) k' J* o1 s/ n4 g- d) i8 F3 * factorial(2)( A0 D$ z& k" t. h- K
    3 * (2 * factorial(1))! n5 Y2 L3 M& A# w1 ?
    3 * (2 * (1 * factorial(0))): S, c& W- K# i0 F3 G: i
    3 * (2 * (1 * 1)) = 6: k+ s& j+ r# @, N- b8 j

    - r7 ]( K1 J/ O  s9 n) P# f" O 递归思维要点% F# P1 h7 w, s2 e: P% P' L
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    , k6 C  j5 |2 Y3 E7 K! R$ r6 f# c2. **栈结构**:每次调用都会创建新的栈帧(内存空间)% n- \5 U0 K0 W  D2 x
    3. **递推过程**:不断向下分解问题(递)
    % `7 i; n) L( c. f- M6 I4. **回溯过程**:组合子问题结果返回(归)
    # q2 m+ _5 c- {+ Z
    4 ]: T& M" u- O$ |% @ 注意事项
    6 m& }$ l9 z8 u& P( K: V必须要有终止条件
    * c8 m; j2 Q( e, ^! z递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & F5 f: Z3 \, P1 a* r$ O某些问题用递归更直观(如树遍历),但效率可能不如迭代
    - u- r: }: J) u  s尾递归优化可以提升效率(但Python不支持)
    " @( x$ [, }2 T& Y. x
    ; V( c/ I  j0 k. N 递归 vs 迭代1 P( [' B5 U9 c! n& |
    |          | 递归                          | 迭代               |* z- S: {! s+ f2 l( F: Q9 k" ~8 I( y
    |----------|-----------------------------|------------------|( g+ A3 ]3 F4 C% j! n; |; B
    | 实现方式    | 函数自调用                        | 循环结构            |
    2 ]0 V2 B0 s* [3 N2 [2 V! T& V| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , P) [- ^& n4 |( C+ U| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |$ ]- f  N0 a# S8 Q+ p$ g& j1 F
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    4 O8 ?$ D- K# S) J7 b% g9 V/ A  L
    3 B8 O! E- X/ L" k5 X& @8 r( F* _ 经典递归应用场景! r  d5 N1 x$ B& u  g5 @9 a2 j0 a
    1. 文件系统遍历(目录树结构)
    # Q& ^2 u0 y' A; O. W2. 快速排序/归并排序算法
    , V' X( @: G9 o; q1 @8 F3. 汉诺塔问题" r5 a7 b+ B# x# J: s4 [' }
    4. 二叉树遍历(前序/中序/后序)
    0 w: P% F1 q" w4 n4 c5. 生成所有可能的组合(回溯算法)
    * b4 o- O/ [# b4 f6 H9 I) Y5 p% R' p! p; k- i/ F2 E) ^/ O$ l9 @
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    7 小时前
  • 签到天数: 3210 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ v# P, I1 N0 c5 B7 L2 `
    我推理机的核心算法应该是二叉树遍历的变种。) _& j9 P- V* z8 ~' 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:
    & p* V9 T1 U4 r6 xKey Idea of Recursion
    / M/ m5 w: ]7 l
    2 @& [$ g$ a; y  ZA recursive function solves a problem by:
    % {9 n, O& @" n; @/ v- ]
    $ i5 s. v2 y4 A' a    Breaking the problem into smaller instances of the same problem.' s8 u8 P4 D# I+ C# \0 [
    6 ]( i( I9 t" p9 l. O
        Solving the smallest instance directly (base case).& t( ~7 p- j5 y9 N/ q

    8 N5 U, F' S0 s* I: @9 L# Y: C  D    Combining the results of smaller instances to solve the larger problem.& W7 _' w, B: m0 \
    * Q8 s3 Y+ o+ W9 ~
    Components of a Recursive Function
    % Q' B8 M1 X; d3 m' v% Z
      Z% h+ ~; L; e, C1 o, I8 ?    Base Case:& \7 W3 G/ c- R/ P* M( z1 R; O- W) K

    2 W7 N1 U5 _" {# D, W        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # s$ V* Y& Y  m3 ~2 q+ Q6 F' Z. |9 \# k) l  x/ V6 q
            It acts as the stopping condition to prevent infinite recursion.% x/ [* m, I4 r/ ?
    2 z4 Q1 H! U; q/ T3 z: C- U5 b
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
      x8 B: k% {9 \- Z, T! C" D2 j- t+ @, z7 s) p
        Recursive Case:
    " Z! K9 m$ U. O; p
    ) n: J* E; D+ J+ @/ n0 K! T        This is where the function calls itself with a smaller or simpler version of the problem.8 j# x3 C2 |" d7 |; S. a+ ]% J4 v

    ) G1 i! V  l2 Z* ^5 R4 d! n        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    + ?% y/ X% p1 O1 ]# _& j: ~6 f! [2 U
    Example: Factorial Calculation$ Y8 s2 @% @: V
    8 l, q0 ~8 X9 K- l& R
    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:
    % P! _# a- C/ z& j3 s# F# P7 [
    8 G6 t1 u1 N# A    Base case: 0! = 1
    3 J( f, O/ r( l% x, w- [# c, o' |5 w$ ~
        Recursive case: n! = n * (n-1)!
    0 ?5 Y) s0 j9 M
    1 b9 r0 H# O( ^1 M( y% e5 P9 x, w' YHere’s how it looks in code (Python):8 [! h( l0 ]' _: a  N; T/ s- ~( J
    python
    % y& A( F2 n" P* d
    4 ]$ g( d- O2 y0 w+ k9 i! x, r" A, s1 O) f! {( f' b
    def factorial(n):. Q2 |& Z6 f0 B" n
        # Base case8 A. z# m7 Y' d4 {6 v! n4 a
        if n == 0:, }3 j2 k: y3 a. @* `
            return 1
    - ?$ x( |. w% T7 q& \    # Recursive case
    & \# `  e! b+ u% Z: E; Y: [    else:
    8 C0 a  I4 H6 F$ m/ N7 y2 N$ l4 W        return n * factorial(n - 1)" k: M5 Q4 Y& n( B( e0 k

    5 r  D! P9 Y5 U7 c5 l- M/ C# Example usage  B6 N1 W% V- d2 ]0 f( ~
    print(factorial(5))  # Output: 1203 C% G% @# k6 `4 ~4 {

    4 k9 x% ]4 e6 w3 G0 ]; LHow Recursion Works( ]. s4 S/ l- B: s; L3 n8 f

    8 @9 u$ x1 M1 C) |6 m0 T, B$ n+ @    The function keeps calling itself with smaller inputs until it reaches the base case.+ d( c$ A2 I+ S- b# ]4 f
    , c, D$ ^& p% O" O$ y
        Once the base case is reached, the function starts returning values back up the call stack.
    3 w3 {+ z- [7 R6 Z
    2 X* B$ m( \3 P8 b7 M$ n    These returned values are combined to produce the final result.
    5 r9 `" G- }  |6 \3 [0 n/ E" R: S3 b0 \* b% ?' T  _
    For factorial(5):% M7 m+ L, G& U4 c

    0 g% \. r. \. @# @' T& l. \
    ' L) w6 \3 S( K: h: lfactorial(5) = 5 * factorial(4)
    ) r9 e' J1 g8 D  W( Yfactorial(4) = 4 * factorial(3). g7 Z. a/ I0 l  x& W6 I# s
    factorial(3) = 3 * factorial(2)
    7 N! a0 y- C* I2 U  |factorial(2) = 2 * factorial(1)/ r+ p' V1 l6 [9 h1 ^
    factorial(1) = 1 * factorial(0)
    4 D* b0 P7 e& h. n' U* S$ Yfactorial(0) = 1  # Base case
    ! y2 s2 {6 ^1 d$ [3 m) q7 d. I
    / ~0 f8 w% Q* l: a+ [- PThen, the results are combined:4 W1 r& g  E. R4 n
    : R% K2 s  |( r& j  w
    ! }/ T, P! E, ~! e! U  s
    factorial(1) = 1 * 1 = 1/ T$ ~( T: M1 h3 b
    factorial(2) = 2 * 1 = 2
    3 T- O4 r. T, C/ n9 o& Hfactorial(3) = 3 * 2 = 6
    8 Q6 Z: h4 E6 w3 X. S# Wfactorial(4) = 4 * 6 = 24, ^8 g2 p$ n) R# L% ?  o' p$ J6 h
    factorial(5) = 5 * 24 = 120- G+ O- j* P0 Y1 {! E
    % |" [: t* l6 ]# E2 Z8 m
    Advantages of Recursion
    ! z. q- H7 y" T; [" i; {
    7 |( A1 ~2 ?5 d/ A6 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).$ y5 W: @) X, B% O7 |* t1 z
    ' p! @! B! Y4 F
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - V) r  s% Z9 s5 X  i- M
    % ~4 H0 W# O' i6 x. M& f4 F; pDisadvantages of Recursion. g- L, w4 B' u7 ~. i5 z
    & g" Y' C0 W% E4 L. t. _8 _% U9 _
        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.5 h% F2 _8 @. X- |

    5 D8 L0 ^: F  v; O    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ P3 Z. F$ m1 z6 M7 {7 R
    % G! G: ~3 A4 t+ q+ ~" x6 u2 I
    When to Use Recursion6 L* {* `( X1 k6 ?9 F
    ( @5 m" F3 G* }
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 ]. ]# M  b' B4 k; y) d+ h1 `& ]* \
    ' m5 H: X7 T* T) L  p( O
        Problems with a clear base case and recursive case., I% r& C! p( R# ^

    * d% r% |$ y& U/ QExample: Fibonacci Sequence
    9 X' ^; Z" [6 E. y( P; d6 m
    9 R$ y$ w7 z5 p; ~+ e5 r  AThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ' X7 H9 K) B/ Y  J4 W& p) Z/ k$ M5 B, i9 b( X/ H8 y; q
        Base case: fib(0) = 0, fib(1) = 1
    % z* k  O% H- G6 J  L  K, ?4 ^$ t
        Recursive case: fib(n) = fib(n-1) + fib(n-2)0 t8 T( n2 q% ^7 n$ ?4 J" \

    7 v9 z# m7 Q3 n! b; {, x, ipython
    - W* ^! w) U$ R# |1 s+ E
    6 v  I- ]  Y+ j, P" _
    ( p% Z) V4 j( edef fibonacci(n):
    7 q( ~5 d9 n3 ~% y0 [' ~    # Base cases
    $ E+ h! z7 H( \    if n == 0:
    & l4 o" k& w% a1 G* E$ c' X        return 0
    4 t; u2 K' z  ~& u5 v. _    elif n == 1:
    / F& E; C3 V/ q, e9 r: g; J+ j        return 1
    * Z5 U5 f9 s6 u8 b2 O    # Recursive case
    # I4 W; Z0 n2 i! I    else:8 Q4 G5 q" ]! D* j/ K
            return fibonacci(n - 1) + fibonacci(n - 2)
    ' ]" u8 |" n; ^8 z1 J, N  r' I6 l' \- s! O
    # Example usage3 P9 x5 W, {# t! l) Y& p% F  F
    print(fibonacci(6))  # Output: 8# T& a5 N: i$ S' p/ Q0 z
    3 h# c( I$ \4 [
    Tail Recursion
    , p0 n5 L! H! ?+ v) ^+ {. P" N: _, l. t" r
    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).! t- v% z7 Z% V

    0 B% h4 S0 V9 R6 k+ C4 g; fIn 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-31 14:02 , Processed in 0.066763 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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