设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / L% d. s: t, c2 U
    ; `* o4 S+ e6 D) S解释的不错, c4 x! O" G% x# _& B; E
    / |+ S5 z/ m9 W5 d5 B
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & y& V2 G* F! @1 |
    , ?% G( p. K8 u. S: c2 [ 关键要素, _1 H7 L! v9 a  c5 j: P% ?5 R1 n
    1. **基线条件(Base Case)**
    ' S& Y1 T+ Y8 a# Q   - 递归终止的条件,防止无限循环! C  d0 |1 q! v/ ]/ _
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 b) x5 n8 Y2 p' g$ o7 V) E' V9 X
    . z: {& B7 `1 ~: t6 f0 i* @, m
    2. **递归条件(Recursive Case)**$ W' I4 K- a5 v* I9 o  w+ u
       - 将原问题分解为更小的子问题
    & M1 B- d6 D3 i   - 例如:n! = n × (n-1)!
    * f# M5 a# t4 A% p  S8 r( a. N. v  L( `
    经典示例:计算阶乘
    9 P/ J. q* k, ~; A  ^6 C" Jpython
    * y# O8 x: v2 M; S% |def factorial(n):
    6 X, u- I1 Q$ N( @6 @    if n == 0:        # 基线条件* ^2 L0 V( E% e1 n
            return 1. z% V  D! a- P+ I: N
        else:             # 递归条件. V) S+ l" ]8 r9 r
            return n * factorial(n-1)
    ( r5 c2 E2 M; F8 B  H. w  c执行过程(以计算 3! 为例):
    6 z% V" g" I8 p, Z6 z$ Pfactorial(3)
    6 n' ~9 B% d0 L4 H1 z3 * factorial(2)
    8 n* c( `: d2 R# x0 s3 * (2 * factorial(1))* O3 e, y; f7 Q+ ?
    3 * (2 * (1 * factorial(0)))
    / _* d* G$ t0 \8 E: L1 C9 E& C  v7 V4 `( h3 * (2 * (1 * 1)) = 6
    . c" b" S: S  ^- k4 R  b( }" r
    . t1 M% i6 U3 @  E7 C8 e  l& f2 B 递归思维要点
    0 }" L2 o3 d2 O7 t% C1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ( v7 R3 `7 @* o5 E, F) ~2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - T( a. \, D  }& k/ k( M, z3. **递推过程**:不断向下分解问题(递)- |  R0 ~1 l1 f+ s  D/ C
    4. **回溯过程**:组合子问题结果返回(归)- E( f3 S/ N6 c* {6 j0 _
    ' S' G/ ]  b9 r! f  T
    注意事项
    ( d1 I/ O8 C. `  w) ^& U; U; \必须要有终止条件
    7 C/ H; k4 R/ {7 H; j递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    2 h0 K1 U0 N: Q* p" j) |( n某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 |& Z% I1 R5 S1 |) E  Q尾递归优化可以提升效率(但Python不支持): i+ }" T1 Y$ `% W' @, z# W

    / n& z. m/ ?- s& h9 l. D: e 递归 vs 迭代( e# x6 z. G/ z5 v' R
    |          | 递归                          | 迭代               |
    6 I2 W7 X0 c3 K9 x% Q. N3 T|----------|-----------------------------|------------------|
    7 H2 K7 L" Z/ @0 ]* ?" d| 实现方式    | 函数自调用                        | 循环结构            |
    % x* R4 |, c9 D( `| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    % a- h% ^  W* o. U| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |5 K& \( f# b1 L
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |( n8 ~9 `. O' W" m! G% B! E5 t# s

    ( g% h9 E' c" C2 z 经典递归应用场景/ n' \% `* I, E" v- s6 L
    1. 文件系统遍历(目录树结构)
    % I, T* l! \+ V. a2. 快速排序/归并排序算法' E/ z* s! t. ^$ r# f
    3. 汉诺塔问题
    ' B" `) C  _$ `% o2 i( @# z4. 二叉树遍历(前序/中序/后序). {4 e0 E- f# O/ s& Y* b. C# x
    5. 生成所有可能的组合(回溯算法)2 u4 ^* M: N& K4 j6 l

    8 t8 E* m* L7 M% i& N+ I2 U- A, o试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % D  ~: x8 E( Y我推理机的核心算法应该是二叉树遍历的变种。" T' U+ Q* a  k1 t0 I6 b
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 }) q" C7 u5 ~( m6 z8 ?0 v- h2 F
    Key Idea of Recursion
    , T! v9 z1 z# X3 b, h2 P6 l$ J1 u+ }0 Y: b( E2 @
    A recursive function solves a problem by:3 C7 r, ^6 ?# R) ?

    7 z. G% f2 {% q" M0 ?; z2 p    Breaking the problem into smaller instances of the same problem.! n  V5 e0 ~5 d! d  _5 `
    5 P- [9 U1 e2 B( i, d
        Solving the smallest instance directly (base case).
      J! v) }  g1 [& b# O# B  R/ R3 P* P5 }! H6 O$ ^
        Combining the results of smaller instances to solve the larger problem.* q9 b! r  f0 s6 [; R
      N$ d9 ~; e5 n; u
    Components of a Recursive Function
    * d# o1 Z' E8 {1 m- h7 e2 \) W; K3 H# c. k% I* r
        Base Case:
    + z8 q! H' |' n" N
    9 l/ V, h% u9 C; j7 P1 ]; z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    * x( d' J4 T) C" I. i- _8 _+ |: ?5 k, g5 ^9 @- p
            It acts as the stopping condition to prevent infinite recursion.
    , n+ T! n- p7 W
    2 k3 v" ]: l. b4 _2 N- H. l' {        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    7 l; l. i! q. N8 X6 ?+ X3 L; I9 I# Y3 d6 H6 j6 N0 {
        Recursive Case:
    " _* D& S2 u+ p+ D( o7 Z9 J
    ; S5 K9 I, w7 `; ]/ D        This is where the function calls itself with a smaller or simpler version of the problem.
    , p) k  k4 H) t$ X/ B" r( V8 l9 H1 ?2 j
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    + s, V+ e! u6 i9 i! a/ N' D( e
    6 `5 \! \! R( _$ O* {Example: Factorial Calculation
    , U1 @/ Z8 z- J6 |3 h! H+ q0 s) H. a$ c0 M; k# |  r
    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:) O% ]% z0 r: d  d/ H

    : H$ l/ u# _: x$ ?4 A! @7 ~$ z    Base case: 0! = 1; f4 S' ~; y& T" E& ^
    $ F( M: E2 i+ c$ o9 I0 m
        Recursive case: n! = n * (n-1)!. s. ]" Q- g0 f, W9 T" @

    & R! y) E, m% d3 h  Q7 M4 S5 KHere’s how it looks in code (Python):9 n7 [9 S$ S* Z
    python: K5 n1 x$ q$ C, M( h; l
    ! @4 X" @+ o9 `8 ]* ~) r
    9 Q# l+ q( |1 I; A! k
    def factorial(n):8 R, c6 z: t) z. U' U
        # Base case
    : l7 U2 f! n$ y* Y8 \- E6 Q  |    if n == 0:
    9 |* Y) D) L+ u8 e        return 17 L% S2 u4 q! r
        # Recursive case
    + g# R5 I3 D5 j; i" c, X+ g" L: E    else:# H' @1 J$ x2 X2 G: j
            return n * factorial(n - 1)
    2 V" U1 Y4 u5 X6 r. f
    & i( b6 L5 g+ e. l# Example usage4 d9 F4 `! R; d- G9 N0 J+ d( I/ _. s/ Q
    print(factorial(5))  # Output: 1208 L. m! |  S1 Y# _- E3 m3 {
    1 ]/ M# [" x# I7 p* J. N  q0 C
    How Recursion Works3 A# j' g! ?# l$ x8 ~$ Q9 k+ Z. Y5 F

    # h8 e' z& c# D7 W, y    The function keeps calling itself with smaller inputs until it reaches the base case.% b; ^/ x& O" _4 J) Q! z, Q: m9 g
    ( t$ H" m' S! s/ @& {
        Once the base case is reached, the function starts returning values back up the call stack.: l1 N+ H) {/ @* t4 ?% `
    % A1 e$ a* Y: H6 D1 e/ P; s
        These returned values are combined to produce the final result.& g" K$ L1 F! |9 z2 ?; L. u5 F

    / L* J& p! Y6 _. {4 Z0 {0 YFor factorial(5):
    $ L- k. H6 L8 R0 o7 v
    - A+ j5 ~5 O, a5 K) Y3 S& A
    0 d$ Y3 T  a6 y% J0 M" Pfactorial(5) = 5 * factorial(4)
    # p. M( s/ b+ R2 bfactorial(4) = 4 * factorial(3); z5 g, B: z# \. s0 G: m7 o
    factorial(3) = 3 * factorial(2)
    0 V: Q& y  q: C- [6 C) H# c+ kfactorial(2) = 2 * factorial(1); S: z7 F! {7 m: m1 e9 i% B1 i1 \
    factorial(1) = 1 * factorial(0)
    * m3 h: q$ m% P0 x; Sfactorial(0) = 1  # Base case
    0 h1 l1 ?/ B# `: a' l$ [9 |6 i4 ~  B  x/ p$ n6 a9 u9 u
    Then, the results are combined:  U7 a' ~9 i- N4 y% V2 s

    $ }2 q* |  p2 z% l% H0 _8 t1 F1 ]) U$ P5 y; H! `
    factorial(1) = 1 * 1 = 1
    1 K' Q. {: z/ i! sfactorial(2) = 2 * 1 = 2/ I7 O+ P* Z, s, a0 g6 H0 K
    factorial(3) = 3 * 2 = 6; ?+ u& h/ o, [/ I! i
    factorial(4) = 4 * 6 = 24
    3 i& J; U* V: Z. R8 u7 m5 ^/ N6 @factorial(5) = 5 * 24 = 120, N4 j* y5 h/ z  M

    & z) Z0 @- R. W# t/ {Advantages of Recursion3 D7 p) q( J1 e" ~) {: ]
    : G/ ]9 G! S4 P" j$ c
        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).7 {) O$ G! G1 D: l  u# R& J' H
    / B% D/ t& q* m
        Readability: Recursive code can be more readable and concise compared to iterative solutions.& u$ t& u6 G9 W

    0 w2 @, ?6 ]  J$ D& t/ t! M  uDisadvantages of Recursion1 M) V7 z: g( L9 F
    " M$ q  U" C' G5 H
        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.- |) o7 N* ^- h' e; B0 J8 s9 H6 |  i
    8 i" t% ]* @! a  h7 I
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# h6 ~4 d1 j1 j0 @
    + |1 z! P( Q! E$ F" x
    When to Use Recursion* @3 x! m% E' F6 v9 Z. i4 J
    $ n7 \8 e' ?& G3 f4 q# j
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    8 P2 x- j3 M, h1 p3 L" d; p1 e2 N4 ], c
        Problems with a clear base case and recursive case.
    7 S8 ^6 T7 l. |( n$ r5 ]5 J& ^# n
    / g8 s' k+ W+ w- I1 ~* YExample: Fibonacci Sequence
    ; C8 j* q2 Z& r, z1 _- n
      q* L0 ?' N6 z% _" TThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    : c/ s' {7 a9 _7 K& v! D9 b/ o" s4 u8 i1 u% a3 M# u
        Base case: fib(0) = 0, fib(1) = 1
    , O: B3 [  w5 {7 {' @2 ~$ K! w: w* G$ ~
        Recursive case: fib(n) = fib(n-1) + fib(n-2); ~( E5 e' v- I7 P9 r, R
    ! M9 N, p* i# n+ _7 \
    python
    $ C" M2 N9 j; Q% {! D1 W% @' A! ^" _3 q$ a, X, k! p4 E6 S. s
    8 _1 Y* q/ I( R$ e+ F; ^- X8 X% U8 q
    def fibonacci(n):# m% E8 k0 R* O$ `2 A
        # Base cases
    6 D5 J1 k$ R( u2 N1 ]1 S    if n == 0:; k, P6 u7 C) A1 f: v# \/ d+ n7 l
            return 0$ w- Z. y7 V7 K' S
        elif n == 1:5 G$ k4 J) l4 O6 K& U3 y0 o
            return 18 q+ S* }- ?1 O
        # Recursive case
    ) k2 g8 ?- _+ r8 H4 d6 j! B    else:9 w3 ]* B! g& c3 F& _6 Q; z: O
            return fibonacci(n - 1) + fibonacci(n - 2)
    * d) {8 n6 M! a# }  U# Y: l1 V) l- _& B; \% h# f
    # Example usage" E3 z6 ^" z* t
    print(fibonacci(6))  # Output: 85 Z) b- ]0 I6 s, ]% G1 B

    9 t5 j# v/ H" ?4 o/ B! P& s% m" qTail Recursion
    ( p8 S' X7 i0 }3 {
    9 G) h- e( M+ p9 h1 yTail 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).
    1 D2 t5 X7 R8 X& }+ r% v: h5 q; F; m: |2 R' ?  |! c
    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-7-4 21:39 , Processed in 0.044299 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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