设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 $ j" L+ p$ d' K3 z" a
    1 n0 J2 ~% w' h- b
    解释的不错
      Y2 D' P8 F9 i+ p
    / n- T" g5 H$ p1 W3 f递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    . `$ x! p( w8 I3 p- o! a
    7 k1 S! Q- V; T; E  U& k 关键要素
    . H2 l* g- m5 p' N/ t1. **基线条件(Base Case)**) ]2 \- C+ l! a* V8 X
       - 递归终止的条件,防止无限循环
    & @( J" M& V, ]2 Y* S   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    6 P; f; L* q% ~5 M. P4 W& `+ l$ ^3 D! |1 f
    2. **递归条件(Recursive Case)**
    ) O: B  w& f! B( O# i   - 将原问题分解为更小的子问题
    . U7 J8 d, u3 x  A. \   - 例如:n! = n × (n-1)!5 Q! f" g! Z! n2 A- f8 S$ q& l+ o% l
    + r0 e" T/ ^) M1 w$ O% ?
    经典示例:计算阶乘9 ]0 _/ j( K! ?! d1 w5 h
    python7 U- R/ D: n- S: L
    def factorial(n):
    8 A+ o) a, t! R7 c# `6 Z8 }0 i    if n == 0:        # 基线条件
    & t; q5 a' k9 c8 |3 m        return 1! }) Z" A/ I, q1 U1 n
        else:             # 递归条件
    , l3 u' x. t: r! b" ~; X( }2 U        return n * factorial(n-1)5 k. F0 A/ V8 _& A, s$ M0 G/ a0 r
    执行过程(以计算 3! 为例):! k+ ~7 U! Y6 }
    factorial(3)+ Y6 K1 m! a6 O7 Q; Y* u
    3 * factorial(2)
    + s- }" E  v  Y$ J' F; Y" V3 * (2 * factorial(1))
    1 K. ~& e) b' j9 f- _1 ~6 n3 * (2 * (1 * factorial(0)))
    4 o- W- |! w$ n+ e" S" ], I& Q8 C3 * (2 * (1 * 1)) = 6$ F: I$ M+ U5 @" e

    6 U7 ^3 z; \# R; Q5 b' t 递归思维要点. ^- g& }( [( o# T4 \
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 G" t# j  d+ ^7 ~& Y: O: w2. **栈结构**:每次调用都会创建新的栈帧(内存空间)) m$ H* O. e! ]
    3. **递推过程**:不断向下分解问题(递)" O6 f, {4 ?$ v' P6 `
    4. **回溯过程**:组合子问题结果返回(归)
    ! ]1 F& o5 }3 i+ c
    $ M! i7 Z, Y) B/ y9 f# } 注意事项
    ) w1 h3 S+ n; s3 V- `必须要有终止条件  b9 R" t6 o( P" P9 _5 k
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ; Y# O$ {4 C- Q; w5 D# h) O某些问题用递归更直观(如树遍历),但效率可能不如迭代
    % K! f. |0 H. [2 J; b1 [! k尾递归优化可以提升效率(但Python不支持)" k5 b9 ~' v4 |$ i
    4 f+ @5 J( ]4 s! o) J; E4 x
    递归 vs 迭代" @, C/ s1 a+ s6 V. u0 A
    |          | 递归                          | 迭代               |
    ) n: ?4 B! ?( `' \: ~|----------|-----------------------------|------------------|: o/ G6 h8 p* I; W7 D3 ]
    | 实现方式    | 函数自调用                        | 循环结构            |* ]7 S  X0 A% y2 J
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & J! B) @0 s  I# q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ' H6 R3 W: `8 E" s6 l6 {9 p| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    7 T" `( L. z- Z. T! [1 q2 u2 O" w. i# l) m% J# r
    经典递归应用场景# L0 N6 L( N' T& S- |/ X+ m
    1. 文件系统遍历(目录树结构)
    ( w2 n* h7 D7 |* p6 }2. 快速排序/归并排序算法. d5 O6 d+ u; `0 ?# n
    3. 汉诺塔问题) f  o$ V. a+ p- v4 F/ U
    4. 二叉树遍历(前序/中序/后序)9 D0 T" X4 H) h% ~, u
    5. 生成所有可能的组合(回溯算法)7 ]6 n& b; p: E" A$ j% V7 a

    ) E4 b+ }3 n# y$ W试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 06:41
  • 签到天数: 3102 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,& T' Z7 y; W( W4 }* W
    我推理机的核心算法应该是二叉树遍历的变种。
    ) T5 r* q% ^5 g; Y( Z/ v# f' V另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:  c' }& l' h. m2 c+ {3 E- J! A% w
    Key Idea of Recursion0 z/ S1 c# [3 E: v% q2 i  j
    8 r1 Q8 Z% {, ?4 l; ~( h
    A recursive function solves a problem by:- K  E# O  z' m+ |- \% e; q: z: a

    6 V9 N* L) f. v) |) O. k3 L9 n! K8 g    Breaking the problem into smaller instances of the same problem.7 U( z+ Y( Q" f0 K) `' @
    2 \4 s- u3 w: u8 p! J) c
        Solving the smallest instance directly (base case).5 ]6 L7 x2 o) A: J" S
    / J( \3 D# D. x  d  \+ G( u3 X) C3 d
        Combining the results of smaller instances to solve the larger problem.
    ! Q* U9 N5 l( P) \$ x0 Q, S; P/ R6 }
    9 j0 `: D( X% `5 S' ]2 ~& ]Components of a Recursive Function
      \3 r; c* i1 A: Q/ r+ [6 c% D1 F. E, y
    8 h, N% p" E" \    Base Case:
    0 B/ k* @0 Q5 s, p
    $ j4 q; z( R  s. F7 ~        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 M4 g: k6 T* E2 d: t$ t% C
    & m( ?2 i" W$ V        It acts as the stopping condition to prevent infinite recursion.  t# h/ t" O7 I* Q9 U

    + R  k- [# _" u! ]        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.0 Z" ^) _4 Q9 e9 Z& T6 G+ b) \
    $ S; S# o8 b3 d2 H$ C: {
        Recursive Case:
      E- C9 c" d  \$ I+ {1 f2 ~5 i2 c% x% o2 L+ g! C# y5 i" E2 [# n
            This is where the function calls itself with a smaller or simpler version of the problem.4 T  x) V+ G4 j8 C, Z# q! c. L1 E3 E

    " s9 G% G5 y3 P7 f  _* A4 d$ F        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    * k* }0 K; g) _7 y9 Z& |4 C- |2 X5 j3 ^$ ^' u1 H) i- A9 w
    Example: Factorial Calculation
    / }: @6 q5 H4 H4 n; V
    - {, l5 I1 e+ k7 M" n! bThe 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:8 ~3 W1 S' J' s
    & G( n# r  |+ P2 Y) z( W1 q
        Base case: 0! = 1% @/ g) |/ P7 S; J
    " \1 A  l7 ]( i) ?5 M& e; }: C
        Recursive case: n! = n * (n-1)!/ l6 v0 p5 `6 P7 Q5 `
    + ^) f  U, y7 u) x6 f0 q
    Here’s how it looks in code (Python):
    . u- o1 ?8 B' J/ |  O2 Xpython: V+ h) d  R" q' X
    - k. ^# o4 [2 K" F

    # }; j' w/ b/ q  T1 u" O' pdef factorial(n):
    / \8 W( [2 q4 i9 w1 j! k    # Base case
    4 I2 q* t9 }) z4 T) z1 s    if n == 0:
    4 ~! I/ e8 n% J4 {; w& R  ]        return 1
    & m- [" X5 P( P& ~    # Recursive case
    3 X- _; [1 a; s: R8 V) X    else:
    % s( ~7 Z! ?9 r: z" g        return n * factorial(n - 1)$ z5 D$ B, K& Y+ s* y( q8 J& F
    & f. |* B7 a9 M+ t" q
    # Example usage5 e9 g+ r( e$ T& x2 G7 t& k
    print(factorial(5))  # Output: 120
    % K1 r- r# H5 ^) F2 P" U4 D
      X# K0 _/ [1 m+ o6 @+ I1 FHow Recursion Works& L& o; z: c" W& r

    % [* E6 f7 i, M    The function keeps calling itself with smaller inputs until it reaches the base case.+ \" Z& M$ t9 m( P- m+ h9 U

    3 @3 Z. X4 P5 M0 q4 l2 A    Once the base case is reached, the function starts returning values back up the call stack.
    . F9 E. ?% ~9 x" F7 R8 _
    % s; z% K3 }! G) h0 ]4 v    These returned values are combined to produce the final result.
    ) u$ m& n/ U( ?, a2 S) m
    4 c. ?2 _$ m; {' Q5 Y. AFor factorial(5):1 L% ?% _% ]$ I* U
    + }' M1 A+ J8 `: J; `

    1 H( b8 g3 _$ u3 Q2 ^: F0 ofactorial(5) = 5 * factorial(4)- R0 L0 i; W# N2 S; Y4 @3 [0 R4 F+ N
    factorial(4) = 4 * factorial(3). i/ ]# G! v: N! Y+ r3 ~
    factorial(3) = 3 * factorial(2)' i  x# d" \  [" ~4 G  a. d
    factorial(2) = 2 * factorial(1)
    + t( w  M& d3 afactorial(1) = 1 * factorial(0)
    8 X; X7 X8 a, ~* I- [; Wfactorial(0) = 1  # Base case/ H' W; Q. A0 f& ~) K. V

    7 X5 o! `0 x4 W7 [4 CThen, the results are combined:, J5 Y# A# @# p( w- B2 N
    5 Y9 Y; J) a- V

      `, F' V7 S+ i$ Xfactorial(1) = 1 * 1 = 1
    , c- |  n% ?7 f$ \4 ]% \factorial(2) = 2 * 1 = 2
    " \" a' |' e  t) l# Sfactorial(3) = 3 * 2 = 6
    # M2 D. T3 Z$ E  v$ [, w+ k! o! @factorial(4) = 4 * 6 = 24  e6 A% w6 r$ }* x4 V8 y9 m
    factorial(5) = 5 * 24 = 120
    ' W* P: C, H8 l0 `! w  W: W- I* y  T7 o. [! |5 B
    Advantages of Recursion  ~. [; e$ ?8 h, |' D3 J$ s

    9 N5 I6 R5 l& U2 g% l    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).
    # I* m* [$ t* |. ^( A4 _' u, s/ P, v! U$ O2 E  @% r/ z( K3 ~. t+ e
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    9 A9 R1 u$ ], q
    8 m6 v) K: }+ O, I/ QDisadvantages of Recursion2 l# p# M/ @+ |$ ?4 N

    : Z. _' F$ O: J    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.- \, e8 o* V. A2 \% X

    & d" W8 d5 D4 }% e7 Y    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)., T2 q/ O4 Q  v2 p7 X

      M5 a& w8 i5 J: E+ L3 y/ k8 b7 ^When to Use Recursion
    % V: N4 F! n! w! v# y4 J. P; W! R; H
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).1 ~8 z3 o- @, e1 S" I
    8 Q' W2 j, }' R
        Problems with a clear base case and recursive case.
    / H' S; u3 p0 ~# p8 Y- t+ f/ M3 O7 Z9 q1 J0 ~4 N
    Example: Fibonacci Sequence
    9 W4 N$ ^% V. W
    8 Z  k) h% k- G, Z  z; DThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    0 ~" N2 k7 L, D7 `! }8 }* B$ @1 X! s2 o# x" M1 u# v
        Base case: fib(0) = 0, fib(1) = 1
    6 E- m  q3 H* s# |  ^% n9 w! @4 a: R9 J- I
        Recursive case: fib(n) = fib(n-1) + fib(n-2)! a3 N+ `" I4 D! i$ g' S/ O2 K

    5 y: ^6 `3 X. s+ w- Jpython7 ?, _3 E" B# F
    $ G- ]; w5 E. x' A8 ?! d

    ! o7 @5 s3 a. |# X8 Qdef fibonacci(n):+ v5 {# L' `' A
        # Base cases
    0 q  O* A' m. a4 N  y    if n == 0:
    7 f# T  f! k2 |% Z4 ]: v        return 0
    / Q$ Y* a1 J+ `" O: n& b    elif n == 1:
    / W2 k* {$ ]: G: }0 |& }        return 1
      v# T' ]/ n7 ^, `* x    # Recursive case- x7 r6 _$ a: x( s9 q
        else:
    1 W) C7 ^8 R# c! ~. l" i% i% t" V        return fibonacci(n - 1) + fibonacci(n - 2)
    1 d0 d3 ?: \% @) T
    7 t* U" K: E& ?% G1 \" Z, F& n8 A& m# Example usage
    & V; r- o1 D. Zprint(fibonacci(6))  # Output: 8
    + k" Y" z; k8 K5 C: q' Q5 y$ `5 h1 F
    Tail Recursion" i& H5 D0 U$ G5 x

    + t# X' }5 j5 ?, K/ }7 oTail 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).8 z5 _" E: p) _$ t

    3 C  T! E+ B$ A9 t+ eIn 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-27 23:15 , Processed in 0.029933 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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