设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ; Z9 k5 T) C9 G9 ]! O9 k  @% Y  g; ~

    0 d, t* u+ v; q3 f: T. ?解释的不错' p- _  G. n. ^9 l8 J& @
    / O9 R8 W$ @  l9 }) n3 n
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。7 T4 }/ u6 z, `7 ~
    ! K6 u: G# e/ G; Z6 Z2 N2 i
    关键要素
    ; X$ ]( Q* ?6 d% z9 S# E1. **基线条件(Base Case)**. n  z' c" a6 V, z& P" h( _. X
       - 递归终止的条件,防止无限循环
    3 _0 _7 n2 k# E% i: F- w   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 16 n) [5 y" n$ t" R- \/ f

    2 o- f( u, R/ ]+ }, H) a2. **递归条件(Recursive Case)**( ?1 a; }' [  z9 t6 ?
       - 将原问题分解为更小的子问题# p8 X6 m% E* F0 I
       - 例如:n! = n × (n-1)!
    / t) D% h! Z4 v( l
    * I. P/ k+ Y9 g* y: b6 Q' P 经典示例:计算阶乘
    8 W/ x5 b; _% b/ s# I2 N( {. `python2 _5 _( r3 y3 [2 I# s, x
    def factorial(n):) Y$ p/ g9 b& K1 N/ f7 N* o) l" G
        if n == 0:        # 基线条件
    / C/ t/ S& a) c/ f        return 1  E8 U5 }* a8 s4 _& c. T; v
        else:             # 递归条件
    0 n, E3 y, Z$ l" K3 X        return n * factorial(n-1)8 k& L  {9 }" |
    执行过程(以计算 3! 为例):
    & L) \, x) `. m$ i4 Dfactorial(3)
    ! U5 t% ~1 b; X5 H3 * factorial(2)
    # w' R2 q& x9 b) n1 K; @/ u9 v3 * (2 * factorial(1))
    . }/ l$ U/ Y, Q. E' F3 * (2 * (1 * factorial(0)))
    ; b* U) f& W: @+ F9 t+ \3 * (2 * (1 * 1)) = 6
    8 k: F' j3 ~$ e0 W$ P
    3 t6 m" a* E- Q 递归思维要点7 p: U4 v: {$ @8 `8 P
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    1 g' C$ I& l. R& X. s2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    . R0 D% x' E8 I( [3. **递推过程**:不断向下分解问题(递)
    4 k5 _. x1 {( g4. **回溯过程**:组合子问题结果返回(归)* E- @# W( I- ~, a! ]

    ; i# s! q* h7 w4 K: D& L' } 注意事项
    ' N. J" D8 Q) @/ R: Z必须要有终止条件( c. |  v( ^) a/ R: B+ _% I
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    * B# D9 z; c' H6 M5 Y" {7 ]6 T某些问题用递归更直观(如树遍历),但效率可能不如迭代. G- U$ ~& z3 ?  J
    尾递归优化可以提升效率(但Python不支持)& B0 w* ^0 J. Y" A  Q

    & X! q2 ]: k3 A+ N& ], ^& y 递归 vs 迭代
    0 ~0 k( o% ~2 `. U|          | 递归                          | 迭代               |# w$ S* j0 [; A; C) b6 H
    |----------|-----------------------------|------------------|
    ) K, U5 ^5 T1 n5 L) Q- o| 实现方式    | 函数自调用                        | 循环结构            |
    , q  B# I4 M4 J' r" O6 K& e| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 Z  A- i$ ]. y2 S: }& B
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    % v, a, A$ W/ m3 d( x| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |4 f7 I( ]  P3 t6 {  p- S0 v  C

    " `0 x, t2 q4 J( u$ ` 经典递归应用场景! U% q3 i; m) E* _; U
    1. 文件系统遍历(目录树结构)6 l6 _" n' l. j7 y4 d9 Z' ?4 H
    2. 快速排序/归并排序算法
    7 S& [( J& \5 b$ L0 Q3. 汉诺塔问题
    4 U! X  t3 f% _9 z  g4. 二叉树遍历(前序/中序/后序)) g8 x& \7 p' ]* ^  y; E
    5. 生成所有可能的组合(回溯算法)2 r& q9 P( d0 k# I
    . T7 F6 F" B% ], ]5 ~$ H; ?" m4 e
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,- D4 I) V7 A  `% ?& ^% q
    我推理机的核心算法应该是二叉树遍历的变种。
    ' r; W. u5 y5 K/ \另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    , m' Z5 [2 J( F3 `5 e3 XKey Idea of Recursion# t8 j/ f: j- Q  w5 ~6 Q
    9 T3 o' h; w5 x0 h: V4 J" o$ o
    A recursive function solves a problem by:
    1 G$ M1 c( L. ]2 C) Z4 D
    ! p4 ~" ?# O7 h9 I    Breaking the problem into smaller instances of the same problem.
    ( n4 E# W! t" I! k2 Y3 a2 O7 Y( G9 r) M' k! C
        Solving the smallest instance directly (base case).3 p4 X& T8 V1 a( k
      ^, G# o0 z2 \2 D( C6 h
        Combining the results of smaller instances to solve the larger problem.
    6 R) C1 J; D& x/ R, ~& J  q* D" Z8 Z9 G4 R) t
    Components of a Recursive Function
    5 L0 \$ N. Y  D/ u4 a# U
    # `& \; B4 d1 W! g) s  `    Base Case:
    0 z' U8 U0 ?9 \$ v9 \5 A/ D
    6 w5 d  x& e* s6 ~( T+ G        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( S; l( w! y" i. L4 C/ E
    , @( H! j% X: g, H        It acts as the stopping condition to prevent infinite recursion.
    % l3 V, u1 y" Y7 }+ s4 U" _. o* w+ I# X) P3 |
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1." w+ I& _- [5 F- O1 I, g

    ( q& z3 R! A7 n    Recursive Case:
    & |7 [+ c9 ^! ?: p; }) c% ]- P. x" T+ l' ~+ f# z# B# q
            This is where the function calls itself with a smaller or simpler version of the problem.8 o) X  K: M1 B' g0 {  U! J2 _

    & F8 o% }' z1 r( G4 t: D        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 k: \% C# V+ M
    . c2 l# b$ \+ C* u/ q" q# ?
    Example: Factorial Calculation
    + T' ^+ ]3 G& {1 C) g% m3 |* J( U/ s% `8 z+ i
    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:, H+ X4 T( k- f' B7 U6 h
    . K9 Y+ q* l; Z, m2 i
        Base case: 0! = 1
    9 E: I: A- a" g# A
    - J( s2 q9 ?1 l8 j    Recursive case: n! = n * (n-1)!" K) S* n" b' i
    + c4 W% G$ `5 U6 b3 H5 b' m
    Here’s how it looks in code (Python):
    ! U% z9 y, r, S0 }: Npython
    7 p& q& G+ v! H" i8 y2 ~9 e; s- B: G, d! W) G7 o( Z
    " x- |6 ~/ \" p# f! P
    def factorial(n):. f' R: s, z, o& G: w
        # Base case
    9 ]0 L% e9 _% \& X" E# |% P% S    if n == 0:
    . }% F7 O5 V1 c8 _* F6 e; _! E        return 1
    ! ?) L3 j- D4 {) y+ D    # Recursive case
    8 m( p! g, Y* d6 _5 `6 |% J3 a, {- H    else:
    4 ]% T9 t3 z9 E  {5 W/ b+ J/ i        return n * factorial(n - 1)
    ( A* [7 D; }3 H  v- s/ V  S" |8 f2 u! M" H" Q3 {
    # Example usage% A, f4 e& L3 |0 a' y5 L
    print(factorial(5))  # Output: 120$ f* Y6 W. }9 b1 `" E9 r/ t2 C) g

    1 A) l' Z8 g* a5 b, S9 r7 bHow Recursion Works' o) B8 N- k7 d
    - Y/ Z/ v9 M! i+ L0 H! b
        The function keeps calling itself with smaller inputs until it reaches the base case.
    5 }- Q* l8 S2 Q  G4 d$ z0 O
    " I2 j: J5 U4 o" O) H    Once the base case is reached, the function starts returning values back up the call stack.
    % D. G  @/ ?6 V; o' i- D, [$ L$ S9 Y5 g5 R" u
        These returned values are combined to produce the final result." s3 L! S9 v" v9 ?4 O

    - |: g' s% q9 P  Q$ R& e6 UFor factorial(5):
    & Q0 g9 o+ Q8 x( W
    : c* V2 R# z" h7 _. [( x$ {9 |3 O" k- s
    factorial(5) = 5 * factorial(4), \; {- m8 B' W' R- |: [3 _
    factorial(4) = 4 * factorial(3)% ^" L9 o7 N4 k2 U8 ?( D; E7 r1 U
    factorial(3) = 3 * factorial(2)9 O$ X& z# j$ L7 ?+ Z
    factorial(2) = 2 * factorial(1)' l, g- h3 v0 g# @% i
    factorial(1) = 1 * factorial(0)
    6 C; E3 ]  G/ d* m: l& g: b, ofactorial(0) = 1  # Base case
    * B% z' P* v1 Z5 W- e8 a! `* a; T! T4 R5 K
    Then, the results are combined:9 L3 [1 Y) W2 H+ }
    # F) o' v$ ^- q

    " r( I1 \6 ?+ H) q" N" [factorial(1) = 1 * 1 = 1
    0 o- y2 S3 f* O/ g8 o8 Xfactorial(2) = 2 * 1 = 2' L/ Y* |+ G0 ~7 U% N4 z
    factorial(3) = 3 * 2 = 6
    7 [. `: W/ i: [$ Z) v1 p1 Ufactorial(4) = 4 * 6 = 247 w1 `. c- n8 T% Y4 E; l
    factorial(5) = 5 * 24 = 1204 z7 k+ i( {# E' `% G+ h
    + F4 I9 L8 s4 J- r7 E0 r; ]
    Advantages of Recursion
      I+ I7 i3 b0 _6 e. u. }( n6 `" {/ U/ v& d
        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).
    + n5 \7 M/ _" n1 D4 Z- U
    , g2 r3 R% ^# T3 k    Readability: Recursive code can be more readable and concise compared to iterative solutions.. k+ C) J7 o' {) @0 r. D8 d; g2 M) B
    5 p' n+ Z; y6 X$ t3 F2 x
    Disadvantages of Recursion5 j+ S3 ~6 }8 [% C6 i+ n
    ! Y$ t6 e9 X! v8 a9 \# b- y) r" q
        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.
    8 F5 B; g* U4 t, V- G) \) S) b6 k( x
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    4 d/ N: P2 p! f$ q9 V/ U) n7 P8 h1 y8 V
    When to Use Recursion
    ; J1 r4 S) a; e) O) ?5 p* c
    % O. ~2 l2 H3 j6 m% S) V- t    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ( `& l. }$ f+ y( x
    , z9 J# j# C% R, x8 h6 h6 N    Problems with a clear base case and recursive case.
    * X( F2 R+ n/ W% l3 ~
    3 y) b7 F! f' I( E/ b; h3 bExample: Fibonacci Sequence
    & s' y3 [- c6 ~! l' S, O  D
    2 C; r. i  [; P7 ~0 Y3 ^The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    0 G, G! B8 V( K& W6 l
    ; I) j5 x7 b7 [    Base case: fib(0) = 0, fib(1) = 1" L; }; G* c* y" S8 \) H; o
    ( m3 S5 ^1 g7 ~6 i; _- [' G
        Recursive case: fib(n) = fib(n-1) + fib(n-2)/ k) ?! [% W+ t! k$ k/ B. u1 q7 y2 U
    # y" X& K' b' U9 U
    python
    % ~8 F4 G! H, r3 T  N
    ! }% i' ~3 ]7 m; H9 l* M" }
    9 U  `- ]+ W  P) N) tdef fibonacci(n):( ?2 c3 ?7 g* B* f: o
        # Base cases
    % y/ z1 b6 z$ |: r) ~7 }    if n == 0:+ t0 b0 q: [7 p
            return 0, m+ V; E/ G3 ]5 f
        elif n == 1:
    ( \0 \% V8 z0 U& T! ]6 b        return 1
    ; s, D4 U6 ]# d1 p! s    # Recursive case
    + k! k( d4 A/ N    else:
    / j0 l" v' Y7 f- _5 K        return fibonacci(n - 1) + fibonacci(n - 2)/ H& Q- x8 d  j9 ?7 ]: I  p" x
    & J8 D( {, f$ t! g& D- p
    # Example usage3 |  u3 p3 k; X" h( D
    print(fibonacci(6))  # Output: 8' i, [+ J# G" V
    2 }- b- R3 l3 \; G) Y
    Tail Recursion
    % @+ h! z9 Z5 i0 `* D
    1 q- P0 o+ I$ w6 kTail 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).
    0 F7 n9 G; G& A+ ^2 [5 x' n6 j" W6 b# M$ T2 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-5-7 14:45 , Processed in 0.059464 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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