设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 5 W; I7 V) z/ t5 r

    ; Q3 u! k: A; c- ?解释的不错
    0 z1 H! r& ?, b# G6 k) I. ?
      Z  e% [( k! o递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    4 m, {8 c2 {! G3 b$ z5 H
    ! m$ k" ^3 K7 ~8 k 关键要素3 r" g6 y( |6 X' @# {! F$ ^) |2 v) G
    1. **基线条件(Base Case)**& o; s, O0 x& |( C9 J- z, p
       - 递归终止的条件,防止无限循环
    1 e3 O/ u- N' ^4 c2 \5 |   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    3 ?' D( S9 @0 F* P, Y5 V, F9 r+ Y3 d) T5 ]. V0 q
    2. **递归条件(Recursive Case)**5 n4 O: ?! h0 h! a7 Q
       - 将原问题分解为更小的子问题
    6 E$ x! \" V, K* n6 [- B: }   - 例如:n! = n × (n-1)!
    3 e1 p. [& E2 x! c3 W2 r) w% r" U3 I& e1 ^
    经典示例:计算阶乘# E( N6 F% n# z8 Y, {' q/ _
    python: m% e. }! W' m. E$ e+ w6 |7 D) B
    def factorial(n):
    4 z0 y# x9 l3 _    if n == 0:        # 基线条件
    + k8 J# a5 A8 |" P        return 13 E3 b5 ]$ m4 g: D* I6 u& |
        else:             # 递归条件
    * G; H; A2 ]% j/ F0 x3 |* H        return n * factorial(n-1)
    8 o* q- [  X( r% g7 h) t0 ~执行过程(以计算 3! 为例):
    ! w+ d: s+ \* I) gfactorial(3)* X7 x' }8 `9 h, `, {
    3 * factorial(2)2 N" E8 w; A4 u; D
    3 * (2 * factorial(1))7 f! `( ^& _8 f4 Z
    3 * (2 * (1 * factorial(0)))
    2 {7 T  w6 m9 J! g3 * (2 * (1 * 1)) = 6
      i: p! W9 I3 ], y! V+ ^& K. w
    ) X7 r3 E+ i* A 递归思维要点; D" M5 w: H5 e: k' K- o+ g2 |9 o
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑0 j- d# ?1 o6 t3 B. G
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)) \$ W" n- x9 @! L+ N* y$ b0 }
    3. **递推过程**:不断向下分解问题(递)% v4 f: W. K$ L, _$ i
    4. **回溯过程**:组合子问题结果返回(归)0 D! E) h" L* N0 U2 Y

    6 P- c. v! K6 c 注意事项( L4 P* J; K* J7 R7 k
    必须要有终止条件  n. ~+ ?) Z1 U  O1 j7 q6 U4 a
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层); W/ }: t9 p  S. T! H1 f% w
    某些问题用递归更直观(如树遍历),但效率可能不如迭代6 [+ {. x6 \6 ?
    尾递归优化可以提升效率(但Python不支持)
    2 a" K5 H& X/ i' j2 j3 o/ \; o
    * }2 a7 A. f6 u9 ~# L0 ^ 递归 vs 迭代3 ?6 Q2 L3 y( F* V: C. J
    |          | 递归                          | 迭代               |
    # s) `; f4 P* C& B! t' N|----------|-----------------------------|------------------|7 i; T9 Y3 E% Q0 p3 T
    | 实现方式    | 函数自调用                        | 循环结构            |5 X* X" \9 V5 `$ V( y8 o( z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 t+ e5 y) X. f( b: e6 k| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |5 `& a1 I) @7 e7 M/ W9 p/ r/ r- w
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" s) [$ }( @$ ~( s

    * |# \  H1 G( q) T8 \7 @ 经典递归应用场景
    0 d3 o1 i- r  W: w. w1. 文件系统遍历(目录树结构)$ w/ l' b6 z, o& D
    2. 快速排序/归并排序算法; b3 \$ W" v( e- f
    3. 汉诺塔问题
    * {1 v& v* L% t6 _7 V/ u, G4. 二叉树遍历(前序/中序/后序)
    " b$ j8 f6 F& v% `) O* W; Z- R5. 生成所有可能的组合(回溯算法)  Z# s9 X% |. r* }8 G

    ) G6 G, h( Q  M2 U, _( ^试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 06:13
  • 签到天数: 2840 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,- [1 y* U: V3 _) {' ~1 d+ `
    我推理机的核心算法应该是二叉树遍历的变种。2 Q; B& H) k! _6 V  c. J- r8 O
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    8 i+ j+ k5 t& H5 s5 r) @Key Idea of Recursion* ~) [4 X) t5 V+ q9 r6 D

    * o6 R/ z7 Q' F, LA recursive function solves a problem by:
    $ d& d) s9 o. G5 @. d- M% a2 d+ M9 }* I1 Y
        Breaking the problem into smaller instances of the same problem.. U* |! o$ p& y, c$ b4 \
    9 S0 N! }% ?0 S& j
        Solving the smallest instance directly (base case).
    / X; Y- `4 M# g$ [& ~2 i7 Z8 E- }4 z9 p! U. l+ e/ H
        Combining the results of smaller instances to solve the larger problem.
    7 N& \$ [# `% o& q* g7 r9 {- S& O1 m2 ?( K/ q
    Components of a Recursive Function' d" Q5 c3 R; j6 S; {* D, F' B

    5 @* ~; k8 C- u" }' H0 x/ v7 ~, D) n) V4 Z    Base Case:9 G- w) i8 w6 V

    & s8 C% H8 e% o% F, W( h        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 `; [! j* Q6 d, T" C% b; \0 v. B# i; D
            It acts as the stopping condition to prevent infinite recursion.
    ( @4 U0 w5 p0 d% p  b
    & q- ^1 h# M" `0 i- N! T( F7 p( J7 I        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' T8 }. L3 U, V# d5 h7 B2 N

    ' l$ d5 y* u$ M0 |6 r6 `# X8 S    Recursive Case:  ~/ z! h7 j* e# K; J+ v5 c

    2 `4 K: O  K. n        This is where the function calls itself with a smaller or simpler version of the problem.
    5 ]! g: w2 R  _$ H6 ]" [0 I3 O. }# f3 L. ^# o6 O
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    9 k8 ^2 Z6 {* L! P' L7 Y/ @: ^2 `9 l2 O9 O* r
    Example: Factorial Calculation
    + u# F6 m$ e6 y0 B! k/ C
    4 f8 z! C& P/ b, T+ gThe 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 Q2 |2 V1 _* L0 N5 `1 T" [
    ; Z8 Z: ?$ j6 _
        Base case: 0! = 1
    5 Y, D+ F9 o# C: n3 }- [* j% H: O. S( }6 g; x9 m( }
        Recursive case: n! = n * (n-1)!% a: F( I+ S. @. z

    + J! x- e  u, }- F0 BHere’s how it looks in code (Python):
    + \( u' a" Y1 `$ c0 Kpython
    % H) i. Q% F$ W
    ( [8 N* ?+ [' ~7 D6 l5 f2 |
    ) B' X3 @& ^! pdef factorial(n):6 @9 o3 \3 Y" N7 P# e
        # Base case" z5 t1 ~2 u% I' l
        if n == 0:
    ; [9 v$ R/ R: p0 K% n/ C        return 1
    * `5 Z9 n4 y. y7 Y/ T5 @    # Recursive case
    & W7 v) j" k. c, P! }8 L    else:1 H$ k( d( M$ u, r8 j" M
            return n * factorial(n - 1)! |' f) O6 R# ]( W5 H

    " w/ H0 F) K1 J# Example usage5 ^4 k* V% g$ d& m. q$ s
    print(factorial(5))  # Output: 120
    $ J+ Y' Y' D% S, @
    * l3 j" b. n: b8 z# z' xHow Recursion Works
    . w/ Y6 Y. X  u9 g9 G) D) n
    2 W3 g1 Z! H+ T' A. L2 A" \; Q% t    The function keeps calling itself with smaller inputs until it reaches the base case.: d9 ]& x+ D8 `' B( e1 {

    3 T" l3 L& [2 c1 \/ G: e    Once the base case is reached, the function starts returning values back up the call stack.
    4 F* B9 n0 n3 [' ^) T5 f) x4 V8 X( x
    / N; Y+ R& X/ g    These returned values are combined to produce the final result.2 K. }% D4 L5 j% h; m$ S
    , T) U) ]2 I. d8 n0 c
    For factorial(5):
    3 ^. [2 Y2 z: [8 ?' z, Z7 `/ n; Z) k& T# y6 D: j

      L6 c% I" g0 L7 f  q/ Lfactorial(5) = 5 * factorial(4)
    4 [3 s2 z. C- C! j5 i3 L2 Wfactorial(4) = 4 * factorial(3)9 O, K% T. W3 H0 j8 j5 W, z; [3 P
    factorial(3) = 3 * factorial(2)
    , B" u4 B, i5 ufactorial(2) = 2 * factorial(1)
    5 h) @% S3 [2 n& x) `; R+ `& gfactorial(1) = 1 * factorial(0)
    # ^5 f" ?$ [1 n8 \1 Rfactorial(0) = 1  # Base case8 k# s9 S: m& g) A+ ^5 L

    4 L. o& L& Q% @& aThen, the results are combined:, ?1 d1 g  A/ o
    : O# g/ k; t+ T* |. Y4 q2 G8 {$ h& h

    % Q- V) P/ f' O* }, ?2 i+ E" }factorial(1) = 1 * 1 = 1
    # Y6 W( @# `: d) ]factorial(2) = 2 * 1 = 2
    ! @4 {0 l7 k3 j7 I0 Kfactorial(3) = 3 * 2 = 6
    $ Z, A) o$ F" |, E5 v! u! Lfactorial(4) = 4 * 6 = 24, Y- r, c, N- A
    factorial(5) = 5 * 24 = 120( I. W7 q# h0 @* L! e

    8 @' q4 X' g* L; @& K7 f- gAdvantages of Recursion5 T( @$ B3 X7 p

    6 w/ S8 S0 o3 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).
    1 |7 X1 U; X! Z4 u- ]. L
    $ J0 d- ^1 L# N5 n4 t    Readability: Recursive code can be more readable and concise compared to iterative solutions.$ L0 ~! Q- Y6 ~9 `- N

    / N9 V0 m3 V- |# G6 u8 }Disadvantages of Recursion
    $ S. Z3 i" Q! l
    & o. _- o6 h  p! F1 a9 ?! ]    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.7 W7 H$ [- e( p/ _
    & d5 I, t" j  d* ?8 h. l7 ?
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).* {. P- t( S0 J" g2 S

    & Y& Z8 [$ W) w/ t- Z' X- jWhen to Use Recursion
    ! t% p: ~4 \! G2 S5 Q( Q) e2 b1 {  r7 m' a0 _! a
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).8 |: X- Y6 ]+ G% a+ z5 S/ g8 A+ g! Z

    . i/ g: Q" P4 f6 e  w    Problems with a clear base case and recursive case.
    & y6 K' W: L& [5 n9 `' Y* y- z
    ' i# V  b: ?( }) p" f" LExample: Fibonacci Sequence$ i. G1 [: h# v) X# Q% {

    3 a/ z( ~$ B6 f! L: oThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) h9 g0 v1 w9 B+ M% y5 n2 R
    ; j, O/ d8 c* B
        Base case: fib(0) = 0, fib(1) = 1
    ; a! \" d. V- e% D# V* U
    $ [0 G/ K+ Z/ z    Recursive case: fib(n) = fib(n-1) + fib(n-2). v- B% ^5 X6 J( G. c; e7 C1 p# \/ g
    / ^& X1 @; b+ |9 _; U3 w
    python$ F! \0 A' E4 Z& J" ]3 f( w4 o0 g

    ) A; o$ B/ v  c; B: @5 ~! [% X% H' [& c; C; I4 ]9 z% q3 k
    def fibonacci(n):8 _1 @' R. I; F) f
        # Base cases
    7 _/ \9 C4 R/ W7 q1 Q    if n == 0:" J) X" V+ O: F. u* {
            return 0
    4 Z3 p" Q) [# O8 s8 x; F, z- }% f( r    elif n == 1:) D, w7 g1 r0 {
            return 14 I, t" N7 Z% t, i9 F* `
        # Recursive case
    9 M$ f$ G" L1 w6 F, H# P+ \    else:
    & g+ S* d) G$ Q* Y) _, G& d        return fibonacci(n - 1) + fibonacci(n - 2)- p/ a/ o' _9 W8 h/ e: b# h* o

    : b/ v  I& K4 d$ ]! s% F# j0 e# Example usage
    # S* o/ o0 w8 U5 g* ^2 g1 Pprint(fibonacci(6))  # Output: 8
    " B$ e6 Z9 e7 X! ]9 t, J# b! L8 @
    Tail Recursion
    0 u% f0 ~& H, L9 l% p  U. ~, [* T& _% k8 F! b$ w* w
    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).7 c: e. ]+ @/ Z( v* o
    2 ]" W. x9 U2 G" x1 _1 E& Q
    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-2-23 03:56 , Processed in 0.034875 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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