设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      I- n  c# I* ]
    + ~1 U) V/ Q6 ?: t5 P8 \解释的不错, O  K7 F# e4 ?' F* L* Q' H+ H6 [
    - b. ?- J  O' V" N* j& G+ e( R$ ?
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    8 ]$ R8 k: t+ ~: C6 z# Q7 b5 i6 w6 {* w  D( R7 T4 V/ N
    关键要素
    # {6 \* u6 A7 n. L1. **基线条件(Base Case)**2 o' A6 T5 G  W! E5 _
       - 递归终止的条件,防止无限循环
    1 ~* R0 Z- C8 _: `( E. Q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " S" e# D' Y. [, a
    : o) q. U  ^1 J) _; E, d+ t8 k2. **递归条件(Recursive Case)**
    , {  F7 W# S, f5 k# i) m   - 将原问题分解为更小的子问题
    : P; i# `- x5 g* d0 g   - 例如:n! = n × (n-1)!
    6 ]6 g/ G' d& r$ {* ^6 A! b4 c+ [6 S
    经典示例:计算阶乘( ?; I( ?- x1 R4 y
    python/ Z8 M4 r; U) ~" I. r. s2 d$ B
    def factorial(n):* E% q7 Q+ U% _& d8 U9 z# E
        if n == 0:        # 基线条件' Q; `. H6 Q* b7 X* c9 S9 o0 h' u
            return 1
    ; j' c( J" w! R& U! }    else:             # 递归条件3 r% p( y! ~  J' T
            return n * factorial(n-1)% H, R! |. w! q* N( q  T
    执行过程(以计算 3! 为例):8 S5 t5 ]+ a, _! Q* B
    factorial(3): N9 Y# [4 O8 h( @
    3 * factorial(2)
    3 z7 N5 ?4 C- M+ v; b/ T" ]3 * (2 * factorial(1))
    # ]! ]; W5 w/ q! _. s! M3 * (2 * (1 * factorial(0)))
    # Y# X! D! c" z1 Y5 ~0 U0 y3 * (2 * (1 * 1)) = 6" X+ t) @- r  @  [5 v

    0 G' U: M7 @0 }# d$ u 递归思维要点/ {( i% g, s( F! j
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    * s% i3 C. l! i5 y8 e2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 Q# X2 t5 Y' ?9 l) q  s6 J( t3. **递推过程**:不断向下分解问题(递)
    # }& U6 T6 l$ Q4. **回溯过程**:组合子问题结果返回(归). @8 O/ ?% T( K! P$ D" q. k+ l1 C
    3 Z' a: }3 S( R
    注意事项0 u6 }7 s* k  W: f/ D1 |7 x; M) X
    必须要有终止条件
    ( U" }" }. B4 ]4 X* f递归深度过大可能导致栈溢出(Python默认递归深度约1000层)* a! J% d) [( t) @5 @- r2 y: v, ]
    某些问题用递归更直观(如树遍历),但效率可能不如迭代$ G. O8 u+ K6 u' @. f8 h( f1 m
    尾递归优化可以提升效率(但Python不支持)
    * ?6 Y; t6 Y1 V/ B7 Y" W0 `; K& c% u# W& k' A
    递归 vs 迭代
    0 l2 d- I0 r2 y|          | 递归                          | 迭代               |' G+ t/ H$ ~* w0 W
    |----------|-----------------------------|------------------|2 c/ i! q% ]/ F2 H5 @  t" k
    | 实现方式    | 函数自调用                        | 循环结构            |6 j3 T/ B4 p* c/ O; q
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |$ i% C7 `. ?4 {( p1 w! F. l, j
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    1 D; a3 u' n% O2 b| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |9 q5 D; U* Z. `* }
    . ~; }( D& y0 q& }; |* a4 _
    经典递归应用场景: h# y/ K) q  V: R& o/ m) ?! m
    1. 文件系统遍历(目录树结构)
    , K. M' M8 n* c2 a* v/ b. o: y; ~4 G2. 快速排序/归并排序算法7 {. C8 s3 r& j: X/ Z  x
    3. 汉诺塔问题+ }2 t/ R/ k+ y( p6 A: t* w
    4. 二叉树遍历(前序/中序/后序)4 A- A; I: {- S2 c( A" t
    5. 生成所有可能的组合(回溯算法)
    $ o1 @8 ]5 U  _5 t( j
    6 Q& |! U3 q2 }& t6 r试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    11 小时前
  • 签到天数: 3103 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    4 U2 Y3 A8 x! p6 w) k2 U我推理机的核心算法应该是二叉树遍历的变种。
    ) ^" W4 s) b! C/ ~( l5 W- y, M另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . I0 e8 t5 Q; g( }; ?: TKey Idea of Recursion! F! P1 M8 M/ M2 x
    , K6 J' M0 k3 i' v7 t
    A recursive function solves a problem by:( [' U4 k0 m  s& r0 i. C- i; m. n
    ) \8 B$ Q4 \5 u3 D  [
        Breaking the problem into smaller instances of the same problem.. J* T; r6 B& k5 R  _4 ?0 M

    1 V% @8 L( G+ n  Q4 y3 p' m4 K4 {" J1 w    Solving the smallest instance directly (base case).
    4 R1 w. U0 E! t% \9 U
    - ^- x  u4 a# T! G: L- B: n  B    Combining the results of smaller instances to solve the larger problem.; C0 X5 Y' J4 \& [/ f* p

    7 W% K' D- k  N3 K3 _+ @8 cComponents of a Recursive Function0 V' f; a! e. t
    9 s+ X$ C! e5 O. U
        Base Case:$ z! a9 e) j- N; K% c
    / V& }* L) W1 O% X, }
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 Z8 F9 H( ~( J: T/ r
    " q! {" m1 Y' k$ I" X4 p- A: x
            It acts as the stopping condition to prevent infinite recursion.# y  q, S2 P; a/ a  V/ D
    ' x% c7 N% b- _- N$ V/ v  w
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    7 H  H$ F2 E) x9 ?8 h
    3 I6 e) G9 B- U  e7 ^    Recursive Case:$ a9 ^) F0 x7 v9 w

    0 p  M% z5 p0 l& Q; Q# z        This is where the function calls itself with a smaller or simpler version of the problem.! `+ b8 ?* {1 L) E
    4 }! R9 Y( Z1 H: C9 X$ ^& B0 s
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 `; `( O7 ^1 Z- x) Q
    0 [: P( b" q  ^2 A- A
    Example: Factorial Calculation9 r4 Y9 g/ l9 o6 ]+ K  B

    - I- x6 m# I2 ~) `3 i% L$ t: vThe 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:
    7 V# X1 b& j, O1 W; Q1 o/ n, F$ q6 Y: ~' \6 X- a5 u/ A
        Base case: 0! = 1
    & o3 }) P0 x* V5 M/ z6 f0 R; Y% ]
        Recursive case: n! = n * (n-1)!
    : v0 d7 J# v6 f1 V# M/ e* F& l8 X
    Here’s how it looks in code (Python):
    8 B* F/ I% n2 f4 Q& t/ w  z8 g! S5 spython0 \; U7 D& N2 U2 n- k: h6 `$ ^$ ~
    / Z1 U. W1 g, U
    0 l! d9 ~# e  g7 ^- |, j) H
    def factorial(n):( t$ g5 Z: x7 V
        # Base case, i( A$ Q+ t- i" j4 k0 |
        if n == 0:2 G" R! b' Y* J3 l& f+ e! ?  u# {
            return 1
    7 O3 S# H% |9 W3 F5 Z; b' Z$ a3 ]    # Recursive case$ A3 ?, _4 x4 W. }2 W9 n' S
        else:5 W. |0 t1 ^/ G# t1 B, u9 e/ O
            return n * factorial(n - 1)( [& [4 C7 Q  ~. f6 F
    % \. i0 O# B* k. d
    # Example usage7 A7 }9 F5 i/ k+ ]
    print(factorial(5))  # Output: 120, s2 \: I& I* Z  I& X% k

    4 ^8 G6 t  }* Y" JHow Recursion Works% @! O( F3 d) e* @/ D3 O1 A- r
    ' D; z- Q# W6 U! \, G; X
        The function keeps calling itself with smaller inputs until it reaches the base case.
    + a9 C( Z+ Q* K, U1 ?  E( q9 d: Y8 P' ^' @# }
        Once the base case is reached, the function starts returning values back up the call stack.
    7 c7 m% x% ^& V1 p. ~2 {5 T3 m% {1 `) K" |- v2 T
        These returned values are combined to produce the final result.% E( `- m' p; X8 S1 u, R- |
    1 |6 A5 P+ k+ M# u+ E
    For factorial(5):
    , c& e' x' h. P  W+ B- `1 H( a2 ^' W' V1 e$ G
    0 R# i. X) d! T# X9 ?
    factorial(5) = 5 * factorial(4)' K% N6 o8 {  v* N
    factorial(4) = 4 * factorial(3)- U( U& @4 }2 x! L$ |' U; y
    factorial(3) = 3 * factorial(2)1 B# e! H+ F9 C# o+ K. |" Q2 q  @
    factorial(2) = 2 * factorial(1)% \! d5 P+ y" W4 z: G
    factorial(1) = 1 * factorial(0)) T4 b1 L5 v! S- o( g0 k8 N
    factorial(0) = 1  # Base case
    3 R! a, T; k2 o& |% G' Q
    3 A  w9 K9 n2 g0 y: |4 g! Z/ wThen, the results are combined:
      ]- G$ C# e/ h8 z# V5 x* ~" ], k0 K% j+ t

    9 P4 ?. M0 m; a1 q6 W( rfactorial(1) = 1 * 1 = 14 D# Z; o, @4 B( h, `
    factorial(2) = 2 * 1 = 2
    ! F. ]# }! j4 N5 ]6 Jfactorial(3) = 3 * 2 = 6) X- f& {$ {; w- F
    factorial(4) = 4 * 6 = 24  O: f) h2 h, `) R  Y  G
    factorial(5) = 5 * 24 = 120
    0 g( X2 e( x* O6 m7 p5 T& P) f* P+ v
    Advantages of Recursion* e% u% v" [% \& Z7 u" W, |+ K
    ' I" Q8 l* f9 S" T4 ^3 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).' Q5 z' l- w; ]2 R

    * N# y; O6 q' M. v    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ( N7 b) [1 l( i" j
    $ A. C# i) c' DDisadvantages of Recursion
    % D6 P; q: ~- j( A7 t" J- f) S& O( r* Q* L# S. B
        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.
    ( E: D! g" W2 A
    . `( N+ e! A) `4 i6 A    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    8 g- P8 D/ r, n; T% b
    ! @* z+ N0 x. }9 AWhen to Use Recursion
    # A( C& |5 ~- J, g5 y' x# B3 G# {0 v, k! Q, X
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    6 z, J. ^/ L8 W; {9 {
    ( Y! P, ~, d* o4 V: v    Problems with a clear base case and recursive case.+ H/ z* x9 D/ C( h

    ; \' }9 M! z6 w: O$ E) oExample: Fibonacci Sequence+ p$ r& J- s; \+ f- D3 L, ^

    : t& M7 m" [8 A, L6 R: pThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    9 F) P: m. j) E# o3 m- T$ U( ?
    2 Z: x0 j- D7 r/ P$ G  f    Base case: fib(0) = 0, fib(1) = 18 C1 l) K9 _# e7 G

    + \5 g) e4 n0 k8 S7 U& F! H    Recursive case: fib(n) = fib(n-1) + fib(n-2)/ F2 @3 M4 K- X+ v/ e- [/ b
    / m$ I9 s. N. h$ u% D
    python3 H8 s! m. ~1 s& }8 A& D
    4 Z) n$ o- P3 y2 ]8 i: ]
    4 X7 C" P# r/ H8 i/ A! o
    def fibonacci(n):! z0 Z! s! }2 Q
        # Base cases5 _5 B* e( M3 w( B* j: ~7 P
        if n == 0:& n/ |: E3 E% k# Z
            return 0& Y5 r9 H& z/ Q8 B
        elif n == 1:
    - E1 J. S& f3 b1 f        return 1
    & o9 l/ ^, j9 N( [0 r& W3 l    # Recursive case- f2 m& I$ m9 a/ H
        else:
    # k! S) Q5 ~% @3 K) [7 N" c8 H        return fibonacci(n - 1) + fibonacci(n - 2)
    : D" u6 c7 U( U
    ( C4 J8 l  W" P5 s) t# Example usage
    7 c: p# K' Z. ^8 zprint(fibonacci(6))  # Output: 8
    / I* W$ B7 A& A) C$ G4 w0 N% v
    ( B- u8 q5 Q4 X8 C8 ^5 h0 rTail Recursion$ Y' s) w; u+ w; C; t. B' U% f" z  l
    # y9 r- N: c5 s0 U7 ^/ P4 r
    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)./ r  w! B1 f6 ^" R# R

    , L8 d* h# z4 ?5 ?, m9 ]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-28 18:52 , Processed in 0.033737 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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