设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    : K  n# v! ]8 \9 Y5 B, m; O( c0 t4 M+ ?  C# m; V. W
    解释的不错. Q* z, y1 [8 ~/ c' e' x& w
    ' T/ r% c) e/ I; b0 {1 V. y
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。( V3 Q( j8 C# h2 [

    ( Q$ X+ q6 K. j8 q/ v3 h 关键要素
    $ a6 o  J) l3 o( X8 a1. **基线条件(Base Case)**7 H* {) B5 @. E0 _5 |# u
       - 递归终止的条件,防止无限循环5 W0 b" O) T* L% I( @6 G, Z: F0 ]% w( ~
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! ?6 S' O$ }% _; J4 ~' ]; `3 D
    2 n3 O8 X# i+ o9 E2 ^* h( L2 ]
    2. **递归条件(Recursive Case)**1 ]9 ~" z+ f8 y
       - 将原问题分解为更小的子问题
      w& X. o- X9 @( n+ h1 D0 {; x# a   - 例如:n! = n × (n-1)!
    * P- v0 C$ P: D  W7 z
    " p! |( W& o- c, t8 _; P+ |) ?6 Q 经典示例:计算阶乘
    ; N, I0 b+ C+ t, jpython
    4 i! {4 [* v$ j/ m/ Ydef factorial(n):# H: B# u8 D$ Z/ [3 j1 A
        if n == 0:        # 基线条件7 q$ o" F' ]3 f: O. g) L( n* \" k
            return 1
    + F( v$ o8 p" p, ~* V  Y/ O    else:             # 递归条件3 @" @/ x/ H, _1 H3 y5 `
            return n * factorial(n-1)
    6 S' o, X2 ?- w1 T执行过程(以计算 3! 为例):' F6 R9 V; c3 ?1 A
    factorial(3), A$ h8 B- L8 |% W6 W; }
    3 * factorial(2)
    - |$ X1 {' B8 I, ?) O1 r. S3 * (2 * factorial(1))  A8 @8 H1 y: p4 V; N
    3 * (2 * (1 * factorial(0)))
    3 t4 t' x( Q1 C# W4 v4 ^5 ]3 * (2 * (1 * 1)) = 6
    " n7 ]: o8 q5 R: y) Q8 N1 c; x6 }* X9 T# w0 L; H2 J
    递归思维要点
    8 a/ f! K. o% I& g1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) k; V* Q7 C( k3 K2. **栈结构**:每次调用都会创建新的栈帧(内存空间)9 X" j6 X0 j: o4 ^# f: b* N
    3. **递推过程**:不断向下分解问题(递)
    6 j# Y. E0 D. e, n; J! S7 Z4. **回溯过程**:组合子问题结果返回(归)
    : j. R! M, h6 ]0 c& d+ U: d. ]. R2 z6 j6 J
    注意事项4 B8 n2 ^4 ]/ `) q: Q
    必须要有终止条件3 ]3 z9 l! s' G3 O  s
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)# e* I* N9 A% l3 D/ q5 w
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ; J& P' K4 Z. P8 r& X% K* K' r& @尾递归优化可以提升效率(但Python不支持)
    * @; V' X, c$ O$ I
    , {0 w  H' B' Y8 O; q) L4 M9 {5 x 递归 vs 迭代1 b, y7 F, U+ E: V0 U
    |          | 递归                          | 迭代               |
    ) M4 h# M7 W5 X, Y, R|----------|-----------------------------|------------------|
    # @7 ]) b4 _: f7 V* @3 J, V7 S| 实现方式    | 函数自调用                        | 循环结构            |
    2 \' A# d1 R( P9 T| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |/ R. _: `. W8 ^/ o8 j5 p6 H6 u9 Z9 {
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    : e0 G' _0 x, ]) h| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |# T$ g( `9 S5 h
    3 p- V9 S2 S1 L$ m" ~
    经典递归应用场景, b  y4 k# [0 s) W6 J
    1. 文件系统遍历(目录树结构)6 n% n5 [8 `: _- H1 ]) o
    2. 快速排序/归并排序算法. U$ o# G3 q% b& v- ]6 Y
    3. 汉诺塔问题( {  n8 E! T/ ?9 m
    4. 二叉树遍历(前序/中序/后序)
    $ A6 P2 k1 z: J: ^4 x5. 生成所有可能的组合(回溯算法), X2 d9 R6 m# `" o9 A: c$ [
    3 O! ^& d9 L8 F6 D
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    11 小时前
  • 签到天数: 3218 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 Q( K8 Y4 i, @. p, a
    我推理机的核心算法应该是二叉树遍历的变种。
    + P# Q0 x: b6 ]# \, A另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    0 K' [' @, o; v! _9 G3 q) {; @4 QKey Idea of Recursion/ F. e' b2 b8 c. ~4 J/ {

    7 R: K6 r- e3 `/ K7 {8 S6 YA recursive function solves a problem by:
    9 g" r8 g+ r3 r7 v
    * |4 [6 G. Q' F) q    Breaking the problem into smaller instances of the same problem./ A% m/ V' }: v
    9 M5 R/ O* K0 P  A9 G
        Solving the smallest instance directly (base case).: L8 u2 C# X# [- Z) y/ z6 d; q
    ) ]* X+ K' H# R7 `! O: ~
        Combining the results of smaller instances to solve the larger problem.* N( d/ J& r: G/ `, W

      N( A" e# ]2 {5 J) ?& MComponents of a Recursive Function
    - _" l; e. a$ r- \' h3 Q( ^4 r" ~- G0 B* s' b' ^* p/ A
        Base Case:4 c' W; o' Q7 s* U
    # {  B, O4 m0 e( F3 R
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.( Z& `* j8 Z: |* t# ]
    - `5 Q$ {; s! m& g! S% {7 q
            It acts as the stopping condition to prevent infinite recursion.# @4 {; U) e. X- R$ B- ]

    8 `% E7 \! o6 @5 g        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    . `8 U5 P- r7 |) Y' o* o8 I/ W8 F: `
        Recursive Case:
    1 N& T' ?, I, d, ^5 p: k/ t9 c! x: R* G) M" n
            This is where the function calls itself with a smaller or simpler version of the problem." i1 K8 U, \+ U) q  x- j

    7 [( S! i  ]& z" T        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    # c" c, ~8 f( N8 S" s& Y1 h/ i2 U2 J( \2 o
    Example: Factorial Calculation
    $ E; T0 N2 k; f! q
    $ a9 `/ t' [) m8 DThe 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:
    / f! B6 L3 K- i
    / `- `/ {0 ]- Z6 m    Base case: 0! = 1
    6 U  m! {+ K1 N0 Z" z# i% D, W
    ( k6 c3 j. W# ]/ \4 z0 ?- ]    Recursive case: n! = n * (n-1)!6 H$ l+ H% d" y. Y  v7 |# C

    2 }' t2 V0 @2 B3 MHere’s how it looks in code (Python):1 M3 N; f: s6 ]2 N2 u
    python( [  s: v# K8 ?6 N
    8 G8 [4 h) W+ K. B6 V: Z
    - C& e; Y6 C* F6 i* c- N1 u
    def factorial(n):
    3 J( J0 ^; W+ ?  j& d    # Base case
    , C! B7 l' K% S2 l" s  m9 X    if n == 0:
    * y% S% D3 _( B4 H$ d6 }- Z        return 1
    5 V) k; p& \- p. Q+ X$ S! }    # Recursive case
    + ?) u, {1 h$ V1 E, n5 S  b    else:# o1 m3 t5 p* m% h/ u
            return n * factorial(n - 1)# K1 z, S  F( h

    & N5 g  I1 e5 E2 P2 y1 h# k# Example usage
    9 k( h' X; Y$ H2 qprint(factorial(5))  # Output: 120
    . I7 H/ v& ?  T' ]5 P* W7 X  ]( @7 @% b9 t
    How Recursion Works
      ^8 @; {! _1 W3 N3 C
    ( q; \+ j9 D- s" a' O    The function keeps calling itself with smaller inputs until it reaches the base case.
    " }6 [& z% i& ]. g5 ~1 w! w# v5 }# F) k3 Z; f
        Once the base case is reached, the function starts returning values back up the call stack.% D/ I0 \5 X( \7 M5 c$ D
    3 x- q( t' B- n: [
        These returned values are combined to produce the final result.8 a" O2 Q1 x* \9 V" l

    " `! ?9 T( v+ vFor factorial(5):
    7 t) Z, }6 ]+ }5 ?- v3 r
    ' o% z* f; R* ?
    , y) A1 a. f- Vfactorial(5) = 5 * factorial(4): c; G0 e5 X; R: ?% U
    factorial(4) = 4 * factorial(3)! q7 U9 u: Z, x" H0 N0 p. w) h  a
    factorial(3) = 3 * factorial(2)
      b2 M; g) `. r- Y' F, e! nfactorial(2) = 2 * factorial(1)
    # R0 p" A) \+ W( O8 jfactorial(1) = 1 * factorial(0)
    1 k! J) Z) x2 u( g( Z4 w2 f& sfactorial(0) = 1  # Base case
    1 Z  [4 x/ C! x" a
    4 x) K! [" b6 B, M: x5 }Then, the results are combined:$ n, P: E+ E+ N6 y1 W" m; @2 W
    # P$ c7 Q$ X& g/ R. r4 q
    $ \4 K1 c$ ?+ }& p! F
    factorial(1) = 1 * 1 = 1( h# r; ^# A( O  z) Q' r
    factorial(2) = 2 * 1 = 26 H% B# ^. F* c+ ?% |4 q" t
    factorial(3) = 3 * 2 = 6) U. c# |: h7 {7 y* n0 p5 {
    factorial(4) = 4 * 6 = 24
    5 x' N5 v- z( Y+ |: x$ F+ s. wfactorial(5) = 5 * 24 = 1205 v  }& F! `2 L: s8 Z
    . ]- z- X2 J  ~2 a" r: z" U7 \
    Advantages of Recursion. P/ h' N+ q' Y. |
    - f* P! o; m: L4 B  h  j+ 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 k$ R0 z& n, Q: f8 d

    ; b7 Z. u9 u# D5 @" l    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; \, p4 z+ L' P$ ^. g! Q) k3 w4 ^
    Disadvantages of Recursion" R, {5 M! e8 y" {0 M3 ^

    ' C3 [2 Z: P+ }0 |8 N( |: {" @* K    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.+ ?) G9 Q. }: e5 L
    8 F9 r8 V) ^" S5 O& u
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).; O, d  Y7 f* D+ L" ]0 O

    . u" y7 v) e; c1 a, s9 EWhen to Use Recursion) l* A  O2 n4 b" z; ^8 t( e5 v
      J8 T+ V/ \+ j+ F4 ^( D& Q+ o
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)." S+ |. I; k3 A- S3 }  [* i% F& U
    5 {6 s5 Y9 G  Y2 T  t7 ^  `: M
        Problems with a clear base case and recursive case.0 @2 c% M+ \- }/ w) n2 x: U

    . i$ q$ N3 i! \3 CExample: Fibonacci Sequence2 R9 r" E1 T  F( l6 }
    0 W5 @2 g5 `$ [! p. y
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ( ^9 {4 F+ G9 g  m* P4 [: q% m' T& F5 V( H
        Base case: fib(0) = 0, fib(1) = 12 ~, i8 b3 T3 X' d; u

    & ?; Y! f% W/ U/ D- \3 G* i: n9 Z    Recursive case: fib(n) = fib(n-1) + fib(n-2)* G4 J9 O' F& s' D) \8 ?

    % L' K; t% J' J: _python
    0 u; b7 X# Y% z& m$ F5 e. O: P( k; C. t& \* v% H

    ( p: v/ w$ a! t5 ~; V% m4 o6 ldef fibonacci(n):4 @3 g4 O: `6 x( m
        # Base cases! \% Z# h2 q3 _3 a: B1 Y$ H, ~
        if n == 0:( }6 U; t+ o! l8 o2 i
            return 00 P: Y; M! S# t. L, a' P6 \
        elif n == 1:
    4 F; P7 B7 I" l9 A, e5 ^        return 1) e, @: o* ]9 {4 M: `* H& Y$ q* d
        # Recursive case
    8 Y( c: T3 Y4 L# y) W( e: U    else:" ?5 D1 s0 d, O% r4 z4 Y- O
            return fibonacci(n - 1) + fibonacci(n - 2)
    1 a$ `9 v( P7 u9 }# s" _) z. B1 r+ G# i( P3 O: c
    # Example usage
    ( s8 z5 s/ N; t% F# f3 Zprint(fibonacci(6))  # Output: 8
    ( m- u7 ~2 {2 `2 w* u- Z# H2 [! a% ]5 ]5 f8 n/ q: _
    Tail Recursion
    / S9 Q* I$ G; u2 V. o6 d; z+ [& F; K( U
    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).
    # L9 `5 g4 Y* k. s! k- s
    " L! P- q& E6 i" yIn 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-4-17 21:11 , Processed in 0.060683 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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