设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + r& q! G8 B* W

    : T- p- i. J' T0 T0 {7 S& t解释的不错
    & B" A3 }# _* W4 U$ t+ {1 {2 G3 b2 n) q9 G# E
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ! f  }$ v0 M6 S! C8 r+ I" D; L& J5 K2 v! ?2 [# @( m
    关键要素7 h# |) ~" u0 F% ~3 R4 {
    1. **基线条件(Base Case)**
    % b, ?0 r) x" \* m   - 递归终止的条件,防止无限循环9 W; e+ H1 K  Y0 y
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ' a2 q  K7 b0 `; d+ W( _5 b1 ^/ ]- D! N
    2. **递归条件(Recursive Case)**  j6 v/ e$ E3 R8 F5 Q5 h
       - 将原问题分解为更小的子问题, I* [: P+ i6 n- r1 d
       - 例如:n! = n × (n-1)!! D0 p) Y2 ^, E/ e6 b: P
    ; B% ]" x4 n! }7 H+ _
    经典示例:计算阶乘
    ; v0 J' o( Y6 R" n, \python4 V- p0 z, j; D8 H
    def factorial(n):& D! _) Q, X. }5 j+ h6 g" t$ Y# `
        if n == 0:        # 基线条件
    : y. `; N/ s* U. ]- r( E( N) ?: k. Q        return 18 V' Y4 w' O3 ^+ d) r
        else:             # 递归条件
      B" B$ |. T  y        return n * factorial(n-1): D$ f1 M; t7 U
    执行过程(以计算 3! 为例):' D2 ?) r. V# r, t8 {, Q! I
    factorial(3)
    ) N) u) J! S! A3 T8 r3 * factorial(2)
    ! r5 P. ^3 P! `  z) p3 * (2 * factorial(1)). a, F- P/ G3 @% U2 |, f/ z
    3 * (2 * (1 * factorial(0)))
    * O" o6 J1 W2 A3 * (2 * (1 * 1)) = 6
      W# a2 @( q9 X4 T
    7 C, {9 f9 R) C  U2 e5 q* r; V 递归思维要点& k/ e/ T( s# l0 k" `, V6 Q
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑9 `4 M8 i/ X  Y$ ^
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    8 `& u$ T6 b: O7 q/ V- K  S3. **递推过程**:不断向下分解问题(递)" z. N  w1 f, l( J& r  Q
    4. **回溯过程**:组合子问题结果返回(归)
    1 G+ Q2 {* p  U3 H/ L6 T* ?: Y  s" H/ C% [0 g7 U
    注意事项" h( ?. q. h, t7 e1 b; G* I
    必须要有终止条件
    5 ?2 \5 C2 Y! }: _% h2 f递归深度过大可能导致栈溢出(Python默认递归深度约1000层)& j% y4 x  T& S4 S: }9 y
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    : w4 r) p$ K* l3 i% W0 ^尾递归优化可以提升效率(但Python不支持)
    / c0 k( L( j  _- @% H1 |; R1 I' N& r! k
    递归 vs 迭代
    3 n) O/ O) r+ {; U|          | 递归                          | 迭代               |
    ) e3 T1 E, F5 B+ b- b|----------|-----------------------------|------------------|
    7 F( s; p( N3 f3 || 实现方式    | 函数自调用                        | 循环结构            |
    . S( a3 V8 u; `5 Z( e| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 u: ~$ L, F8 @9 A0 G| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * p$ f+ x. ^7 N& D8 i2 v2 R| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! A# P6 l/ J2 J$ h8 @

    4 U8 s9 h" v! T4 Z. X# E* { 经典递归应用场景
    8 Q9 u' m( }7 w4 G+ X6 X  s5 b% R1. 文件系统遍历(目录树结构)
    ( C5 O1 e4 ^+ n, k2 s9 p2. 快速排序/归并排序算法
    $ R' b+ L2 a+ N8 ]+ l9 s3. 汉诺塔问题
    2 d9 y/ o9 @2 V4. 二叉树遍历(前序/中序/后序)
      H7 O! u  Q8 {: E. J* N5. 生成所有可能的组合(回溯算法)
    7 L6 l/ L3 |( Z: q* ?3 v
    1 o9 _+ C9 P* {4 O: [8 `  ^试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 06:37
  • 签到天数: 3272 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,; \" w  s% h, y7 W, |$ @7 X" m3 I
    我推理机的核心算法应该是二叉树遍历的变种。
    2 h$ q0 @; J, R+ A! {  s. h另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 |" s# Q9 k( @/ L1 {Key Idea of Recursion
    ! H! p" a/ Q8 I% F* @2 G
      H- t( ~# c& X0 c$ ?4 X2 JA recursive function solves a problem by:
    % S' N0 |' L, ?( F( L7 y) H! @' h6 }. X0 Y1 H- n1 m
        Breaking the problem into smaller instances of the same problem.
    ) d6 y9 N8 e1 G0 O2 z  Z/ U# T/ w
    ) u  |! Q% O1 i; H# n    Solving the smallest instance directly (base case).7 [$ s+ p3 K; n& Q1 I
      @5 t* Q+ v8 L( {& r" V4 R9 M
        Combining the results of smaller instances to solve the larger problem.
    - [% w! K3 H* Q% _/ f
    * @( |9 [1 Z. N1 \% J# W* o, mComponents of a Recursive Function( Y7 C5 y' g+ E3 [% N' M9 {  H, x
    6 w, p, f+ d, ]# b
        Base Case:
    . C/ s2 L) C" {3 v, E: `; X  J; C# A
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & }0 h$ `# M# T; h9 s& M# ]# S
    , H& `. F+ x9 r$ T9 v) _, n        It acts as the stopping condition to prevent infinite recursion.
    + S4 y5 u3 \5 {8 ?* P' z4 T7 z' l+ E6 \% `5 L8 d5 g" ~: H9 M
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.1 [( r4 c8 J" L" ?! A9 J4 U9 K) ?

    . V1 Q% g$ g3 x( y2 ]    Recursive Case:
    * u% R2 V# B) T8 K( q9 r; n, ^6 {0 l
            This is where the function calls itself with a smaller or simpler version of the problem.; [# D$ X% s6 @+ w' v  c

    8 o. B  I% C" ~( O- |0 W% K        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    % g% }: ?5 q8 S5 V+ E, X' I# O4 U7 Y" W/ q" I& H2 K
    Example: Factorial Calculation6 T0 d6 T7 {3 ]7 \1 Q" M
    0 l6 \2 K  x- @" J7 K
    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:8 s- _6 \. }  }& q" g# Q

    & ^# [3 h* T# y    Base case: 0! = 1" `5 H  X: |0 O3 M
    0 {, H# @) R- Z, o- Q$ W3 {
        Recursive case: n! = n * (n-1)!
    0 T& _- H. D3 A$ t0 M- R" a7 S! i% K& z; v( ~; ~0 T" z8 _" s
    Here’s how it looks in code (Python):4 {' `* O% O& B4 \; y6 }" h
    python
    ! s. Q, _$ y+ g
    6 z  t6 Y4 u$ t" J& S# L% P) b# n, b+ h! S) F
    def factorial(n):
    3 v. ]1 Z, I0 p' w    # Base case
    * ]# G# f/ t( g' H& s" O    if n == 0:
    9 T' ?2 ]  P% t( l/ c2 q% {        return 1
    % w0 d( N. b0 ~, h' q: k8 r4 ?$ ~    # Recursive case
    9 V& }9 i8 L; U4 \% c    else:
    7 a- {9 [9 |0 k/ w& Q& s7 g9 \        return n * factorial(n - 1), ~1 `& D$ ]2 L5 r# b
    . W. t  A) a( N. h0 `
    # Example usage& e6 e* ^2 T7 ~5 E$ o: d! V5 E
    print(factorial(5))  # Output: 120) J3 C3 x8 i6 u( ~8 p2 o" w
    0 f0 O: {$ T  m7 Q0 S
    How Recursion Works
    , E0 O, }# C) J1 T; l
    5 R1 {" T! n# E: x5 |" a# O    The function keeps calling itself with smaller inputs until it reaches the base case.
    / m5 e' P1 q! u+ H
    $ u/ a" S. U' W- }7 D4 o    Once the base case is reached, the function starts returning values back up the call stack.
    1 H% x9 j. O' ]  }" ^1 L8 C2 s
    4 q8 m* N- Q1 ?4 ~+ K+ s3 T    These returned values are combined to produce the final result.) u) F( @/ k8 R% L
    0 L( c( c, p5 e9 D* S$ _& B
    For factorial(5):
    + a' q+ }; P# K( l* P, E6 B* b9 L" P

    9 w6 {% C5 W% x8 |& q4 R  zfactorial(5) = 5 * factorial(4)
    + K+ v* ?1 U9 ~, p: W, F8 xfactorial(4) = 4 * factorial(3)# z9 s  {6 A+ x, g
    factorial(3) = 3 * factorial(2)" ^; G. N  @: o7 d- l
    factorial(2) = 2 * factorial(1)& `6 G9 S; i- ]  a
    factorial(1) = 1 * factorial(0)
    1 b: {/ h$ G) A+ t; C* `0 Efactorial(0) = 1  # Base case
    4 L: o) I- ?! }5 d! R% i; H* ]* a& X( s  ~$ M# j5 r
    Then, the results are combined:
    * _2 u' j! Z* V: @. h: N. T" ^
    # ]/ g# ?- \! V/ A2 }0 i# A
    factorial(1) = 1 * 1 = 1
    ( g# ^, `" E4 K8 j5 t# V" Gfactorial(2) = 2 * 1 = 2& V' O8 k, C0 [# _
    factorial(3) = 3 * 2 = 6
      B/ z, D* h- t7 u, D9 S( Mfactorial(4) = 4 * 6 = 24
    0 l5 k6 M. Y2 q8 Tfactorial(5) = 5 * 24 = 120
    ; J0 S3 X* R8 ?! C6 R' Y, b3 [5 C& j+ ?, U( k. b
    Advantages of Recursion
      f- ^5 A2 V6 s/ B, H; Q7 j) h& y2 l) V6 U: o7 o+ \' C$ F
        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).
    5 O1 D4 I, a2 ?" M0 u4 Z( ^* @0 [3 [$ ]; W4 S3 W9 c
        Readability: Recursive code can be more readable and concise compared to iterative solutions./ w5 A" h) l" Z. y; l8 \9 k
    - c$ M: i% [4 i! ?6 P- F
    Disadvantages of Recursion
    ' {6 I' `+ m: E9 M  S1 U3 T4 W0 [" O
    8 N& o2 `. |* M% [2 M    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.. u4 O6 w% s& N7 Q

    ' K2 L5 Z: o- t* G) Z    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ; B* F) I, V/ B2 c' A/ L# x' V3 c9 H
    & p; w$ A. y2 Q# s" u! Q. rWhen to Use Recursion# T3 z1 w# l+ U

    % E7 M/ g  z/ j) X    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    3 B* R) j. J! i  d$ p$ W7 ?; _) b4 M0 Q% \* i4 Q
        Problems with a clear base case and recursive case.
    * v7 l7 v; x# W" [2 Q; C& O4 h) x0 D' D4 W: G
    Example: Fibonacci Sequence2 z, L: ?4 E9 C
    9 ?$ x! h& J2 a9 }2 ^% @8 a- r
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 S, Y, i: [% g  d  r" j* c& E* G0 q

    * ?8 O: T& m# y: g5 k" b: S+ ?    Base case: fib(0) = 0, fib(1) = 1+ A8 X+ p  X$ v$ ?8 M4 d

    : Q6 U7 t0 a# Z$ J% a  l    Recursive case: fib(n) = fib(n-1) + fib(n-2): o/ N! }' Q  n* T
    : _" u4 }" D! U
    python
    / |$ o6 m/ M7 U: p6 v
    7 U3 D: n( C( G' [7 _3 i8 a$ C$ ]) R
    def fibonacci(n):3 D5 X4 R# l1 e8 R' P9 E% E
        # Base cases
    ( h2 j0 y7 g: x; u, O  `- E/ X. v5 G    if n == 0:2 c) U9 P8 g* K- T$ M4 l
            return 0: q" |. j" _1 K! ?3 ]) v% N
        elif n == 1:, q8 K; y* X9 V* H4 _4 l+ A/ z
            return 1
    3 N/ v5 f8 P5 ~( H. |. ~5 a& H' @+ X# W8 E    # Recursive case
    ) V, [! N( a* v; A9 Y    else:
    . U: N1 o9 p, Q& j0 ^7 G7 {        return fibonacci(n - 1) + fibonacci(n - 2)
    7 P; T& O- s5 k* k& ?9 d! e" a7 I7 U7 t- {  q: E( h/ y+ k5 O4 r
    # Example usage
    0 v3 W0 [, S; `% qprint(fibonacci(6))  # Output: 81 |3 V; F& E. j1 q' f3 N7 Q* O, k
    ( I, C+ A: J) i& K: r  A
    Tail Recursion
    ( ~# Y  f9 J3 j! G. U/ ~/ q  C+ e+ v) o  y
    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)./ {- @, [2 G7 a( u( ?

    * \' m3 t5 P/ 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-6-19 02:54 , Processed in 0.062022 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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