设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      {; Y7 u) d9 }/ k( j* G$ E, h8 v: ?
    解释的不错
    $ [0 t2 p/ G# f' o, n; P0 h+ t2 [: B7 d7 U& m8 f. C3 {0 W5 ^
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ! Y0 y9 ~5 g1 A7 q9 m: m$ |& z. e1 N1 D; u- G
    关键要素8 _4 J0 l5 V$ k5 ?# h( m% O
    1. **基线条件(Base Case)**8 R' d. ]/ [3 X( W4 r
       - 递归终止的条件,防止无限循环( m8 ~, z, J$ _; u. P
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    0 i2 ~2 _1 D6 s# O$ E3 u+ @. C& a* m+ W( @0 x# [% u, O, s
    2. **递归条件(Recursive Case)**, |6 Z9 ^( s5 H' [2 t8 A7 |" C( T' j
       - 将原问题分解为更小的子问题
    ; g, j9 Y% z& }4 F* b: ?   - 例如:n! = n × (n-1)!
    0 D# l* {) v' d1 A1 ?3 b) I2 K4 O2 i- |% a+ f5 f) g- I) m& B6 Y
    经典示例:计算阶乘
    ( v0 u  Z& [' U+ k1 O8 y% mpython4 K% t1 T" L8 s5 I
    def factorial(n):7 Q( y; ]& s, T% I9 Q8 x0 A4 C8 Y
        if n == 0:        # 基线条件  k) u+ M- Z  y* v) w
            return 18 \7 o" T1 e; A& L6 J
        else:             # 递归条件. q/ D3 S" l2 a+ a7 D4 X
            return n * factorial(n-1)
    2 n* S6 H* D! [/ ^1 L$ g% F执行过程(以计算 3! 为例):
    + j" T: I4 t% nfactorial(3)- ^7 o3 w& @2 `) ^7 H, \3 z% M( v
    3 * factorial(2)
    & z/ z1 D5 ?/ Q# X3 * (2 * factorial(1))
    " w) B) h" w" A* h# m3 * (2 * (1 * factorial(0)))$ t& f. X. J' M8 _6 c' X
    3 * (2 * (1 * 1)) = 63 ?" l9 i; K2 ^& X1 K% x! N' ^
    / H6 G/ E# h; T7 \# w5 a* O
    递归思维要点
    ; w5 @( F$ t0 l1. **信任递归**:假设子问题已经解决,专注当前层逻辑3 {, q& x5 J" N: G
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ; P! B2 i% b4 f3. **递推过程**:不断向下分解问题(递)2 h$ Q; Z6 H6 u1 z
    4. **回溯过程**:组合子问题结果返回(归)8 E1 C" N; M) m% V% x4 c' J

    . S: A* O* |2 n: z 注意事项
    2 K, m3 L$ G0 Q+ u必须要有终止条件
    0 ]' ?! P) }2 u0 ]) R8 X递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 ]& e1 Z5 u0 Y
    某些问题用递归更直观(如树遍历),但效率可能不如迭代6 w' S& \7 Q- o' K1 m0 W! R* n
    尾递归优化可以提升效率(但Python不支持)3 W) n$ ^; y* j/ y1 I7 @& {

    6 k+ n0 _3 a5 M, W 递归 vs 迭代
    $ C) _8 l9 U' E, K: k! M6 s|          | 递归                          | 迭代               |4 c$ X/ T% U8 ^/ Z. K
    |----------|-----------------------------|------------------|
    & D6 d/ @' Z" [, y4 \  P9 V, A| 实现方式    | 函数自调用                        | 循环结构            |
    4 o4 @) G- ^4 s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |2 h. |3 @6 {, X1 ~
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |' l* N" \; g8 U6 @* T
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 z$ O) |2 B0 k# z) H5 h$ Z& \7 I6 P$ \- Q1 k5 s- ]: E) ~7 T, i
    经典递归应用场景
    3 m+ {. l0 u# A3 h" m1. 文件系统遍历(目录树结构)9 O  N# d1 z) S6 u+ C
    2. 快速排序/归并排序算法4 m$ `3 u7 y# M
    3. 汉诺塔问题
    ( L& q. b# B) z$ Z4. 二叉树遍历(前序/中序/后序)
    ' N4 ^- F2 _# K$ M/ B( ]: Q5 b5. 生成所有可能的组合(回溯算法)
    5 ?9 k& L6 Q/ I" }& ]
    6 f% G* Q, K( B7 W* O  y3 z8 h7 E9 k试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    4 天前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
      G/ I# d- T9 \  X/ o2 Z" g' H我推理机的核心算法应该是二叉树遍历的变种。9 S; H8 l2 h! h
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    : p; P" e0 `9 N9 s8 jKey Idea of Recursion) j7 Y; l0 L, q5 [) `6 K2 u+ K
      ~' ?% d9 ?% r  [0 A
    A recursive function solves a problem by:8 L( N% R8 g/ U$ T) R! i

    4 ~0 m& o( J9 K: S    Breaking the problem into smaller instances of the same problem.
    % ~) g# d. P% q$ e4 j" N% g
    # D7 ^3 Q/ c5 D0 {5 ]& C% T( H    Solving the smallest instance directly (base case).
    9 R6 ^8 W; ~+ m0 P; N8 `$ E- D  ~' H! _" P
        Combining the results of smaller instances to solve the larger problem.$ [4 s+ B+ c8 M
    1 u- f- B' d# a) O' Y
    Components of a Recursive Function
    6 e# S6 g9 h* G6 x* U; T% w
    , Y3 Q9 O  ~! V2 o) C    Base Case:: o- r) a3 ~$ ?( q
    ! i  p2 {3 a( i
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.- l1 C8 \( y5 I# {/ b

    % s8 N& R& Y# X- l- O/ T        It acts as the stopping condition to prevent infinite recursion.
    ' i- o- ]5 o  p7 U5 l
    ( o/ h0 |5 j# o3 w# }2 W        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( X1 s+ x  P) X2 _" ]8 z
    , p' I' w+ y  C2 A
        Recursive Case:
    ( f0 B4 e6 T+ V7 k6 W4 W
    1 J! `' E1 d3 z) W: M% Z        This is where the function calls itself with a smaller or simpler version of the problem.
    ) a& \2 K4 P& s+ {6 r+ K6 A( j: f
    % n8 k( @. h# \; n        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
      D+ Y8 z  ~3 w' Y% ~$ _0 q' k% o! e: Z$ S
    Example: Factorial Calculation* e4 @$ j% [$ t& Z1 b5 F, \
    3 z$ n; y1 W0 P1 S, e  z
    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:1 o$ R4 E9 `  J8 {8 k
    / m8 [7 u% n5 T
        Base case: 0! = 10 K4 Y5 n3 q/ X4 _5 t

    & s6 @9 W; s  Z3 p" }; E    Recursive case: n! = n * (n-1)!5 Y5 x- N* J% G9 ^

    4 r) \$ J, ?! e3 r; _Here’s how it looks in code (Python):
    ( i$ L  z# U- E. E) b. bpython$ l7 w) K( `, R; T9 b' Q$ F

    4 O/ I) j8 s6 w* H
    4 w$ R" ~% I) x( U- Z( A$ v" _def factorial(n):7 f! q& Q( I2 [& C5 p  P8 O! A
        # Base case0 [0 Q! F! p" E+ t4 Z% ~
        if n == 0:# \9 N8 M; p' h, C: i
            return 1/ M7 ]. I8 s% r1 V9 A  V+ f
        # Recursive case/ `7 b5 `+ i' b' X. y2 c
        else:3 R7 p4 ]. |' @# k; n
            return n * factorial(n - 1)
    1 P5 L3 J# d) u. ]* S- n- n) g' j6 B0 F$ R0 c4 W" r- ^2 v
    # Example usage' {0 x: ]! \* a. M4 F
    print(factorial(5))  # Output: 120
    . }4 o* B' o" ~, p, ?5 p
    9 f4 ~& i& f2 v* j9 YHow Recursion Works
    - n6 I( ?; e! o. M
    ) W: n3 Q( k8 q- J2 c    The function keeps calling itself with smaller inputs until it reaches the base case.3 y! g' v/ @1 D' Y

    5 Q" J4 D# U. C$ N8 h6 P/ n    Once the base case is reached, the function starts returning values back up the call stack.* v. k$ e! P# c
    $ |+ r0 O3 O  C  h& m$ {0 S
        These returned values are combined to produce the final result.
    9 j' V" ~/ w4 v2 N7 l$ A1 Y+ T% r# e6 f1 j) X  h
    For factorial(5):
      L8 X8 Y3 {, O, k7 O4 ~6 t( q3 M1 j: L9 z+ _- O
    4 A( Z9 X* Z, _' }9 c& |: ]
    factorial(5) = 5 * factorial(4)
    , q5 d, L( n( N* `1 H3 {factorial(4) = 4 * factorial(3)
    ' O) W1 F& Q" P' ^' x/ cfactorial(3) = 3 * factorial(2). t) G# P  W. G4 B
    factorial(2) = 2 * factorial(1)2 I( s/ ]& e4 Y
    factorial(1) = 1 * factorial(0)
    : h9 M4 y& w8 F) Q4 w! ~factorial(0) = 1  # Base case
    . Q! s9 B1 s& o5 b" n$ ?
    + U) a6 z, G% L0 b; fThen, the results are combined:0 F& q; q) f$ Q3 u3 |; ^+ D6 O+ K& }1 i$ ^
    , D5 _6 O( n8 `. H. }5 Y
    8 r- Z7 ~0 S4 P+ t7 x, }, l
    factorial(1) = 1 * 1 = 1
    + p: s9 ^: _! e' z" ffactorial(2) = 2 * 1 = 2
    7 E) Y$ M0 s6 l1 L4 \) @& [. }factorial(3) = 3 * 2 = 6
    5 b. q! m6 J; z- j$ x* ufactorial(4) = 4 * 6 = 24- ~4 c/ _: Z; f$ e% k3 D% A2 P+ o
    factorial(5) = 5 * 24 = 120
    3 b8 i" F7 ?3 }+ w1 k7 D. j: u) q( j' t" I( S
    Advantages of Recursion: N9 V$ r! q; N3 {# e. p% [5 ~

    , ?0 W: ?3 }$ e( u0 M  C+ q    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).
    , ~( a1 y2 n8 w/ d: y
    : A. V+ c$ {. Y8 l    Readability: Recursive code can be more readable and concise compared to iterative solutions.1 s, I9 `( R3 L: R2 h

    # |3 H$ ]" y5 }5 M; I9 P; SDisadvantages of Recursion
    ' a" D* _: {( a$ @. d7 K6 j" X8 ~9 A- a/ I
        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., Y& L: b5 T5 Z# ^
    + B( R+ F2 T; S; S% d# Z( k
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ( C( t! n2 q. W% F1 F# ^$ [* l: g: {, L0 G% }6 @
    When to Use Recursion
    6 X5 r# s5 N4 d: |; y0 h- V- H+ g: z( ~! c& Q
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).+ g4 ^7 a8 F& P. S

    1 l) S; W8 j& t4 a( I    Problems with a clear base case and recursive case.3 a! D8 l$ M) ~; I2 m" J5 h7 ~

    : ^" F6 F! ~1 YExample: Fibonacci Sequence% d) f) H( d- ~2 J
    $ y+ Q5 C, `% E) j+ F
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# S5 i4 A7 G' V3 T- j4 {% X

    % ?* r- h5 w* @5 O% u    Base case: fib(0) = 0, fib(1) = 1
    / }$ L) R! @& l# F; }# b4 ]# E$ \) t! r' |% L
        Recursive case: fib(n) = fib(n-1) + fib(n-2)3 w- T; P* e0 D9 z
    & ]; h0 u$ h1 v5 O2 N
    python
    % o  z! ?) i, g4 F/ k5 B. ~$ L% I; S0 f- g% r
    ' O, ~2 S/ l9 L& \  T; }- p# W
    def fibonacci(n):' P; ?; X- s6 N; k$ K; B
        # Base cases
    5 A$ [% U* Q4 Y    if n == 0:# B! d# x7 a( y! X& _8 `. O) g
            return 0- q% z" {8 K( E6 \4 T$ F  ]; }
        elif n == 1:4 l% M: v. a' v, G' m
            return 1( x& z2 L4 B5 r8 x5 U4 ~! |
        # Recursive case
    # ]. f. ^5 W# K) l    else:
    % G* d2 i0 r* ~8 J8 V& w        return fibonacci(n - 1) + fibonacci(n - 2)
    ! X3 Y0 Q- ?" t
    " r. o' A  i; p0 ~; |$ ?' c& V# Example usage
    9 v0 s; E' Z. U! E- s& pprint(fibonacci(6))  # Output: 8
    : t, m3 r! ?: q- g( K: |1 [) X
    Tail Recursion( q6 X+ p8 G* ~

    . j* A0 ~! m& n: I4 k, 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).% F+ @9 ~! N5 g- q# M/ r
    + q' V% z: f" O( d
    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, 2026-3-27 17:38 , Processed in 0.076360 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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