设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 & F0 A1 ]8 ]( B7 x- G7 B- q7 U

    1 z9 f1 q8 D7 D: ?1 r0 U. [% U! o* r, v解释的不错6 `3 g4 E0 g! t/ v. V

    ) X* i2 I8 r* o! {递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- o# k2 V# Y, e- g3 g: s
    ' j5 U! E" p5 T+ l6 O6 \& R
    关键要素. M4 M9 ~9 N6 k; C
    1. **基线条件(Base Case)**/ @6 m' J6 L8 t9 R
       - 递归终止的条件,防止无限循环
      Q# Q" F$ A) v   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' j8 J' E' V1 w4 ^& p) @3 ^  q/ s
    & V& ~) |; Y% W/ E- F
    2. **递归条件(Recursive Case)**
    / d  @6 c$ c( j5 R   - 将原问题分解为更小的子问题6 `, k# f* \8 S8 |: H1 X
       - 例如:n! = n × (n-1)!( j* Q/ g: w: w4 i
    - B/ P- x: T9 O1 Z! h/ C& ^
    经典示例:计算阶乘: S9 f+ \7 U! p5 [; c
    python7 H3 {$ |* a1 @2 P# B; @6 r7 c" j
    def factorial(n):
    . v  m3 ?, t4 x! F+ H( v$ O    if n == 0:        # 基线条件  ^0 Z; H/ I  ~$ g" o- ^
            return 12 Y& [/ J$ x5 I) N+ r
        else:             # 递归条件/ B  A- [% [* ~  c
            return n * factorial(n-1)
    ) o7 F5 T* a$ ?* D2 ~1 e# z0 E7 e执行过程(以计算 3! 为例):& I. F2 D. w+ H6 U; M
    factorial(3)' e, L0 Z1 o0 H* l9 x+ Z; b
    3 * factorial(2)  }  e. U1 a& d$ R4 p9 I
    3 * (2 * factorial(1))
    ) \7 Z6 Z7 t- x( Z% h1 V3 * (2 * (1 * factorial(0)))3 R5 y& Q5 d  @. q
    3 * (2 * (1 * 1)) = 6
    2 s& _/ q( z+ g7 V- Q3 P6 L
    $ Q  r! F) p- p, D8 D0 Z 递归思维要点
    3 a3 O: Q9 F. M+ Y2 [5 v) H7 v1. **信任递归**:假设子问题已经解决,专注当前层逻辑  g: ?/ }+ h- L0 h6 r
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! G; `0 A0 j. g& K' Y# ]
    3. **递推过程**:不断向下分解问题(递)
    , D. x0 i0 x) t4. **回溯过程**:组合子问题结果返回(归)$ x2 h  c9 q( D

    & P' e3 S# t) B+ }2 u, _ 注意事项  \, F' Q3 _" S- H. g; \8 ~
    必须要有终止条件+ P9 O3 m$ s5 ]( k$ g3 g1 l
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层), x% j9 d8 j" f2 {
    某些问题用递归更直观(如树遍历),但效率可能不如迭代- H4 t3 m' q0 J% c0 x
    尾递归优化可以提升效率(但Python不支持)$ t1 @1 k% P% Q1 M* n! ^, O2 {
    " v. U% y  L, r; Y: V* M# `
    递归 vs 迭代
      E9 C9 I8 s9 [) H& H' l( ]|          | 递归                          | 迭代               |
    ; F7 U" A0 [& a; u# @. j( `|----------|-----------------------------|------------------|3 N% U, _2 I& g( I8 N* A
    | 实现方式    | 函数自调用                        | 循环结构            |
    % I( T9 i6 L' g* K8 b( l  N| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ' e7 E1 }0 J" _0 T) q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |# X+ k7 c. d. @* z# ~
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ l; G" i2 p; b* f) ~0 l
    ( a) R. J7 g* A7 W
    经典递归应用场景
    , z1 B% I9 a# A: h( K; A8 j/ u1. 文件系统遍历(目录树结构)
    ; g* I4 r4 w/ ~2. 快速排序/归并排序算法
    . \% f+ E1 p7 B; Q2 B, R! \+ m3. 汉诺塔问题
    7 p  Z5 p# G% A  a( e; j4. 二叉树遍历(前序/中序/后序)
    $ T4 Y$ r* q0 V$ k6 i+ L% Z# t5. 生成所有可能的组合(回溯算法)' v) R$ z' s  g6 _

    , |% ?1 n* l; n( R; e% x  ?0 N试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 s) v5 l4 D7 C- ]3 r+ H7 v0 |
    我推理机的核心算法应该是二叉树遍历的变种。
    : a$ F* [7 E2 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:
    / d& y. b3 H7 Q; c1 n. _' C: EKey Idea of Recursion" o. A1 n8 j" i" p# V

    4 q) A) f+ h; m2 s2 |# w9 cA recursive function solves a problem by:
    & Z1 O+ V" j5 w8 F. Y8 a  o4 V% K7 r* Q
        Breaking the problem into smaller instances of the same problem.
    . U" F' X$ P* f" _" s5 s' M: b2 H# L$ B* z5 J  |" u0 K  V3 D+ M
        Solving the smallest instance directly (base case).5 t; v) Z- L+ H  |1 R) ?  M* z
    9 {; n. c3 q4 K8 D3 d  Y& ]
        Combining the results of smaller instances to solve the larger problem.  V: z  H) j- L- C2 T0 j  q
    ' y2 B5 t4 Q3 T# ^
    Components of a Recursive Function) r4 s  [$ V9 g. I# z6 E
    1 \6 M( W( a% g
        Base Case:- p/ X$ A' h1 C3 _" B7 x

    / q7 L; D/ E" T" L        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / C- [: ?" n. Y4 ^* D+ ?9 z# S7 k& o' Z) e7 G
            It acts as the stopping condition to prevent infinite recursion.2 R" v+ I( _2 z: b0 _. O+ I1 m4 i

    & e1 [- F7 `+ }: f2 I1 m        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' `7 E4 U6 r" E" H+ r* U3 l( a

    * r$ h) ^" k% ]4 H3 F" P) v* {( T    Recursive Case:+ F. }+ D" u7 U6 S" T

    ; q* ^) x. ?' _        This is where the function calls itself with a smaller or simpler version of the problem.0 a2 C0 f6 a2 `1 `* A8 \
    8 u4 Z6 l3 o( s( h; r4 F, M
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
      |0 y* _" i% O  M+ r. ~& X% W6 Q! b- ~  l$ g4 g
    Example: Factorial Calculation
    0 y+ H4 G) N( p: V4 u1 X7 ?9 J  g6 \! _9 ~7 Q4 O& \/ R$ R7 g' z
    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:: }, B. R8 A5 ?
    0 o$ @, O# _- N5 G
        Base case: 0! = 1+ _8 d' w  R4 X* {: i

    , r, m; O9 a1 I    Recursive case: n! = n * (n-1)!6 W5 @  I! \. l. a/ f/ t

    6 p3 m  B( O2 _: DHere’s how it looks in code (Python):! o0 Y% K- X- Q- g( E: H! [
    python
    - o, h. k# L6 c1 G! D$ }( ]1 X: [" b6 @

    2 v7 y! j# B% J4 o  g. Fdef factorial(n):
    1 }, f: E7 ?6 ^* U7 R( D1 N6 x0 x2 F    # Base case
    5 _' G1 J( P) A    if n == 0:0 p* t8 I# `: s" }) }, _
            return 1
    + K1 G2 B/ |5 V6 Z' ~) b    # Recursive case, [. Y$ z/ m; C! D5 L0 l7 t& M
        else:$ `1 C! w" j) b  `
            return n * factorial(n - 1)
      r  _3 ]' h( n( q" `3 o8 y
    # e2 }; w+ l" }& @# Example usage
    . s; \' S8 j! |: S( |print(factorial(5))  # Output: 120
    * y6 U' R0 J1 v) S' Z, t
    " g$ z# e" o6 ~. ?* Y$ N9 PHow Recursion Works/ ?" F% a2 ]1 {" Q) T
    * T7 Q8 G% Q9 ~8 O5 \0 ]
        The function keeps calling itself with smaller inputs until it reaches the base case.$ }  S" H% p+ J( k' ?4 ~: v4 ^. T

    4 L1 X+ z2 Z0 J) [) ^/ Q" T/ F0 a0 Z1 _    Once the base case is reached, the function starts returning values back up the call stack.
    . i9 e( d- ?. m. X' @5 S% G, O; }# v+ P/ G% d2 o6 @. f& o
        These returned values are combined to produce the final result.
    ! ?2 u" h( D' r0 k8 Y- Q5 w1 N0 W- g5 h7 J* X+ J
    For factorial(5):
    ( K" N+ C3 O! e
    ; A# B3 Y  w7 x, [6 y" y% ]! A4 O2 F
    factorial(5) = 5 * factorial(4)& N, K; _% T; ^
    factorial(4) = 4 * factorial(3)
    " R$ w. I9 t2 j8 q( [8 Y- K: lfactorial(3) = 3 * factorial(2)4 o) O8 o  [8 |8 a8 f
    factorial(2) = 2 * factorial(1): i# S) G% L! [
    factorial(1) = 1 * factorial(0)
    % }/ h$ j7 n5 g& Gfactorial(0) = 1  # Base case
    . o* B5 ^. Y9 ^' N& u* Z- |& d# G0 a. _# }( B
    Then, the results are combined:
    ) s9 p( Q+ m9 J1 {" ~( D9 ]( E9 Z: W+ H

    # x1 t( j, G9 Y) Y; O, C% {factorial(1) = 1 * 1 = 1
    : O3 g2 }4 P- v, ^, H9 _factorial(2) = 2 * 1 = 2
    4 v' g$ X0 a" t1 m) |3 h4 U7 Sfactorial(3) = 3 * 2 = 61 ~# @9 \: A# v( `. b' R. d! [7 U
    factorial(4) = 4 * 6 = 247 ^7 ?- W* ^; d% O  ~2 j
    factorial(5) = 5 * 24 = 120
    / |& \/ A$ `. H$ }- o) n+ ^
    : K+ ]6 i' j- hAdvantages of Recursion& Z( R" `9 h) j( ~+ r* d8 F0 Q

    ) m3 k5 M+ I* 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)., R1 v7 E& ]1 S5 m, T4 I

    & V3 s: O7 ]$ W8 R    Readability: Recursive code can be more readable and concise compared to iterative solutions.0 r- F) ?# k+ M. b

    1 ^" i- c  ^0 B1 r4 S. F9 T- [0 qDisadvantages of Recursion: O' l4 `4 N' C' p( q1 k
    3 N0 e. T1 ^7 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.6 }" W- E0 I/ G

    # g% j, S& O& ~% t    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).( W! J3 M) h4 B$ A( p0 v

    # Y6 U2 Q) Q; E; k9 H7 jWhen to Use Recursion, C8 i4 I* o, R8 G; ?+ t7 b
    + Y) k+ f9 j, K% v$ J7 c6 Z
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ' C4 X$ ?9 H! z9 U/ V# o% X; H
    7 ~0 i% R# ?) x2 K9 N; G/ A    Problems with a clear base case and recursive case.
    4 F8 T: J/ A0 Q( t
    ' l# O* V3 A. Y, H" I( O+ a9 EExample: Fibonacci Sequence$ U  \' N0 E0 B9 Y* r% `5 W
    $ q. ~9 r1 T; g
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    / g* G8 t0 ^6 \7 j( u. q3 C- q: b, |. _. P* y/ t( d: H
        Base case: fib(0) = 0, fib(1) = 1
    0 q" @/ Z* J2 p+ @
      P8 `. A6 F7 y( w3 P! n* [    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    7 l- o) d; m+ _: `1 K3 S" {% ]% F% y& X
    python
    : O5 a9 n3 p& C7 t( K+ y! v5 y
    4 f5 Q, J) V; v: E+ D5 R( A/ }) N7 d
    def fibonacci(n):& v; \: l5 r3 r2 ?
        # Base cases. Y9 \6 m9 h% P$ P
        if n == 0:
    1 b, B3 f, q3 F: S9 O        return 09 n/ g& u$ m. d# i6 [) V
        elif n == 1:
    8 T  K7 U9 a* Q, Y) y        return 1
    0 k8 F6 j% _. F2 D, P/ o    # Recursive case
    8 y( Z  }" i% I    else:3 o% u: @- y& ~7 r- @
            return fibonacci(n - 1) + fibonacci(n - 2)( i6 p  q9 N2 h( ~& g7 ~5 c9 G1 c/ H
    * ]& v0 W* _& E2 A3 ?
    # Example usage5 i  |. h* g0 n3 B. a$ a3 C
    print(fibonacci(6))  # Output: 8" ?6 V: |  t/ v4 R, T

    ! v8 |) B$ d1 LTail Recursion3 [% }- r" v; M

      _8 `0 v* Z9 [# \1 JTail 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).
    / {( {- r, Q1 b0 w( F4 \" ]/ d+ u1 a  u+ T
    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-5-31 16:50 , Processed in 0.057948 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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