设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 / Z; p0 D8 h* p7 B. i1 O% P; u
    # s6 h# Y, T9 Z* b; L' i/ x9 \
    解释的不错5 P$ k3 Q5 G9 w, ?$ j  R# X: N
    " Y- U; o$ @! K2 L, F" I
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。* _" f/ U0 c" u( t: F( C  c( [6 S

    , S. o) S& U1 h' B2 g% b8 H; I; M 关键要素
    2 L" j% y+ {) @5 r4 r' |1. **基线条件(Base Case)**) m. `2 s+ B0 G- l
       - 递归终止的条件,防止无限循环
    ; S& @) {$ A4 t. H( n   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    + c! s: l$ ]  C) Y: C
    ( W8 ]7 _. y8 ^/ n3 ~2. **递归条件(Recursive Case)**
    . t8 Q' z7 @* p& \5 h; \   - 将原问题分解为更小的子问题
    $ b6 ?& x7 C6 e9 Y   - 例如:n! = n × (n-1)!' \1 s# N0 i, a" L8 i- O

    2 b6 x; b8 W% P, U- n 经典示例:计算阶乘
    , T2 w) {5 y( L6 S2 Jpython
    ; a( |$ l) {9 g* e/ X( p: y9 Rdef factorial(n):
    0 h/ \+ W- ~4 A    if n == 0:        # 基线条件
    , G, L4 b7 l: {: H# }2 n        return 11 ^: ]' i, B2 n; y2 Q
        else:             # 递归条件) q! ^! d/ Z0 V$ z7 X% L
            return n * factorial(n-1)% }( @- w* x! h6 y& @9 y6 ~
    执行过程(以计算 3! 为例):
    - ?" _$ O" \7 O6 r6 efactorial(3)
    8 m/ G" V% P. T% \* k. E3 * factorial(2)6 b' X( i) |% n4 u& L. P
    3 * (2 * factorial(1))
    8 D( E% S  b( ]$ G3 * (2 * (1 * factorial(0)))3 y2 i* c4 d  f9 i- H3 C
    3 * (2 * (1 * 1)) = 6
    + e0 d) C7 m0 _- v1 R- `: T" C4 b5 Q$ A$ Q1 ^! d
    递归思维要点8 s6 D4 y3 r, V
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ( {! W$ f) B: i& U% C4 }9 p6 _2 l2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    3 x: }% W( O3 P2 h9 i+ \; c3. **递推过程**:不断向下分解问题(递)
    , l2 g. b/ r+ J4 H0 |3 L7 D4. **回溯过程**:组合子问题结果返回(归)" b) ]) ~/ T  q' ^2 E% R9 S

    " T" Q7 z3 N) J: M# F7 F 注意事项
    1 l9 U3 n8 p# [3 y! S2 s3 {5 x必须要有终止条件
    ' B& S1 p2 R! P& G# Z. B递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ) [: v, f* s' O某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 i2 E) Z+ |( H9 R尾递归优化可以提升效率(但Python不支持)
    ( \; U' C% m: \) y' `
    : ^; \7 u  i+ d1 S" A. l 递归 vs 迭代
    ! x% u/ i+ C! v0 J( `4 m& a|          | 递归                          | 迭代               |. U8 i0 }0 p7 p
    |----------|-----------------------------|------------------|1 W+ r7 R1 O3 K9 @  S. b% u' ]
    | 实现方式    | 函数自调用                        | 循环结构            |
    . t1 I  {; L& C( u| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ ?( x" M$ f# _2 X! f& J. e+ v- b5 w| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& A( D7 i) p( ?! p$ {+ C
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |# ]9 U7 A7 }. E
    7 b0 O) M! [1 O! V9 z2 y# u; y! U
    经典递归应用场景# @2 c5 z% g1 \4 H; Z+ v
    1. 文件系统遍历(目录树结构)
    1 Q$ I8 m! B$ }( Z) X1 O2. 快速排序/归并排序算法
    " Q" H6 t8 B* T8 p/ Z3. 汉诺塔问题
    % T+ l9 o1 z0 v; G9 m1 \3 `3 N4. 二叉树遍历(前序/中序/后序)& p' i) v. H4 z# B1 n/ {
    5. 生成所有可能的组合(回溯算法)
    * ]% O6 k$ \% V5 q! w/ g3 [4 Y3 f! J6 v; i! J7 i
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ w) [8 l* h# y
    我推理机的核心算法应该是二叉树遍历的变种。
    2 w0 b. H7 {# e& M$ P7 G4 k另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:7 G' d2 o, H3 ^# h  Z5 p3 }! E
    Key Idea of Recursion- n' q8 p. w0 z, d7 w" q. v

    2 V6 y, j) i( y9 Z3 \: S' z* R. ~A recursive function solves a problem by:6 V- ]8 h% {; L/ Y
    5 w% |+ u& f. a' C7 ~3 x) ]4 n5 I
        Breaking the problem into smaller instances of the same problem.
    + L, b! }1 o* |- f* v) D
    ' k+ Q( A9 R0 x1 h8 n& K$ |    Solving the smallest instance directly (base case).
    1 c2 p. _6 ]) E& C
    ' l0 N+ b$ k3 f  P8 C7 @- \5 G    Combining the results of smaller instances to solve the larger problem.
    0 D# P2 O" Y* }1 w/ Q
    1 g2 p0 m. w; LComponents of a Recursive Function7 }- B, G* g% k2 c+ \
    6 e. g0 e/ S9 P' u" ?: Q! f
        Base Case:$ O( m% p' `- R; o+ X7 n' S' H

    3 [' a% p4 h: w, j! L2 b: f        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.  j* z- Y/ a3 A+ k) P# i7 @

    4 X: l+ L% Z6 ~) E% t8 A( U        It acts as the stopping condition to prevent infinite recursion.) A6 M: l! Z% _% H( m
    ; {% X" ]1 s0 T3 _
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * S. |  L; x  ^5 Y# u% e( E# ?4 H; B6 `2 J  k. m4 @
        Recursive Case:  Y2 ^0 \# ?; i. p' o! l$ u% w

    , D, @2 e9 j3 d  {0 w' Z7 i6 k+ b        This is where the function calls itself with a smaller or simpler version of the problem.
    - B, g+ H( K- |7 w; N+ {$ |
    ! P# w1 y4 H/ W4 Z2 ^        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' z7 @, D* `; J9 c2 j$ Z

    ; h7 @4 K6 s0 A8 h1 U6 cExample: Factorial Calculation
    & d+ v2 l, F, z5 E5 a- r0 Q+ t# O8 k- v8 h
    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:2 l( j% n" Y% ?
    / C" g' U2 r6 H, V
        Base case: 0! = 1
    $ U. P1 p/ `% }  {4 q: a0 `, {" [9 g) o- F
        Recursive case: n! = n * (n-1)!6 f/ ]* j: p9 e+ r6 e9 a
    ' S% s2 I( `( e/ Y! B3 \  c
    Here’s how it looks in code (Python):+ f, F- }! @+ n1 H, g  H3 A& s# N
    python6 P: q9 \1 O1 j1 d( \, \
    ' n2 P5 n  T( r1 U: B* M
    + d1 Q- p* @7 g& A7 }
    def factorial(n):7 b' E9 U# I4 @9 O! L1 F2 @8 a$ g
        # Base case
    8 }/ Q" o* |5 W5 Q( x& n7 n: E    if n == 0:
    6 R% @& T7 e. ^; h8 q* _        return 1; o' n% N: F: a% A9 n
        # Recursive case1 S8 k7 L9 E) s  D1 u
        else:1 J8 v" N6 c+ G* ^
            return n * factorial(n - 1)
    7 s5 e. B0 _( |7 k( O# H0 w- |* P2 d
    # Example usage
    9 m0 k/ M. f2 y4 hprint(factorial(5))  # Output: 120
    2 E0 q/ C- C( h1 ~# ~# v9 v% A; ^: k  G1 h; ~. O
    How Recursion Works
    $ k! f+ s) Q% o- R# A0 R- ~3 A$ X1 s9 M3 {# ?
        The function keeps calling itself with smaller inputs until it reaches the base case.
    % x0 Z: m+ ?2 E5 }6 r' Q- n6 A
    & J+ P* ^, [9 k! [% |7 X" x7 }    Once the base case is reached, the function starts returning values back up the call stack.
    - Z5 i) Z+ k! h0 O6 c, k6 O- b; f; t! i/ G
        These returned values are combined to produce the final result.
      b+ a5 X) s$ g  q# a& @% n! |/ l4 N. a0 E/ ?6 `
    For factorial(5):
    6 y( N7 Z+ p2 y! L7 O% f, a( t6 R' G9 h( Y+ }2 x! R0 [$ ~

    9 T) B2 u; g0 ], ^( U9 v3 |factorial(5) = 5 * factorial(4)! f/ o3 ?7 q3 @( v2 [
    factorial(4) = 4 * factorial(3)' @6 h5 O1 Q0 H; J3 G/ l. L
    factorial(3) = 3 * factorial(2)
    2 z  d. A  w1 y, p% \" _1 D5 C/ Cfactorial(2) = 2 * factorial(1)+ N7 D& A. D; J1 F* w
    factorial(1) = 1 * factorial(0)
    ' _5 a; v1 j6 |  i5 L5 l3 jfactorial(0) = 1  # Base case5 J3 }$ E- k# j# V

    4 I# }; s+ w. jThen, the results are combined:
    / y5 O. q8 \" ~) O+ f. e* }( p
    3 E/ P& j/ @1 n+ x+ y; h- @) h/ ^' M+ Y8 a
    factorial(1) = 1 * 1 = 17 E- R4 s+ r8 `) P/ j/ r  D# `1 b
    factorial(2) = 2 * 1 = 2
    0 ]+ `  X' j- E. R# ofactorial(3) = 3 * 2 = 6
    . q0 [& v* B( t* e( \# P0 Ufactorial(4) = 4 * 6 = 24# R9 B0 M$ g8 t% }" g0 e# f
    factorial(5) = 5 * 24 = 120
    " I. x$ }! h& T5 Y" [7 [, G3 W4 _5 }3 j7 r: C$ n& O
    Advantages of Recursion6 T! k% O4 E( ~+ k2 b2 D* w& F' F

    / A0 v+ @: x) ~' y6 [) B# L    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).' a! n$ }  o/ A& b) o/ ^, U
    ! d4 h! x; t; H: |( E
        Readability: Recursive code can be more readable and concise compared to iterative solutions.( Q. @9 h% N% \/ j2 ]
    + n7 p4 R% w3 }: D. q, ]) g
    Disadvantages of Recursion
    ; q2 x9 R, v$ {/ x
    3 P2 D5 _4 a  p- q: h2 I% v8 C4 N    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.4 e5 ]' y9 \1 ~; J
    ; t- b) l( e( K  Z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).9 g, g4 ~0 `7 ?5 x
    . ~6 F8 b+ x" n1 w. ]/ ]; H
    When to Use Recursion
    7 G! Q9 ~6 {, z6 F) b& d& Y) Q
    , U* I& ^' q' z1 K# f* U- K    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! ~) v' N+ O/ O$ B' {* O
    % Z, h6 d/ N0 h3 H. s
        Problems with a clear base case and recursive case.
    : |6 R4 ]2 N5 e3 Z2 }2 B6 o) L( m- N) B( z1 d0 L, ]2 |( A* q
    Example: Fibonacci Sequence
    . r! l. B- ?8 b' e$ ?! h6 a+ \' `/ A3 J: \
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    , Q, _; V6 H1 t1 ]4 U' H5 J
    " g/ s1 n* G7 b$ p$ X    Base case: fib(0) = 0, fib(1) = 1
    & k" w, z2 f  k9 h+ K5 }; F3 q1 t& ^5 q. U) A3 U" M2 ~
        Recursive case: fib(n) = fib(n-1) + fib(n-2)) e3 t4 u, t  U
    ! j2 W0 s8 m( S6 y, s
    python
    1 D' t9 P# W# W6 k7 w4 x
    % e' z/ l2 M8 j+ k; u# `$ ^$ b* V6 O7 S5 g3 J; T& _
    def fibonacci(n):( a/ h' B. N3 E% z
        # Base cases
    # \& d9 C8 l" v8 u& h    if n == 0:
    $ H4 L4 B& c$ B        return 0
    ! O3 q7 W0 |3 V# N) \    elif n == 1:7 |% k( U' A9 i% Z" C) `2 n3 U9 B- i
            return 1# F" _, F  y( @2 ]
        # Recursive case9 L4 C0 @* y" ]4 y$ I6 [, m8 c
        else:' X! s0 f! H/ ?2 E
            return fibonacci(n - 1) + fibonacci(n - 2)+ l1 ?8 `" w4 y+ I, L3 p

    $ C8 w4 N9 U% z# Example usage
    $ E5 \: s- }0 @' L; eprint(fibonacci(6))  # Output: 8# N$ W0 D0 k( u- E: v! J* O
    5 K9 h! `8 b( k5 p6 S: C) x! p5 S. p
    Tail Recursion. {: ?( X+ y7 m# F. z4 v# h

    : N0 e' N' [! B/ C7 }9 MTail 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).3 ~: E- d0 p: Q. U( ~( ~" r
    : h/ ~  M% X% G- 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-11-21 06:42 , Processed in 0.030465 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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