设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 / Q( j/ N+ T. j$ z7 I

      v& d3 v, O# [解释的不错
    " ^: q4 m/ q; |9 _7 W" E$ g9 M6 `" A7 [: t
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & h' b# t. R: B& h6 a" G7 p1 q, [% p9 i% E3 B% @2 H6 \& f( l
    关键要素
    " e- b( E* Q6 p1. **基线条件(Base Case)**
    . r& \+ F0 \) g" E& N   - 递归终止的条件,防止无限循环
    & _7 f! z, Z7 g+ K: ?. Z$ E  H   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ |( T) o( R% N
    ' M/ h( a% N" d6 m
    2. **递归条件(Recursive Case)**
    7 H9 r- H( E" F   - 将原问题分解为更小的子问题- A1 \5 T% N" P) I/ q$ v5 q
       - 例如:n! = n × (n-1)!
    * C% l' C6 f- F
    1 q* F3 `! A- A" y& m7 D" T" ` 经典示例:计算阶乘
    & P$ W: a; y3 _, h. V, F2 K. npython
    3 t& r& B5 k2 D$ D" P. [7 P; ?def factorial(n):
    # N* }; _7 V5 w& p# b6 m# f    if n == 0:        # 基线条件
    8 m; g% A: |7 y: T. @- A        return 1
    ' v) l/ g3 e$ a+ @# ^1 h    else:             # 递归条件* Q5 V# ?. Y2 g5 X
            return n * factorial(n-1)1 b5 o) R7 ~" U. ?  V7 o1 ?
    执行过程(以计算 3! 为例):
    + A8 f. W- i- _& Xfactorial(3)
    , R, M9 |8 L6 E4 H, j, i0 A- j3 * factorial(2)- v7 d, x5 q2 E0 n( ]
    3 * (2 * factorial(1))
    8 n! \; e  k6 Y! H  m3 * (2 * (1 * factorial(0)))
    + J/ _2 R5 S/ X5 ]6 F& j! G3 * (2 * (1 * 1)) = 6
    % |/ R2 g+ o# l" O0 V. i6 M
    7 m: p  }" z* K3 e! x. e 递归思维要点! T# I9 K4 t+ e* I( R7 A5 A
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    : n% W& @* u: q/ R6 v$ Q& Q4 W9 O1 S2. **栈结构**:每次调用都会创建新的栈帧(内存空间)( S8 ^$ j- I, u( |3 {3 M
    3. **递推过程**:不断向下分解问题(递)0 ~7 H' r$ |; x) R8 N1 {
    4. **回溯过程**:组合子问题结果返回(归)
    - G8 U( H: w$ ^7 C
    % c. T: o; x2 I7 i6 p% d8 s+ ^ 注意事项% X3 l( ?* ^% `
    必须要有终止条件1 W# @" }* j# L" d
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 s" T4 T6 E. j; c某些问题用递归更直观(如树遍历),但效率可能不如迭代0 T; j' F: {( V# _- k! i
    尾递归优化可以提升效率(但Python不支持)
    + D4 `" C, `( ^; k; ]5 T; g
    ( _8 Y4 W$ R5 m* [$ W! k 递归 vs 迭代
    ; q# Y$ A* G2 k  F! L+ P  n. g|          | 递归                          | 迭代               |$ C& p) a, a' F/ Z
    |----------|-----------------------------|------------------|
    + ?- O# z: m% p! Q| 实现方式    | 函数自调用                        | 循环结构            |% l/ a9 o! W+ y% I- U( D
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ! g! C. K- [. Y% Z, [* g, F| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    - l! p5 z4 X- B* v| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 b- v5 o! [1 o; x: K) V
    0 m$ L; x5 G3 M9 U9 x  W# ~ 经典递归应用场景4 t( t4 P7 l' E4 h, l# C
    1. 文件系统遍历(目录树结构)8 [% @9 J2 l+ b# \# W! F! @+ J! x
    2. 快速排序/归并排序算法
    + w! P3 ]4 N; }$ l: H6 N) V3. 汉诺塔问题
    & g5 F, y7 X. D, E  y& ^  {  P7 r4. 二叉树遍历(前序/中序/后序)8 |2 g$ J3 R% T* C7 C$ E% B
    5. 生成所有可能的组合(回溯算法)+ r- x4 U0 I, ^

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

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 06:19
  • 签到天数: 3163 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# L6 ^5 V/ s( D( y; v
    我推理机的核心算法应该是二叉树遍历的变种。/ l1 k% W' ?- j
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:/ g8 c# f( l0 K. \' G
    Key Idea of Recursion
    1 k: W5 G5 n5 {. j
    8 P# ?8 w$ G2 {, b1 RA recursive function solves a problem by:
    6 o- d4 w9 p" F" t# M/ p+ C1 w
    7 w# J* p0 x6 B: {/ w$ F0 T) X% {    Breaking the problem into smaller instances of the same problem.
      ~1 N8 {! |9 V) s5 R6 Y- q) c% O2 W* g* G) K/ N: d
        Solving the smallest instance directly (base case)., w0 f0 O5 V5 Q- a0 H

    ; y. ~1 ^- P+ b0 b    Combining the results of smaller instances to solve the larger problem.
    ( B0 v& }( L# G" N4 G* K4 q) d
      z9 d  Y" M) |! [, qComponents of a Recursive Function
    ' q6 C! x& v; I5 R/ I9 a; M) x/ g
        Base Case:
    5 e9 w$ q4 Q6 v: @" P1 r
    4 K% {; W9 t/ x/ ^        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 k2 |1 `& u( A. a6 V. v

    ; ?# j9 ]7 `' p        It acts as the stopping condition to prevent infinite recursion.
    0 |5 u1 T  ?9 N. T7 _
    7 ~7 T$ u! \% x' P, Z" F& e        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.7 s6 X  Z; r) }  E$ ~4 {  Y$ j

    , v0 H2 N6 _7 Y' d" p& r8 k0 w# x    Recursive Case:5 q  ~$ ^+ [; D' S! y& W3 k3 A% h
    9 A6 Y: u- k. Z- X) a
            This is where the function calls itself with a smaller or simpler version of the problem.. q% X  Z% V7 W" F

    0 k  s. r; |7 H  f) `        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 Y" _" n( J: B2 T  h. t" b. l* v4 O  X7 M' F; N$ P# S3 T9 v0 k
    Example: Factorial Calculation8 k/ z  v0 J5 w& I9 ~

    8 \0 j# Q3 G# z# B( l' M4 CThe 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:
    + t: I, M+ q. l0 }! M$ f
    4 p# G$ Q6 [% q    Base case: 0! = 1
    7 c! U( C, r5 x0 H9 p0 ]+ k# i; z/ U5 _% K' ~% {
        Recursive case: n! = n * (n-1)!
    / P) q+ u0 e3 u- k: F  g/ k" n7 J
    ' I* v& C( o6 ^- O( ~Here’s how it looks in code (Python):
    * l& F0 {% V0 G2 t/ Epython
    6 `' x: n( h. i# |  _! I( E' c% V: e0 R. n" e  {
    3 y) U9 [8 ~2 p$ k& O$ h1 L6 k/ g
    def factorial(n):
    ( @' R4 B9 Y3 I$ c* B    # Base case
    ) C  [8 }6 K$ U; z; h    if n == 0:
    # Q4 `9 R- U) F; }* R0 ~7 q" X- {        return 1
    ! K. {9 L) N0 T    # Recursive case3 I8 i: O) e% K( ^1 M
        else:& k) y' ], o7 Q, [3 X& r$ ^
            return n * factorial(n - 1)( a; ~; b( s& R6 A# O* R" f
    ' K$ k& n, F# T& O  j9 l, l
    # Example usage
    , U% L1 W# ^6 vprint(factorial(5))  # Output: 120
    " S- K! U* N* u% A
    # \8 k9 ]; Q$ v. o  `9 dHow Recursion Works! L7 L- b5 m0 i7 C0 t2 C
    $ x- e. ]* U- L$ z! C) _
        The function keeps calling itself with smaller inputs until it reaches the base case.
    % l7 [( G" ]& }1 k
    9 ], \# t2 \( h& e2 _    Once the base case is reached, the function starts returning values back up the call stack." o4 |. O9 _/ H( }0 L# o

    / c) M& V( _7 \+ j9 ?- _, S1 L1 {    These returned values are combined to produce the final result.5 x' C3 U- m1 P3 p# K6 `$ ^% ~8 `
    # j+ ?1 r, e* D5 p6 V0 J+ x
    For factorial(5):
    4 a  D5 C" e( k7 b; T6 l1 G8 c* t8 ]( G0 E

    5 q5 B+ {& h- l4 E# t1 Tfactorial(5) = 5 * factorial(4)
    - ^) D$ c6 g# t8 z* tfactorial(4) = 4 * factorial(3)7 E7 o; z) I0 e6 E. P
    factorial(3) = 3 * factorial(2)( i, H+ R& Y: Q) J/ j8 s
    factorial(2) = 2 * factorial(1)
    0 Y4 ?; ^. f0 p7 `factorial(1) = 1 * factorial(0)1 c+ L3 E0 y4 C3 Q1 Z  r" Y3 p
    factorial(0) = 1  # Base case
    8 _9 P- l% N) y0 F# m) F6 d# r, D, l: p" r  z
    Then, the results are combined:( a0 ~6 C. e3 Y2 |3 F$ @

    3 M7 \; L' E2 m1 n+ r4 _2 j# G& K# P% w) M. D4 Q/ n
    factorial(1) = 1 * 1 = 1
    , J- m! e3 [4 w5 Z0 z% rfactorial(2) = 2 * 1 = 2: X- Y8 ~3 R7 B
    factorial(3) = 3 * 2 = 63 a8 E% M) r/ P5 M% s
    factorial(4) = 4 * 6 = 24
    / |  _; N9 M$ X- d/ f9 a' Ufactorial(5) = 5 * 24 = 120
      u" M0 {/ J1 k$ H
    2 p. K% d7 }% e8 f( T& gAdvantages of Recursion
      w6 S: T+ L0 L8 w
    - q  ~5 x& c2 q- _+ X9 X    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).% y3 G( P9 B# N8 _/ ?0 }: |3 u

    + `' ^; R* _8 r/ C9 z    Readability: Recursive code can be more readable and concise compared to iterative solutions.9 l, C+ j, A5 F# y
    8 t4 i/ H" S% I6 u' |
    Disadvantages of Recursion
    ! v) V# a$ b0 }1 S8 E9 B) n* A
    % O5 R$ E$ \  h* W2 O4 G    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.) C0 ?4 {  v# Q, Q% U, o. q
    9 \& K2 W, H7 h9 `9 ~) @5 c
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . c% K9 R0 \4 ^5 v1 X2 [/ Z) w* H% R; a; [4 |
    When to Use Recursion
    ) G5 Z8 Q/ v6 l- \4 J" @9 h( s" W4 R6 m9 {/ J! `1 r& v: L% F; I
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! M$ m; j4 ~9 N! |- d( X' b$ G

    * x2 C% P6 `1 @$ _& m0 {+ }    Problems with a clear base case and recursive case.
    ; _9 P0 x5 r" _, d) X$ Y, _% v$ a% x% v3 k$ u
    Example: Fibonacci Sequence$ s* Z5 f, x# l# _' b4 _( A1 w
    5 v' x4 l, k; l/ g
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 A( ^* d. Y8 H# V3 K  J

    " N% ?6 L% y+ v4 \9 q0 k    Base case: fib(0) = 0, fib(1) = 1$ ~7 G8 L& l' C4 Z  g  I8 C; @. H- Z

    % {9 P0 V$ F1 X7 @# t    Recursive case: fib(n) = fib(n-1) + fib(n-2)/ r0 l4 E, R) \! U" H
    * i5 y* p  }( u8 i7 A
    python
    $ n9 U  H! a, U4 u1 x9 p, e# b# i1 G
    & G% S2 S6 k3 R0 L% A
    def fibonacci(n):
    ' {, c5 g7 Z" P* h6 m3 d  O+ {    # Base cases1 a: A& u# c2 y5 n  Q, |" P
        if n == 0:
    " ?9 m- j$ |: ]& C+ K2 I+ ?! m        return 0. ?) i) E. I* I: L( u( S
        elif n == 1:
    2 _( R. N$ r/ {0 {0 j        return 1
    3 e2 }/ R% _* {$ n3 X    # Recursive case8 R7 N- ~  @$ m# _) Z) M: s
        else:- U% L1 U# i- B2 Y; ]
            return fibonacci(n - 1) + fibonacci(n - 2)
    1 t  L3 Q- t/ ^4 J  M, D% h- p
      O: D* B, v( h7 {# Example usage  w) ^4 `  k' ]
    print(fibonacci(6))  # Output: 8
    9 }! D1 }8 v$ d9 T' n* H+ J3 i2 C; `! z% t4 r
    Tail Recursion
    / D/ g3 F* O  x4 \2 _8 x4 ?% i; t  z( M6 T
    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)./ @4 w4 h* u+ B% i- D6 }  {: H
    . |. Z2 W! b6 b; p8 n2 y
    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-2-5 03:09 , Processed in 0.057902 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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