设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    - b0 g9 N) \# I: c# m' \% P& F0 I: H6 h0 s
    解释的不错
    3 g! W  z" R2 f$ k) D$ T! r7 a
    2 O' ^3 x# K8 x( E* w递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    . Q& V0 }4 U2 b' g4 m& r# f7 G5 H) h$ a7 n
    关键要素- Y, j( I2 [; y+ L% T. d
    1. **基线条件(Base Case)**5 x# @1 h9 K2 Z' q
       - 递归终止的条件,防止无限循环
    / |! Y  @6 t& \4 ~   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 q6 l" i% P! s, _8 t1 ^4 R
    ! Z" q8 h' A6 B4 }; w9 ]
    2. **递归条件(Recursive Case)**7 x" M4 B3 b8 o& ^/ ]5 C1 L
       - 将原问题分解为更小的子问题
    ) p3 c9 m2 [; ~% z/ A   - 例如:n! = n × (n-1)!
    ! \, L5 t) R5 y" V8 n; ^2 q! `3 R0 `
    $ {: s- j0 X0 K; K" }5 l 经典示例:计算阶乘
    % W7 |% h6 d! J- j& ]; }python
    3 B) l4 s/ n9 y" _& e1 Tdef factorial(n):
    1 s( ^7 z" l  {  h& G! v+ W    if n == 0:        # 基线条件% v( o6 P3 K1 @% l* A5 A% S4 z
            return 1
    $ J9 h/ O7 i: s+ [8 g    else:             # 递归条件7 [% }+ R% A! S2 Z+ r
            return n * factorial(n-1)3 M% I+ A! [! s, b. f9 u+ L, i
    执行过程(以计算 3! 为例):( O, J0 F; D+ x- t- D
    factorial(3)
    , ?! o: s4 K2 s) Z4 a6 l3 * factorial(2)  w( Y+ r: D9 ^* \+ p# T7 Z0 x
    3 * (2 * factorial(1))
    : I* V0 T% s* M2 |4 K. D! w3 * (2 * (1 * factorial(0)))$ ^- F2 ^4 P9 S) e& i! t; P' G( |
    3 * (2 * (1 * 1)) = 68 d' \* o" D/ |1 u# D9 S4 a
    ( C! d% D/ ?( c4 `
    递归思维要点4 j9 |* E4 ]. a
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    6 {6 M. G9 T& k9 @( y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    2 m" P* Y+ [4 }3. **递推过程**:不断向下分解问题(递)
    5 s$ B$ l/ E0 H6 O4. **回溯过程**:组合子问题结果返回(归)
    # `& k# d0 `& B! |
    # P! r& I. M, m( L 注意事项
    ! C' c. V2 G+ K9 r2 G1 t/ n必须要有终止条件3 Q4 {" [+ p4 I6 l. r9 o# g
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
      z$ T' n8 k" G$ T某些问题用递归更直观(如树遍历),但效率可能不如迭代
    / x% O% ~. H; t! h" ?7 a) ]! b尾递归优化可以提升效率(但Python不支持)  {2 _, {6 L7 \
    " o! l1 u. B9 Y4 F6 |
    递归 vs 迭代7 C* _: S2 |! C+ a2 j: J4 z; T- K
    |          | 递归                          | 迭代               |9 \4 Q/ o) R) V) Z7 P2 b
    |----------|-----------------------------|------------------|1 Z7 D) p# y: J7 C
    | 实现方式    | 函数自调用                        | 循环结构            |% \3 M* Y% R7 e5 f/ k6 E
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |( T( m: z5 Q. `* t6 j
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 ~, U  v5 ]& m5 y| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    * j) x! {/ V: J1 A- R  `3 l$ e- ]2 Z. U
    经典递归应用场景
    ! @& a  E* C% E1. 文件系统遍历(目录树结构)+ j6 c# M+ X2 l0 m
    2. 快速排序/归并排序算法% n; ?$ ?% t7 W
    3. 汉诺塔问题  M5 ^% @$ J% u6 n
    4. 二叉树遍历(前序/中序/后序)
    ( ^9 B$ s; @" B: D9 U' Y0 j$ S% e) t5. 生成所有可能的组合(回溯算法)
    2 q7 _; x# F" b/ E
    * P+ W' G5 l1 P2 F! F试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ N$ W$ y% W- A4 c( J$ d9 i- l
    我推理机的核心算法应该是二叉树遍历的变种。  I8 F3 |$ p( }& o
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    $ X# t  U- k- j% |$ u' rKey Idea of Recursion
    ; B8 F) |1 N( k' l" d2 L# ~
    4 g3 [  f: r4 f7 a5 b; ^/ W! FA recursive function solves a problem by:2 v5 h# A% H# A4 V+ U  r6 `

    ) o# U" ~7 p) d4 L    Breaking the problem into smaller instances of the same problem.8 A$ y' H+ v8 N5 {  N
    ! d7 Y/ @/ n' ]/ F+ ?1 c* A9 c4 [9 z
        Solving the smallest instance directly (base case).
    ' j, y  ~  _, O: s1 ?
    ; S: f! ]* z, r  J: D: W    Combining the results of smaller instances to solve the larger problem.+ A8 A$ s  c/ \+ S4 l

    ' Z+ G5 t" N0 UComponents of a Recursive Function
    . j- ]. O: ~6 Z; l( ]9 _) a3 r8 h
    / G( Q: C3 |! W9 l3 O* v    Base Case:
    * x8 g! D& m( w5 S" P. g# ]- e9 [- A  l9 z6 D
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ! l! `& h, L2 L* I. v- M% Q+ k
    9 `; O/ z, Z& q5 X& K" w& Q. e, g$ L% m        It acts as the stopping condition to prevent infinite recursion.) V/ y6 S0 ]/ ^
    ' S/ y  w# x  @, X9 ^
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    8 k- _+ V' S# P1 u! L% s1 q" i! J' ]4 P) R! P
        Recursive Case:) P! u' A. q2 `6 r* k) h. ~
    8 ]2 m1 X9 X& \( O
            This is where the function calls itself with a smaller or simpler version of the problem.# L7 c0 n( Y. O# l) ~( _

    3 A( j% ^1 h. |5 M' p0 \        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    0 g" k' p6 G" C$ e8 f7 b
    4 Q9 A' b1 a" s+ tExample: Factorial Calculation+ l, O& @: i/ C( J, x# c
    - [3 s" W: `; n) r3 g5 K" c( `7 N6 W
    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:
    0 m, R/ }7 ^# h' K+ ?) M& f# o. j4 @6 _$ \& y$ l3 b
        Base case: 0! = 18 P3 t  a. H) S5 u! W
    ' u$ {! f8 R: B6 b! E+ k
        Recursive case: n! = n * (n-1)!
    4 ^6 x. i1 B4 U2 J, D9 c4 Z' N& }1 g
    Here’s how it looks in code (Python):4 b, `. n1 X* b; G2 q: Z
    python8 K! L. ~; h0 H0 ]

    , V$ B+ X$ m5 P( b6 j) |& a: ~2 N/ B% o* s! s
    def factorial(n):
    # I; Q8 m9 h+ ]. i# |- x7 j    # Base case
    4 y. r+ Z6 g7 T% q) ]: c    if n == 0:! S; i3 Y& @5 c- f7 |- s# X9 B
            return 1  a) V# D, R. [. v
        # Recursive case
    # ^/ e% o1 B4 s" c    else:, S; r& a4 f7 L) m3 C3 s
            return n * factorial(n - 1)) U, V# l0 H# _7 @2 |. K
    $ M9 P. a  ~# `- i
    # Example usage2 e& d- F6 M, O" P% [: P
    print(factorial(5))  # Output: 120
    " c: q6 i3 u* p2 s5 h$ g( H% K! X8 w/ i' W. w; I
    How Recursion Works0 Y5 Z3 A& I3 m) q1 K: E. y
    8 N9 P- w9 V) p6 t9 m$ {: f
        The function keeps calling itself with smaller inputs until it reaches the base case.
    " T  {! P1 Z7 N; ~# C$ Y5 m2 G4 b. p4 D
        Once the base case is reached, the function starts returning values back up the call stack.# E4 F! i0 A! J
    4 W4 Y6 k; U; h5 @
        These returned values are combined to produce the final result.8 Q5 h+ X* C" u; `6 ]9 N

    0 w9 p1 [- @3 b( ^/ ]For factorial(5):  h2 P) T  c6 z' I0 D

    ! ^  E/ @1 E' b* e# s
      y! ^0 u6 V8 e* mfactorial(5) = 5 * factorial(4)$ g2 H9 x6 F7 w; Z
    factorial(4) = 4 * factorial(3)
    ; q' w4 W. s/ lfactorial(3) = 3 * factorial(2)
    6 P$ z% X8 ~9 r3 Tfactorial(2) = 2 * factorial(1)
    , {3 V& M% }; m3 O. A9 l! r  I$ afactorial(1) = 1 * factorial(0)1 ~0 p% m) M4 P: y/ w) e0 e  D- o
    factorial(0) = 1  # Base case, X9 G: M7 ~  \
    : q4 z1 s3 `7 z) O. q
    Then, the results are combined:
    # p) ~8 ~# F8 O% l; B( A  V% }4 M" U' M# Z6 o/ U4 X

    7 y2 X4 v9 ^* ^factorial(1) = 1 * 1 = 1# P# w# z0 P) y3 k, F& h6 Q4 |4 `
    factorial(2) = 2 * 1 = 2
    6 f; [, G7 n, [* X$ T1 i; `factorial(3) = 3 * 2 = 6
    2 B" h0 j( |4 g7 z* sfactorial(4) = 4 * 6 = 24
    2 ^$ X# e) k+ [0 N# R: a# hfactorial(5) = 5 * 24 = 120
    ( x* j% u6 P% G3 [+ o* M4 n7 S" |$ G1 Z! g) _' `, k$ Q0 x
    Advantages of Recursion
    " }) i" H5 ?, \0 W& l) u- ~
    : A  b7 y3 ^% O& q. ?: W8 u( A    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).- x% f, q# q0 M8 P9 l2 Z+ E

    ( L0 y, q  o7 G- N  }: B    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , e8 H2 Y( I8 c$ O' C. \, I' P
    - X! o# K0 ~8 Z' dDisadvantages of Recursion
    0 `2 l2 D+ f' Q. y; D5 N; `" \- q$ ~$ {$ ~7 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.
    6 K# c! H! M* u5 C9 L6 J* B0 ]7 [8 p) f. B4 V! k" z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)./ |& I4 @3 i/ C- ~
    $ b, M* z0 U. y, h
    When to Use Recursion7 T8 Y6 ~: ]6 F) O

    0 `* V: ^# {4 K: e0 r$ X; C8 L    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 }8 L+ m# D0 g' h: Y5 b

    ; V- t2 M! \4 [+ O, a# X* n    Problems with a clear base case and recursive case.
    4 Q9 b' j5 U0 O1 c
    . e! U3 A/ I4 `Example: Fibonacci Sequence
      p5 m+ h0 d7 v2 e0 x! m$ n
    + {5 J+ c5 R, |0 q- M( q$ AThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:$ ~8 N8 d+ S" O1 w! O5 {$ o
    0 v- Q% `6 A( z; F; F- Y( x
        Base case: fib(0) = 0, fib(1) = 1; G8 ^8 T" R5 Q9 m4 M
    $ b) S* I6 E( ~8 s& ~  L1 R8 ?' e  b
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    % H! ~$ c: G" g! I# s) W2 p: s' x& F- ^# Y
    python
    ( g% V: l! d' V* T+ |
    / j$ _5 \. V( b( W' s8 [1 E0 X3 y
    def fibonacci(n):
    " t; a% h9 w9 A4 W0 m" l2 T9 Q. N    # Base cases
    0 X( ~2 Q+ I+ x& b2 t    if n == 0:
    % l+ j  S. h* s0 V" s        return 0& t0 T# I, J- Q# D" I
        elif n == 1:
    / w1 t% {; h6 t& [" m        return 1* Z) P2 Y+ l- k/ B' Q
        # Recursive case
    ( x, A! X$ n& e" g# x    else:
    & y& Z. x* U2 @$ i" A. t" F! n        return fibonacci(n - 1) + fibonacci(n - 2)
    7 P7 K6 P0 r7 x$ r3 h9 i+ e9 ^7 }  i# K" U% P* [/ ?  z0 ~7 ?
    # Example usage
      Q- K; h1 L& P5 ]8 Tprint(fibonacci(6))  # Output: 8# k) g9 v& w# M# G  N+ D% j9 f7 q% @) M

      t+ Y' r1 p. w0 FTail Recursion5 {+ f2 }* R' l! |3 \- x* D+ O

      A+ N, t" Z6 c& ~  s4 ZTail 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)./ L/ S; H$ R* C! k8 ?* g

    ; \" z6 c! T2 K7 S' hIn 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-12-7 05:10 , Processed in 0.035852 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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