设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! ~( p; W& y/ a2 K7 b7 Z
    . H# ~  ?' _. H, o9 Q- Z解释的不错
    7 q1 ^  ^9 j$ A5 o
    ) F3 R* C! j& o1 ?+ n% |: J递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ' z0 O+ A& U! Y/ e- x
    ! P8 B; e" Z3 K9 F) x, q) b 关键要素
    ! q2 T5 C- A/ g% G# b* S1. **基线条件(Base Case)**
    5 l4 E! U9 \2 `" w9 [3 E   - 递归终止的条件,防止无限循环. _0 r* L7 M+ U' ~* {- t
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% t2 q( c' O4 Z' R# ]7 ]: P
    % V+ z3 [9 W4 |- K6 _+ p7 j
    2. **递归条件(Recursive Case)**
    ; V+ L! Z' v- @% _0 A3 C1 |   - 将原问题分解为更小的子问题- \  N0 {. i2 _
       - 例如:n! = n × (n-1)!
    + W! y0 }2 z3 q( a) J" V9 \; y1 G$ `/ B- r
    经典示例:计算阶乘
    : |4 X4 K4 C" N+ Upython7 X8 |3 a) |! ]# ^( O
    def factorial(n):! S. k* H+ @0 g3 `
        if n == 0:        # 基线条件
    / W* H  p( a. B& j; P6 n: g        return 1
    ( J9 u; o# x3 p    else:             # 递归条件9 O7 b) [: I) ]& |, c
            return n * factorial(n-1)% c& d# X5 F. ~2 Y/ r+ T5 H9 y6 J
    执行过程(以计算 3! 为例):
    3 E' W8 e% U$ q" G; dfactorial(3)
    2 A4 {' t* _, F5 J3 * factorial(2)( S$ J% e( f- t  f/ O
    3 * (2 * factorial(1))9 u2 h6 ~. M, s+ T, F( ]
    3 * (2 * (1 * factorial(0)))
    8 t% k) N: B0 M/ A3 * (2 * (1 * 1)) = 6
    ; O% P4 l8 T- B% D4 g* F' n) v& J( Z# V4 F& E
    递归思维要点
    5 G: Z5 n+ W; \1. **信任递归**:假设子问题已经解决,专注当前层逻辑: U* ^/ n7 b; y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    1 ?' V3 ?2 j. v# m3. **递推过程**:不断向下分解问题(递)
    8 c) u- F! ^: x* {$ P6 `2 ^% R4. **回溯过程**:组合子问题结果返回(归)
    4 A2 U& `$ |' p; ^
      A. I# m6 I1 j! A3 A 注意事项
    ' l7 l+ q+ H: J; r; S8 |! ?必须要有终止条件
    5 P1 }# X6 K! `0 O递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( ]- |* \) i6 B; Y某些问题用递归更直观(如树遍历),但效率可能不如迭代9 a4 Z* d( p1 S, D- X
    尾递归优化可以提升效率(但Python不支持)' }5 K4 i" i/ n. V3 j8 C$ R
    ( |$ z1 x% H" Z8 _7 X3 f' y/ F
    递归 vs 迭代
    ! R# u1 R; l3 P- ?$ S7 C) E|          | 递归                          | 迭代               |8 ~  }0 N5 M5 _
    |----------|-----------------------------|------------------|
    6 o- T6 R- e  [4 {| 实现方式    | 函数自调用                        | 循环结构            |
    ) @6 n2 X% m0 d| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- Z$ m5 C4 W% c5 y* i9 I2 [+ z
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * X3 I5 j3 Z6 x9 K9 R| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 [8 U* {" h5 t4 {5 j- \$ C+ T! u, C. m3 V* Y- Y$ Q3 ?
    经典递归应用场景
    7 c/ u; Z) Z( ^1 [; B1 Z1. 文件系统遍历(目录树结构)
    : h% _& D  @1 k' P2. 快速排序/归并排序算法4 Y5 r! {# C3 V% v. w1 |. Y) y2 t
    3. 汉诺塔问题; C) w2 V7 x/ @6 N
    4. 二叉树遍历(前序/中序/后序)
    . ~6 \9 Z. K4 B/ Z5 V$ \5. 生成所有可能的组合(回溯算法)/ \8 E7 w/ C$ m/ d! s
      y: e, Q- q9 s, g: x4 c) p+ n
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 14:35
  • 签到天数: 3132 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,; @+ Z! `0 r0 l
    我推理机的核心算法应该是二叉树遍历的变种。# g, M  A0 Y2 c/ g+ P
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    + d- b+ ]6 d* Z9 OKey Idea of Recursion( u0 F' f( m5 }) h/ K3 G0 `  D

    0 F* R0 n; F  t# G# a' c" h% y/ WA recursive function solves a problem by:- y* G7 J( o7 c: x  p7 {4 }$ R  i

    * n+ V  _! |& O3 A7 [6 f+ l: I3 }    Breaking the problem into smaller instances of the same problem.
    + u5 H3 v4 C' M/ t; I; S3 {9 t+ \3 p: K6 T/ e- b7 j& w$ Q
        Solving the smallest instance directly (base case).7 g3 `! R; J6 D2 L4 i8 p' F

    : E# ~. X3 P% Q9 w" T    Combining the results of smaller instances to solve the larger problem.
    ; C3 `, m# v  w% Y% U
    1 a1 e* E( Z) \2 K  LComponents of a Recursive Function
    : q  J4 _% m" p6 E. i  a; M
    ( N  O1 j! N' j" O! U! a    Base Case:
    - p( ?0 G( X+ e
    ( I5 D3 A: F( A0 G        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    % [; F$ p* _* M  T0 r) L3 w# v" [1 h
            It acts as the stopping condition to prevent infinite recursion.- S2 T6 d8 {' K- j$ g
    $ G5 V5 O: e( P: v$ t+ Y8 E
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    : Y# K+ D& v$ b  N! v0 A: Q6 e4 N" t! _+ S8 a: P) \
        Recursive Case:* Y+ ^0 |0 A, K

    % a8 g% S" Y) v+ \9 c        This is where the function calls itself with a smaller or simpler version of the problem.( {$ X8 c* A& H1 u; L: a8 J2 v. ?

    $ `4 K" J& x! @, n        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ! V2 V5 r! S6 A  U: n, v1 V' f+ K- N! l* _, D
    Example: Factorial Calculation
    4 q- q( `3 q. `, p' G
    1 B0 U' _- d7 d9 k& u- NThe 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:0 W6 A4 ?6 l8 L) `* F0 Q
    ; T6 _) Y: \6 e' g. A- n* d
        Base case: 0! = 1/ V. Z: m- T4 G# G' L5 D

    3 E% S2 T5 ^* b5 |  L    Recursive case: n! = n * (n-1)!
    / i8 [9 n; a* h% p( e
    7 q8 b- S$ R+ S2 Z8 jHere’s how it looks in code (Python):6 D# i% I* i; O; w9 ]
    python3 z3 |4 H1 T3 W+ i1 t/ R

    4 m, @) {* I5 Q- r0 P) C" x5 c
    + b( e( b- S7 kdef factorial(n):
    & V/ c. m0 O  |: ^! [6 A  h    # Base case
    + h- i% V5 L# t6 P1 y5 t, Y    if n == 0:
    3 {' ]- z; o. x& n( @; a7 t        return 1
    ( j& S; e3 p  ?4 Z& g8 m# `    # Recursive case
    1 o- i* M' H7 G/ O, d    else:
    + Q! a0 q) E- J) Z" [        return n * factorial(n - 1)4 E6 T+ q3 R  _3 e- \, l

    ' |; \& Q% B# Y% i( h) U- i$ E5 U# Example usage
    ; R  b( {( w0 U5 z0 V# Zprint(factorial(5))  # Output: 1208 Y: R7 [# F6 V9 \
    $ E/ V1 C, ?: S7 z" a
    How Recursion Works
    , \3 I, w7 R4 L  u* T( E' ]6 y  }3 C( r8 Y. V6 n
        The function keeps calling itself with smaller inputs until it reaches the base case.
    2 I. \; W) ~; O, B7 a' K: @2 \$ G4 s5 V
        Once the base case is reached, the function starts returning values back up the call stack.& v* S9 Y  H) s) c" c# k8 N
    * U" T* v) g; a5 N/ Q* o, ?4 s: z
        These returned values are combined to produce the final result.
    * C: t5 x% w! M8 y* A
    - w5 z/ ^  C0 a/ T. Q1 xFor factorial(5):  B- N' J7 \9 N" y' L
    . w2 o6 l  v* |9 d' g! \
    9 y: t6 q) ^/ s) s
    factorial(5) = 5 * factorial(4)
    4 @9 L- c! k5 F+ N  b  ?% p8 B7 `factorial(4) = 4 * factorial(3)
    2 h' @" f2 L7 Y! X' sfactorial(3) = 3 * factorial(2)
    7 ?4 v) {! G5 p: K. s: F1 zfactorial(2) = 2 * factorial(1)
    ! J: Y" L1 T5 L. Y$ z% Rfactorial(1) = 1 * factorial(0)( N1 V) B/ a# E5 D' U# w7 P! t
    factorial(0) = 1  # Base case7 r6 k; Q: l& `6 I% U+ O" w" Y

    * G. @& I" ?4 ~9 l3 s, \Then, the results are combined:# r  i- }9 L+ q( Q+ v  D% H* w
    ; p/ K0 c. ~1 p9 C; I
    / W0 D) ?* K9 i+ M4 e9 c
    factorial(1) = 1 * 1 = 1' @% a' N0 H7 }& C( L/ U
    factorial(2) = 2 * 1 = 24 u& a7 g# j# e$ g2 Q' O
    factorial(3) = 3 * 2 = 6
    $ ?1 _+ f+ \1 _' N9 Z( K0 bfactorial(4) = 4 * 6 = 24
    5 k: ?3 a+ d* [5 ]0 u" Xfactorial(5) = 5 * 24 = 120% S2 G0 N/ \- }' C

    + C% o1 R; v0 W& I, F8 d1 uAdvantages of Recursion( `0 l; R+ i2 z  G. }

    - Z. X4 Q; G; N  `    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).
    + F: [8 d' I% S4 r$ e  J; T
    + L: V7 R9 Z- T0 {    Readability: Recursive code can be more readable and concise compared to iterative solutions.3 m8 H+ W' b: W6 n* \- s9 n

    & i5 j: m. D1 \Disadvantages of Recursion' ~# G, @) l. ]5 R9 ~/ ]& K
    2 C5 Z8 e  c' c$ 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.
    2 {9 @, t: [: f! e% v/ i) o( Z6 A  `8 x
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).  A" k1 f: L3 m" a! k# f& ~& f  V

    & ^! `/ M" {/ _! uWhen to Use Recursion
    2 d) V: F6 ~2 N; m- q; f. P0 S) q6 \% f1 h, v
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ' c5 }; d, e6 a$ h: L" p( c
    2 n+ R" D" a  [0 X3 c    Problems with a clear base case and recursive case.+ P, J* L/ q% x: L
    3 J% v- R1 G( D+ s. f0 A' W! v5 v
    Example: Fibonacci Sequence' C) J  f0 L, i: R4 \. P% o
    ( H( E0 P+ @9 T# ~) X
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:3 P2 g. U* W8 K: A3 U7 l4 b& q; U

    & @# b/ o- y( l8 M! R    Base case: fib(0) = 0, fib(1) = 1) e4 E' A! _0 e

    7 `0 `  @( Z* E/ T! F0 }    Recursive case: fib(n) = fib(n-1) + fib(n-2)  P2 \* W5 x) [
    3 a; B* A/ v* e- G
    python
    6 y( N% Q$ b7 E* H$ s, Z6 A, T( ^8 \
    ' p$ `: H4 v" D- z5 r
    def fibonacci(n):& A% F( p: ]+ W  K
        # Base cases
    # \5 g. D6 ^& `9 a1 [/ p' {    if n == 0:
    2 Z; F' O4 S8 y3 L        return 0
    ; R: A( q9 O+ u& |- A" ?+ f4 [' h% X    elif n == 1:8 Q8 z; y  M" d* n$ y. P' U/ J5 ]$ V
            return 14 o2 ], |6 E0 Q# |. w2 P. K& \8 y
        # Recursive case
    * X' Y  V; o: B; t; g3 D4 v6 c    else:
    4 }) ^3 R( `( J% X7 N$ \7 S        return fibonacci(n - 1) + fibonacci(n - 2)# H& E& B! {4 \

    & w& _* ]/ ]$ Z+ H7 h& C# Example usage
    4 P0 L1 G0 i7 M% cprint(fibonacci(6))  # Output: 8
    4 I* F- b2 e0 @& N  J# E
    # N3 l0 C; r* ^) I) y% ^% }9 _Tail Recursion
    6 U* G1 ~$ {3 c% s' p" Z: E
    4 x7 t. Y- {  a) y$ n7 hTail 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).6 B6 C. S2 a6 J5 S

    ! n! _9 ^8 q3 d) B* n4 H7 s2 BIn 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-1-1 09:29 , Processed in 0.032359 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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