设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 0 _2 [* X# k& b0 o

    % u- q. K7 h) _8 f( g7 G( ]解释的不错% Z: {( S/ I0 H3 v) z- K; t! h1 \
    ' j" Z6 e/ C- F% G$ t+ L
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。5 `2 E+ a! A. [2 D0 g

    5 N9 `2 K( T  f% w) v5 { 关键要素5 R) ~; M+ q, H9 M" F9 C, ]# A1 w
    1. **基线条件(Base Case)**: I- @2 A5 Q- T; j- }# {
       - 递归终止的条件,防止无限循环1 |" o! ]3 F6 x7 }% G* r! \) l
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    9 Y+ M' k, k9 A, X1 p" m% Z
    5 p7 o+ b" t+ |7 ~) Y. z$ u" U9 c' g2. **递归条件(Recursive Case)**1 g' V, Z5 @: b
       - 将原问题分解为更小的子问题
    5 T5 i+ O% S4 }8 Z: e! F2 C5 |( S! U   - 例如:n! = n × (n-1)!2 O- Y2 x1 \4 ^

    3 v" s$ I! N2 [% a 经典示例:计算阶乘
    9 j) {5 q! `+ ]. H$ fpython
    6 O0 m4 u/ G% ?9 S& a7 Mdef factorial(n):' m  X+ |) h% I# Z! m4 I
        if n == 0:        # 基线条件/ p! h8 ~9 ?! d
            return 1
    8 h/ E. P5 a9 M) H# N: j8 t6 i    else:             # 递归条件6 Z7 s8 _& r, B' i7 M) W4 r/ h3 {
            return n * factorial(n-1)6 f% v% G6 k) e$ u  P& Y9 V' t
    执行过程(以计算 3! 为例):! b& o% c# j' L: l% `
    factorial(3)
    ( a* P6 C, L( G) V/ [3 * factorial(2)
    : e! b8 S& X7 A3 * (2 * factorial(1))2 F  Y% f- O7 A2 ~
    3 * (2 * (1 * factorial(0)))
    . K$ j, h! a- W  C# E/ G$ \3 o3 * (2 * (1 * 1)) = 6) U0 R0 a# c$ |( h. e
    + t9 Z" Y% `$ E0 z
    递归思维要点
    1 `/ X1 y7 v/ U: z6 A1. **信任递归**:假设子问题已经解决,专注当前层逻辑) f# v4 _. _! s: E: _
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ g& h; N4 y8 U4 S" j9 y& e8 [
    3. **递推过程**:不断向下分解问题(递); H2 m  V3 a  r" S
    4. **回溯过程**:组合子问题结果返回(归)
    3 H( o' S! k& j' O) k- C+ f% h2 t  m4 E' _2 @
    注意事项$ {: B+ D0 Q" S9 S
    必须要有终止条件
    6 N, `4 |. w/ d/ w/ O" J; Y8 \递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ C9 o: Y8 A' C9 ~2 F  Q2 {8 ^
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    9 |# q0 k8 ~  l5 |, `0 g尾递归优化可以提升效率(但Python不支持)
      ~# i5 c/ ~: r- I; G
    # S: ]; M9 N3 N# ]1 }  J. s 递归 vs 迭代  i) D7 X* G4 P: ]
    |          | 递归                          | 迭代               |. }/ F  q7 D5 R( `: P! O6 b% Y. e
    |----------|-----------------------------|------------------|
    - O9 n4 W% V4 |8 v4 Q1 {| 实现方式    | 函数自调用                        | 循环结构            |
    $ z3 i% A* M3 J  ]| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- W) F5 Q2 ~. G& _- }3 e) I
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |; j) z8 _3 R* d
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 _9 z! }; N7 K3 |; i
    : g6 r% s. I1 E4 O3 @1 _ 经典递归应用场景' _( i; E2 R1 G6 c* U0 g" M
    1. 文件系统遍历(目录树结构)7 H/ I' y6 E5 y  }6 T9 R1 B
    2. 快速排序/归并排序算法
    6 T5 _5 g, F# r3 _8 @3. 汉诺塔问题. I, ]7 s+ |$ V
    4. 二叉树遍历(前序/中序/后序): P" x( K: P% V. z4 o0 h
    5. 生成所有可能的组合(回溯算法)2 h) Q8 ]% N5 O5 s- y7 E/ k9 I

    9 _+ M$ Q3 Z* \) @试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 07:03
  • 签到天数: 3075 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : I. m; F# @: \9 }, W我推理机的核心算法应该是二叉树遍历的变种。- f& u2 _) {, |( 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:( I& C5 Z$ ^: K% }3 i; m
    Key Idea of Recursion0 t2 Q" E" ?" ?" w" w' \
    4 o& `9 `% r) G1 F& ~
    A recursive function solves a problem by:
    ) p( N, D, ~5 ~. |( D
    # K: q3 g& N" V) U5 L& M    Breaking the problem into smaller instances of the same problem.4 d; c9 t3 k) ?' X4 \4 [, l

    3 D$ Y- _  ]5 Z! V8 r* ~! l' L    Solving the smallest instance directly (base case)." e$ H. O! n9 A4 n
    - D: n: n$ L8 u2 u7 l7 [
        Combining the results of smaller instances to solve the larger problem.
    * R8 Z( G. |. j$ Y4 s; G) X0 ?( G5 K& s% x
    Components of a Recursive Function' y# x( P) C" u9 l

    8 O* `, [9 Y+ y3 w    Base Case:
    : |' z! u* t+ r* x& ]2 u+ S' R  Q6 y- S% H
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    , \5 s  S+ A1 p% M& l7 N; X
    / J9 h3 _- c& C% u: c2 p        It acts as the stopping condition to prevent infinite recursion.
      z: w# }' _2 g+ L; o( i
    8 L  c* k& u- w        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.& ~2 \! f; H& ~; j
    ; y5 t" j$ ?7 H; p" l
        Recursive Case:6 p* f  R, \' [. @+ c

    6 E* m5 w9 b, X0 ]* q* X        This is where the function calls itself with a smaller or simpler version of the problem.
    ! c6 [7 {: b% E* x
    : K( L* B2 Z1 X' h4 P        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' s( b% r1 O9 ~& Y
    # r. O& L7 L7 l) p$ u' u
    Example: Factorial Calculation* B; J  g/ E  N! U3 Y+ ?% \' Y  Z
    + F! N# a' F4 v
    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:
    ' Z, c* c3 v6 \$ m  f% t7 K1 r7 O0 ~; f3 ?" Y
        Base case: 0! = 1% C" m8 M+ M' ^  b* ?/ e

    5 T  W0 m% U$ X: s- j: Z+ m    Recursive case: n! = n * (n-1)!
    ! ?/ k' a% A  q
    : D5 o$ |9 Y& M4 O. gHere’s how it looks in code (Python):
    # ^5 t; N, a# R. d  jpython
    * ^% }# s+ D! T9 q' f* [7 C8 J$ b6 Q- B

    % D+ R( h+ G# ?5 Cdef factorial(n):
    ' F: E7 b; k2 k7 l+ r( m/ i5 `) R    # Base case
    ' d4 s7 k' e) O2 J& G    if n == 0:7 l2 i8 G) @) p( n1 ~
            return 1
    6 L5 b3 @+ n. |9 l    # Recursive case& W3 ^+ t4 f6 f: b, G* o8 b
        else:
    $ b$ z: \% M5 p& f        return n * factorial(n - 1)
    : Y5 j& {! T% T; X6 n/ j
    - a5 i7 n; v" M- ~# Example usage6 Q2 E' K. D' Z5 k8 d
    print(factorial(5))  # Output: 1202 d7 l8 D5 `/ [
    ! n% ^% g3 r* W9 s1 e3 J* |
    How Recursion Works. n, n- X, w/ P, l

    7 v% H/ ~: z, ~    The function keeps calling itself with smaller inputs until it reaches the base case.4 x6 c% `+ H. u  Y: O, M

    ( n4 s& z; g$ O4 b3 n* B6 j    Once the base case is reached, the function starts returning values back up the call stack.9 ^, ]! D3 D, D0 E5 V" R  |
    : }( q7 H9 m: B& z5 i3 u4 ^
        These returned values are combined to produce the final result.9 t" S  Y5 N: {6 M+ J, b, K

    8 e' ?4 Z  T' N& P9 z9 H' ^For factorial(5):8 ?! O  Y5 f8 U4 L3 Z, |
    & R5 k, ?! r! n/ p3 C6 h

    . |2 H( k4 R- |1 M7 \: H- b/ ifactorial(5) = 5 * factorial(4)
    1 ?+ p7 K* \$ k9 W, X8 A) Tfactorial(4) = 4 * factorial(3)& c6 [9 V! @$ @1 b
    factorial(3) = 3 * factorial(2)
    ( X7 l* k# J' Z' H* Tfactorial(2) = 2 * factorial(1)1 E5 h' J9 t3 r7 s
    factorial(1) = 1 * factorial(0)" R8 P3 S1 C3 {
    factorial(0) = 1  # Base case
    4 e, X" M; Z" E3 y/ v( T+ d
    . a' Y; x8 f6 _3 S6 RThen, the results are combined:/ \- H$ i7 T# v0 f0 z

    1 R& a1 T# [# h" F: `6 m
    6 E, d( y9 R, V% Kfactorial(1) = 1 * 1 = 1( g! Q2 q3 L" k  y* s$ w
    factorial(2) = 2 * 1 = 2
    - p& w6 ?4 A+ Y. j( kfactorial(3) = 3 * 2 = 65 x# H/ s2 {9 ^8 \
    factorial(4) = 4 * 6 = 24% ~0 K6 j4 U& a3 L! W6 u
    factorial(5) = 5 * 24 = 1208 g: b( u4 v0 z: Y: U  ~3 r

      G1 u; R3 S9 C2 S0 |Advantages of Recursion$ q5 z5 p* ^# m# p) {4 @2 m. q

    ) R, H" g* x" K    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 ?' {  M: z5 D% _! U4 i
    # b2 D! I; V0 \1 f) s! A    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    0 ], V: K& d4 M: _5 C- h* m8 y7 b
    7 |) [( W5 v0 J# fDisadvantages of Recursion
    4 }) g7 m/ D. g; u# A$ l3 \* K' m$ J- R8 y4 I
        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.
    ; s. e; r) d- y5 O4 H) _0 R; ^- E  d. R0 v1 @# E2 Z
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).0 H7 D2 f- }1 Z! m
    6 m) V: z; v; @" o
    When to Use Recursion4 r3 q6 X1 x) `; Y$ C7 k
    # o0 w  }! H. h4 Y
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) f" i/ {# S7 b0 _
    2 ?9 |* \! z) N, G
        Problems with a clear base case and recursive case.& a3 }6 N$ u7 u$ s8 M. z
    ( g8 C- E8 S9 X% D
    Example: Fibonacci Sequence: f2 C- C! }  `4 x$ V
    6 X  l, j  t6 g  i/ Q
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    & m+ C% r- [; Q6 y& `: i. ^1 C9 W- b$ N) `! h4 S1 l! H% p. N
        Base case: fib(0) = 0, fib(1) = 1
    6 n$ h2 Y1 e  \8 I0 A
    6 V0 c" e6 ]6 ~3 ~# e" l, O    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ( @5 S% g8 Y: C, e, Y0 H
    3 Z& D) i7 y/ N9 D5 V# [$ fpython
    , p/ }% U3 x' g1 k, c: X# Y
    ) E% c# X& ]3 q
    5 i$ _8 L4 q! f0 ]' `def fibonacci(n):
    7 H& P6 |3 l  D; F- y. e! r" w    # Base cases
    0 A; }, ^) e+ C+ Z    if n == 0:0 w3 j' o* c- M+ ^
            return 0
    * V/ E! x5 _! E& A0 Z2 {( V- X  q0 f    elif n == 1:: b( Q% C+ V+ ^- _
            return 13 J- Z9 L8 m% r' Q' Y1 e
        # Recursive case
    & n( ~8 v* e- G/ |- }    else:; z( g# Y' }* L- q$ u, }0 c
            return fibonacci(n - 1) + fibonacci(n - 2)& k2 D3 s* N8 f2 J/ E

    7 R- e/ w0 P$ k& y& k# Example usage
    : Z. T1 k; d) m+ U1 }$ N. uprint(fibonacci(6))  # Output: 8
    & H# y7 @  G7 k2 w  x- v3 P9 |: d4 X' l% W
    Tail Recursion
    5 U' M6 s0 H  w. V) c; H2 n8 s: F0 u2 [& o" O/ j
    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).
    ' F9 S4 K: f9 S: w1 E# T! l4 @4 @: }, U  o2 f, }2 \  [, p+ ?( H
    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, 2025-10-26 03:17 , Processed in 0.034952 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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