设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 Q, ?1 q! R" j
    ; W1 F  Z' R6 h1 W: H  W
    解释的不错! q7 |/ N& R9 p* q. J" W
    - Z5 H2 @; @2 ^& T
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- J% O5 Q/ `- _# L) l
    0 B- T4 T5 T8 A1 S' |$ ?$ _
    关键要素
    2 A+ N  c- A  r% l  i1. **基线条件(Base Case)**
    : M8 ]* v) u; ]0 m% D! V   - 递归终止的条件,防止无限循环
    % z2 C& W8 E+ i   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " w  a% L0 t. J& a" M) n* h3 L# Z5 ?
    2. **递归条件(Recursive Case)*** V6 E& d1 x8 [: D
       - 将原问题分解为更小的子问题) O4 t0 x3 g% y7 _+ `6 `! \
       - 例如:n! = n × (n-1)!5 r+ L7 h% Z( s' {* ]1 c
    2 w9 Q2 G3 ~8 b( D# y6 K2 V( Z
    经典示例:计算阶乘& ~5 X% ]  s( c4 L( r. o; O  S
    python
    # P3 b  u# ~2 z, X  W' q" f# ddef factorial(n):
    " U: x) B/ l& \    if n == 0:        # 基线条件! S+ m! E- a! }+ R6 ?( O6 F
            return 1: j3 Y; o+ V+ G( V: j( g6 U5 B( G$ N
        else:             # 递归条件+ s5 X( {- o7 d  z! s3 \
            return n * factorial(n-1)
    9 F( I. `! I. }1 h$ G6 v执行过程(以计算 3! 为例):
    ) |; g2 j! e; t3 [1 j; X+ Ifactorial(3)
    1 d) _$ x* \4 _7 d: f3 * factorial(2)
    8 y% |: v/ J9 f7 h3 * (2 * factorial(1))9 B8 ~/ P; O$ Q' ^
    3 * (2 * (1 * factorial(0)))
      H* }$ c2 L& l. G' u) j3 * (2 * (1 * 1)) = 6% G  T2 g% N5 h

    ; \2 [( K: m- B+ H/ h' [5 n 递归思维要点4 m$ `: p) e# a. r2 ~* ]* y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    6 D0 l8 `! n1 Y1 A( H' W2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    / o4 ~# n; V$ K# O! l/ o3. **递推过程**:不断向下分解问题(递)4 L0 d3 s$ B: K1 p8 L, \
    4. **回溯过程**:组合子问题结果返回(归)
    " G2 A+ Q) |9 ]* u
    7 [3 Y) G5 V6 |: y2 A8 [* v 注意事项; g& M2 P& ?+ F: G2 S6 x
    必须要有终止条件
    1 c  ~( S8 G) V( G5 T9 f- h递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ s) y7 {+ A5 P% S" f. e7 z9 J
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ! W7 ~* s. a) w) Y尾递归优化可以提升效率(但Python不支持)( S) b( M8 j" D
    ( N' t  R2 f' ^
    递归 vs 迭代* J: m1 D  k7 t5 [# r& _" t* C! T
    |          | 递归                          | 迭代               |
    # L' I3 C1 @9 P+ p& x|----------|-----------------------------|------------------|) M8 [, \- w9 X5 T. w
    | 实现方式    | 函数自调用                        | 循环结构            |
    1 C, o0 v! C8 k% ^; r| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 f8 c- F3 P0 d; z0 o( Z# v
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ( q2 i0 ~  m1 e0 ~. ]# q| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ; m9 i/ f2 _: p4 U) e$ v, L
    - w9 y3 e: I0 t$ R 经典递归应用场景
    & Q- [) e  o- u( U+ m1. 文件系统遍历(目录树结构)
    $ W: L+ y8 ?+ z( H5 z% Q4 @+ L+ Y7 o. T2. 快速排序/归并排序算法
    3 E' e+ y# B: i) w$ v3 E3. 汉诺塔问题
    , Z* Z: W+ U. d+ l4. 二叉树遍历(前序/中序/后序)
    7 S3 P3 w' A. T+ t" X! l5. 生成所有可能的组合(回溯算法)" \* |, G8 H, H* p; c3 g

    - m) ~% S2 s3 t1 U" C4 O+ O# f试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    8 小时前
  • 签到天数: 3123 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,- a4 P3 _4 q& N' {& V( t
    我推理机的核心算法应该是二叉树遍历的变种。* R! e9 p; ^) t0 Y% ~+ L* 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:
    - t: u/ A& _% yKey Idea of Recursion
    4 ]0 I4 L6 @/ c. |; q& @. c  P
    ' q7 r* `3 C7 d" qA recursive function solves a problem by:# \9 l8 A2 @) s' w! c) X  x1 p  M
    9 K, ~* \; D, |+ Z
        Breaking the problem into smaller instances of the same problem.
    ; a, u- j! l" a0 \, ~9 y
    + H. T9 x# P& O" b8 G1 [    Solving the smallest instance directly (base case).9 t8 s4 R  y# w/ e
    4 I6 g' q; _* p. v& ]' y
        Combining the results of smaller instances to solve the larger problem.. Q% d# V8 J2 k( H
    * X& I" e" c6 `4 T6 \7 w" v
    Components of a Recursive Function% E% _+ k7 f5 n1 |/ u' r
    * f3 Q& t- m. o
        Base Case:: i: e( R. d$ x; S
    9 I3 t& A. D* G1 D& Q: ^5 r
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ) k9 j7 s, b$ B, D3 }0 @4 e6 t+ I, _0 @, U8 [' O3 r0 ?2 }
            It acts as the stopping condition to prevent infinite recursion.
    ) e4 o7 @; Z" A2 K$ r3 d0 i9 C% N- v  Y$ _
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& L* o/ L5 S+ t. O5 ]: D

    . s3 k. g2 r0 C! D    Recursive Case:3 W5 v9 C4 f0 w$ u5 p! _; B

    . e% J3 N! s* M% @8 o; l6 y: A' w        This is where the function calls itself with a smaller or simpler version of the problem.; f+ z/ x0 F! W. g  @

    2 ], h9 D8 A  V5 G, J* _9 v% [        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    9 r: \) [, F. M' {, ?- s: Q& m7 W  w" a# g; G
    Example: Factorial Calculation7 [* |; L& @: e! f- g2 L* R  I+ a

    ( X% m' d2 ^6 l9 h, D3 LThe 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:
    - f+ s/ K' `# I, [% o+ v( I' h9 U: C5 D# y
        Base case: 0! = 1
    9 ~# U8 Y/ n5 W" W. ?# O* r6 g1 D: I- s) |
        Recursive case: n! = n * (n-1)!4 t' N% V: S1 j  L/ x
    % b$ ~# j+ p4 S0 l; ?. ], n
    Here’s how it looks in code (Python):6 O$ @2 Y' g* h$ J8 Q
    python0 t) g% J2 n- T% S+ j' g  I
    ) @5 o* @0 k& z/ g! w5 n
    4 O: Y$ }& ]5 S! W
    def factorial(n):
    : e* u( V& @# b: i    # Base case* e' b# y9 o) n) ?9 Q4 ?  ]9 I
        if n == 0:9 P1 Q+ q' I% [1 s4 R. S/ P1 U/ e
            return 17 h; h) ~/ K' ^5 r6 _; s8 w( @/ f
        # Recursive case) K1 `6 ]. Q; n/ v5 O9 z
        else:
    " @1 Z& q; o9 I. z, S: P7 v+ z        return n * factorial(n - 1)5 k( |/ Q8 @! d; F6 g

    * }2 S. L0 G5 \3 ]2 h! A0 G) E% B7 s# Example usage: I, Q( P9 ~' X
    print(factorial(5))  # Output: 120
    3 A' e2 \; Z; K) m. U; B# |/ r3 N, Y
    How Recursion Works+ {" }' M+ [8 c% W. `4 R1 x

    : `. G! C. J6 x% C  {! g2 e    The function keeps calling itself with smaller inputs until it reaches the base case.; Y, D- E0 g/ |" U  N0 l4 _
    ( N$ Z, p: C. t/ ?
        Once the base case is reached, the function starts returning values back up the call stack.
    2 `$ D& Z7 K/ x( [9 O7 h" o3 t
    ! Q  B6 B1 W7 S7 C/ U8 T( W    These returned values are combined to produce the final result.# u) q% A0 F  d) B6 x* _
    1 D+ I: z, v  ]  g) l" R1 z
    For factorial(5):
    6 t' I% s% }0 J% W$ J. v* e) {, b* \$ u# X% d* B9 w

    - }1 M+ K* k* X9 L+ r2 k5 Jfactorial(5) = 5 * factorial(4)% g' e5 y; f  X) Y/ {4 {3 T: v
    factorial(4) = 4 * factorial(3)( @) g1 X# d5 x2 l9 J, D
    factorial(3) = 3 * factorial(2)) i1 O- F; P: w
    factorial(2) = 2 * factorial(1)
    ' Z7 m! S% N3 S, |' Ofactorial(1) = 1 * factorial(0)
    ! {0 k" @! o/ I9 N  Z  F4 p4 ]factorial(0) = 1  # Base case
    3 J9 A0 p0 Y% v  ~8 L0 V! t" m
    ' d7 W0 |3 k7 _; {6 gThen, the results are combined:; `! i8 |: S( z
    + _: T$ z/ `9 Z0 Z$ f/ }

    3 H- y# N' ?. m  O0 dfactorial(1) = 1 * 1 = 1
    3 W" V0 a1 _& a8 j  G/ \7 Gfactorial(2) = 2 * 1 = 2$ I- J; }& A/ N9 y8 s8 q
    factorial(3) = 3 * 2 = 68 [' y5 U+ G: U0 b3 [8 J4 o
    factorial(4) = 4 * 6 = 24
    ; g, W* l2 L& ^+ R# Pfactorial(5) = 5 * 24 = 1208 E9 I3 a2 U3 Y- H  A/ k8 t

    5 a' E7 h2 U; t- F7 kAdvantages of Recursion
    2 P" Q% R" z9 O; X
    8 D0 M$ p, Z& G7 E0 K2 a    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).
    6 K' g- K# A0 @9 H+ a$ o" Z' v# g) c8 A6 f  G
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - B% z* g9 N4 O) D* Y4 d0 K% o( p5 ]. `3 D9 V. X
    Disadvantages of Recursion6 @- m! t4 Y+ e7 O0 C) O) P0 U
    * v7 e9 m) v7 X1 z% y% m) l
        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.- N+ n! T" {* m% A% d
    ; N/ W, y$ ~, k
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    7 q( f, {; C) p- r' L7 Q1 o4 w- _9 @( N! E: Z) l
    When to Use Recursion
    * @8 c! b0 G! _
    ' B, D6 m: _  v, o7 c, L4 t    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' s4 N; D3 E" D; f% E+ I: u

    # h- d' g& S1 C1 N8 w+ G: y# f2 |    Problems with a clear base case and recursive case.
    4 m. Y  ]1 E/ p& H
      K9 X9 \7 r0 ?/ B" t) jExample: Fibonacci Sequence
    ! P$ A$ k6 t, v) E0 w# W& B
    5 o- a. k* e# j" X8 X3 P5 o3 }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:2 m& q+ h' v8 N  F: Y
      V% B8 Z7 S2 V/ Q
        Base case: fib(0) = 0, fib(1) = 1
    8 g4 X& T: N3 k) W$ u
    / m, K9 \5 v. |7 j% N- l$ d    Recursive case: fib(n) = fib(n-1) + fib(n-2). Z/ m% O5 k5 @& v4 D

    , p) x& O3 L& L, m' ^python+ V4 X3 m, W7 J0 J" p- g# [, C

    7 |5 ^) E& B( t+ T) f" S% D, u! u
    def fibonacci(n):- Y. y- c, ]# n6 }6 y
        # Base cases
    * T, Y5 g6 ^: z7 e: K& Q    if n == 0:& ~' F( Z. r. P
            return 0, ^. b( ^2 J' q* y4 R
        elif n == 1:$ {' N- E7 J2 Y2 ~) a! Q% n
            return 1! _2 G% P" K: w1 |+ V
        # Recursive case! S: w6 H1 _* a% \
        else:
    3 L' ]: ~2 U8 }4 E        return fibonacci(n - 1) + fibonacci(n - 2)7 l. {; O, E, \5 B+ {7 u( u

    - E" j7 L6 c# z2 n( w7 }- s# Example usage. O  ^# g7 _& h  @# l4 D
    print(fibonacci(6))  # Output: 8
    4 `: N, M- |: F6 V( p6 D
    0 [$ n7 f: c4 s- RTail Recursion8 N, o) E2 ~+ d3 B* c, k4 \2 \
    3 W  L# M( @' H/ Z
    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).
    ( h7 g9 x, r7 \  d  v4 G' w3 ^; b+ r9 C' h
    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, 2025-12-20 16:35 , Processed in 0.030330 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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