设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    0 i: _. Y+ m. N5 j
    . S% N9 T& ^8 f, R9 n解释的不错
    ) ~/ y5 m  b. T( \
    2 U7 n  G1 |7 z, o7 x0 N2 b5 ~+ y' r7 O递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。3 v0 \: \" u$ c5 q+ s# m
    & i# @! U( K2 x6 d' |
    关键要素7 h9 Q* j  e" Y9 |: D
    1. **基线条件(Base Case)**
    2 f: y, z6 ^7 s. V0 \   - 递归终止的条件,防止无限循环
    ! K6 _7 [; {( N) l9 H   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' U) q( j% |& T$ }0 k
    $ [4 C% @: z2 l/ c# ~* U7 r
    2. **递归条件(Recursive Case)**
    / ^/ b1 F* M$ A8 [   - 将原问题分解为更小的子问题- \( v: k" Y; z  ]) S
       - 例如:n! = n × (n-1)!
    2 r; i$ r9 g/ c6 T4 s# I: k: D9 V0 i
    & d  P. w+ S* F- V, i0 ^' i 经典示例:计算阶乘' r8 E3 e0 I3 r- Q
    python" X6 M% ]7 j) W0 b
    def factorial(n):
    : t3 \3 T# {/ R5 A% w' J: l    if n == 0:        # 基线条件, Q* S# _' R1 k5 l4 @! @
            return 1
    * V/ G! Z# b- v( k    else:             # 递归条件
    # b+ ]& k. @- ~! `7 M  E$ |# W/ h4 O        return n * factorial(n-1)
    8 M6 ^3 T% }0 g. v  O执行过程(以计算 3! 为例):& G6 H. ^* M. s; a" N
    factorial(3)( P9 l* ?& ]" U- H# j/ U
    3 * factorial(2)1 N! e2 z( i. N( ^. }
    3 * (2 * factorial(1))
    + d: X. ^2 I* |8 l# K3 * (2 * (1 * factorial(0)))
    / b# ^  [& V$ v7 `- b- |3 * (2 * (1 * 1)) = 67 w8 G9 b4 E5 {0 P% {

    ( ]3 ~7 B/ M8 c' Q9 P9 P 递归思维要点
    : T7 S; _; T) R  L& T9 b1 b1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 N, p2 O% a) b, ^0 W! S) U2. **栈结构**:每次调用都会创建新的栈帧(内存空间)5 z7 \& {, q& K' c5 l4 c6 Y
    3. **递推过程**:不断向下分解问题(递)
    ; C+ Y2 ]0 A! w! ]( Y7 U* \4 l4 \4. **回溯过程**:组合子问题结果返回(归)
    9 {8 U+ _/ q$ `2 q( T/ ~" m
    % y9 e6 x! D; g; X 注意事项6 r" M/ X! j, k- {. c8 z
    必须要有终止条件
    - C( {3 P& K* N. u递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 m) a6 J; V, V8 m+ u2 e/ p* Y某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 d  p: f2 d7 J0 Z; t4 x尾递归优化可以提升效率(但Python不支持)5 @/ V; G9 s0 r6 k) |. z3 z4 q
    ' D" F* d- m# [7 y+ n1 I. W' D
    递归 vs 迭代$ f& d, L) V9 J  h
    |          | 递归                          | 迭代               |4 V0 J5 s( D7 h/ U0 `% N- c/ B
    |----------|-----------------------------|------------------|
    0 e1 f* R/ o- ]# B$ z| 实现方式    | 函数自调用                        | 循环结构            |$ ]9 l9 c# b7 [& ]9 f$ N+ a0 V
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) f! c8 N4 Q4 r, H3 y# Z+ }5 C
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |: ?6 D- n0 W& u
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |* V2 W6 G) E1 u. y$ P  F$ R
    5 Q$ s0 D, N) y5 E7 Q
    经典递归应用场景
    / T4 ]0 w0 J. o" z' b5 s8 W3 O1. 文件系统遍历(目录树结构)
    3 I1 e/ ~( P7 g" v2. 快速排序/归并排序算法* z* z( c% Z2 @
    3. 汉诺塔问题& g& K/ Y% }( K* p5 X+ s; r
    4. 二叉树遍历(前序/中序/后序)
    ) V  J# [5 Z8 {. x5. 生成所有可能的组合(回溯算法)
    ' n" m$ [$ v* Q$ t
    ' ?1 m  F& X; p  h3 E, o+ ]& W# T" p试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 10:05
  • 签到天数: 3130 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  n5 K  b" P7 z- L" J: b5 M8 _8 X
    我推理机的核心算法应该是二叉树遍历的变种。7 n: b0 B$ p7 q8 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:
    2 b8 S( O. Q9 }  \9 E; FKey Idea of Recursion1 K' e/ ]2 V7 i
    . f% I' _1 ]$ w8 Z! p
    A recursive function solves a problem by:
    9 e# f5 S7 R  i. Q; o
    # O" `$ `7 `6 |5 W8 \    Breaking the problem into smaller instances of the same problem.6 D7 g5 c( D5 M2 D7 P! r
    / s/ Q. `. b4 |4 w: u$ {
        Solving the smallest instance directly (base case).8 t! a; }/ R' I. j- H" n
    " F, w! i  F( z( O5 z+ j
        Combining the results of smaller instances to solve the larger problem.+ y/ v* I' m! d
    ! {9 R- O; x' a6 E/ j
    Components of a Recursive Function/ H0 b$ m5 V/ y  Q( N2 S! y& H8 a

    / h2 n( Q2 y& {" r* m. D    Base Case:
    6 n. X) Y2 F: c' ]% ~3 K- ~! \' N$ d4 _
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.; A1 H+ l! l, H7 X  b/ Q2 l

    - S. Q4 q5 W4 z4 a4 ]        It acts as the stopping condition to prevent infinite recursion.+ Q5 Q$ H' ~* a7 n. C0 l! p
    0 x* n# h  P, a  [; _
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ! g6 |% o2 K7 ^, V$ `
    ( U4 |0 ~8 R8 [+ p1 M# H4 s    Recursive Case:& b% I6 O0 a! B8 ?# S( l" I7 H

      X6 C3 k: L/ n# f8 W        This is where the function calls itself with a smaller or simpler version of the problem.
    + f7 ^$ b) a7 s. V) E1 ?( f) Q9 p: y0 q1 J$ O4 ?
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).; J- F# S: n4 o5 f! D" M+ j& N6 }

    ( Q0 {' N2 G" ?/ X+ nExample: Factorial Calculation* E$ }8 n3 g' j" j* T5 E
    4 V( p' ]) U! n1 n
    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:3 i3 Q% Q6 B& q! x  J

    - {1 x+ a" x# ?; h+ o    Base case: 0! = 1
    , }0 n* o: U# t% h
    ; q: q# A8 |+ N4 I    Recursive case: n! = n * (n-1)!+ }) w' k0 T+ x
    4 ^3 p% i/ D3 G  G; j
    Here’s how it looks in code (Python):  t! G, X. {+ b4 Y
    python# R4 z5 W4 c7 v: @% o8 _, o; _

    6 {3 w: l% i, s& W
    + |5 M  N5 _1 V; V0 C* Idef factorial(n):
    * T+ |, B3 y9 D, s1 T  O    # Base case: r9 m0 p3 ?% r  q, Q' n5 ]
        if n == 0:$ l! R5 o" K3 y% S& g* K
            return 11 r% K, l% G* h7 T
        # Recursive case
    " \7 A$ O0 j3 i3 x- s    else:
    % w4 E# f7 u2 W* k6 ?/ y/ R        return n * factorial(n - 1)
    & S8 Y. g! L- R* s7 x# y0 Y2 O: D
      D- k6 }0 d6 t' [  O8 x+ a' w& r# Example usage- }$ K: V( b$ z5 a2 L6 g) a- G
    print(factorial(5))  # Output: 120
    9 ~6 p2 ]1 D2 I( ]1 K$ K6 T5 ?1 k1 e5 w+ G
    How Recursion Works
      V# z0 g3 r; Q
    3 F) x" O' R" y  a    The function keeps calling itself with smaller inputs until it reaches the base case.$ R7 {: f1 O, E, b: g1 v. P

    : W2 ~* Q3 ~) D0 ^    Once the base case is reached, the function starts returning values back up the call stack., R$ A; ~8 b- {( q8 O6 z, W

    . n' t4 d8 ?$ X    These returned values are combined to produce the final result.& U% G5 B8 q% ?- @1 Y9 h
    ; n, m9 V2 z2 O4 A/ c
    For factorial(5):
    - H' H* T3 Y$ h
    $ E$ y; B0 _+ Y  L4 @8 {* @4 R
    8 s" v7 d# M2 i1 {; p9 @/ Cfactorial(5) = 5 * factorial(4)0 T- J3 p( s4 I7 c
    factorial(4) = 4 * factorial(3)
    ! K0 L. O$ z! B! U. i( X. vfactorial(3) = 3 * factorial(2), C; U8 L& y; h! M% G( n, ^. y% O
    factorial(2) = 2 * factorial(1)1 Y  E( ]5 K. j
    factorial(1) = 1 * factorial(0)+ n; B. A8 {, S! ?
    factorial(0) = 1  # Base case% ?, g) S% o; }: k

      Z7 A7 ?" Y) w( w5 U5 l& ~Then, the results are combined:: S* j7 W, H: P. q
    2 d9 b4 l6 {/ |) S7 R
    8 ~2 Q' a8 T7 N  ~! d- u
    factorial(1) = 1 * 1 = 1
    7 @5 w' E3 l9 r4 E7 `  D" rfactorial(2) = 2 * 1 = 28 ~' G, x9 H- E, O1 _) N  E$ o6 f
    factorial(3) = 3 * 2 = 6* r3 V  `/ l5 s/ _
    factorial(4) = 4 * 6 = 24) z/ D3 G  l# K/ o
    factorial(5) = 5 * 24 = 120
    4 U. ?  c7 c: X/ _0 l' N% ~# m+ f* h/ i, V1 d
    Advantages of Recursion
    1 M; D1 |5 n3 }# j3 {% s4 E- [' A# X) A2 a, G# q
        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).; w$ A3 u( Z3 C* k. B/ h+ H0 n; a5 i
    - U6 j3 b& j2 x
        Readability: Recursive code can be more readable and concise compared to iterative solutions.0 h2 N2 Q! n2 ?) c+ c7 ^
    : D4 n" W0 }7 {2 R
    Disadvantages of Recursion' b- o# D6 v1 S! s

    % g6 ]% C. t4 G  }, e) g5 ]    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.
    8 s$ ~/ v7 \! M- @1 S% e% U# w0 R2 V+ s/ N
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).0 \% b0 M9 Z6 G, R5 I
    ) }5 J& w$ B0 r1 s: G
    When to Use Recursion
    ) J" d* m+ f4 E( ^
    3 ~6 ^+ i0 r, Z7 E1 D( N( Z6 v    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + U* A, H( n- ~2 C0 c- P4 Z8 c5 D; I9 R, J
        Problems with a clear base case and recursive case.
    # c: `0 s0 g' x2 R* V+ b2 V7 R$ C) H( T0 W# V4 E0 ~
    Example: Fibonacci Sequence
      \# m; ^. @9 q2 t+ V% ~5 F- k4 v; s7 D* A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 T) p& v0 B8 V# l% r5 D: x$ t! L
    ' k# v' D: r+ ~8 x
        Base case: fib(0) = 0, fib(1) = 1
    9 X: z" r1 [% c* P! U
    5 `* B% L- b: g# e) I' b5 q    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    7 S' f! B" K$ G* ^0 q/ k/ h, K- n9 B7 h" i0 {7 M3 _
    python
    ' z* p3 J% o5 d
    7 O  A( F( Y" ^, I. D- `$ Y8 y0 J7 [( I
    def fibonacci(n):, i( g, D% }" L6 ~4 g
        # Base cases- D5 H+ `8 q1 H0 p: ~
        if n == 0:8 `7 {' Y: j) J1 b9 K
            return 07 w/ `# w* |7 |: E1 n, e
        elif n == 1:" _& \5 W; O9 B' v3 W! ~, ]
            return 1
    3 z: V# V" Q& b% @, w1 t! O9 D    # Recursive case
    1 M+ e7 ^. |& @  y) d    else:
    ( N" ?% {& ?$ D7 b4 C        return fibonacci(n - 1) + fibonacci(n - 2)3 }5 L( W- t& H9 z& f* N0 X

    & t# i$ x* ]( c1 J8 ^# Example usage6 c9 Q" ~; @& P+ q7 @% {
    print(fibonacci(6))  # Output: 8) g0 m& n$ a/ Q' |
    3 A5 t9 X) B2 G9 r6 k* ]
    Tail Recursion0 D. \# R3 C5 S  o) r2 b
    * D2 }9 i+ E( K% C/ K- D
    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).
    & |/ Q! o  Q  q3 {$ x
    - y% @$ _+ y; C$ l' aIn 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-12-29 10:34 , Processed in 0.030900 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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