设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 [, R( _3 i* U% L9 o7 ~* s( s9 {, D1 N. p
    解释的不错
    5 _2 b3 D3 |& b# n2 P
    8 F. \9 h0 z3 H' F; ]6 L递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。2 z& V2 h) L4 z8 i; O

      `( p( X' I) X' O2 ]# A( m 关键要素. ?3 Z- B7 y2 w7 b) v" f2 `/ Z
    1. **基线条件(Base Case)**: d) ^; T  q  O2 r* R" J- @
       - 递归终止的条件,防止无限循环
    * ^1 k: l1 L  N4 Z   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ( _/ i  a7 Q& K% E* Q# L" f7 x. x; `0 M3 J- f2 o
    2. **递归条件(Recursive Case)**
    ) j; h' ~3 w8 ], b, B5 O# e   - 将原问题分解为更小的子问题3 N! p2 W' v5 b$ |# L; ?
       - 例如:n! = n × (n-1)!/ B. B/ d' }6 t/ r  u
    * q7 W/ [+ ]/ e, L5 D# s
    经典示例:计算阶乘3 I0 j& A8 j  [9 U( x& l
    python
      M- F: I+ Z% X2 u: i, Ndef factorial(n):$ W% f- x7 ~2 x* T) U' P
        if n == 0:        # 基线条件5 O) I) w" @- C  p5 J
            return 1
      P0 [1 C' G5 Z1 c/ n+ d    else:             # 递归条件
    ( Q4 V2 O' b. x8 b( Z1 _' @        return n * factorial(n-1)1 }3 L$ r: E7 b  b
    执行过程(以计算 3! 为例):
    ( b% t; g1 F8 Sfactorial(3); i# }7 J! B- l2 {+ Q5 R( }
    3 * factorial(2)5 N3 ?: f8 E  _+ x) c
    3 * (2 * factorial(1))/ Q& o( k' E1 r7 B
    3 * (2 * (1 * factorial(0)))# B8 K7 V; T5 a4 E0 r0 r
    3 * (2 * (1 * 1)) = 6  [, k* n- e5 S; G* k
    4 g$ K0 ~# a$ f, |8 f, ]
    递归思维要点! E8 \! S/ N; L) V( T5 F* d
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . g! n* N4 Q" A8 |8 ^: C' a7 d( c2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    # A- A! g. l1 x7 F6 P4 s3. **递推过程**:不断向下分解问题(递). x4 `1 Q; _# R; }" _6 l  h3 w# E4 R
    4. **回溯过程**:组合子问题结果返回(归)
    / T7 g) y5 i& _1 j2 L# V. f
    4 H- F, v8 K* ` 注意事项, |+ ^9 u/ Z: {5 a
    必须要有终止条件
    9 s  v- q6 {' X3 p& R, U递归深度过大可能导致栈溢出(Python默认递归深度约1000层), I6 j4 Z3 _/ L4 H9 ]4 |
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    / o: \3 j0 N( V- U9 x/ C$ j尾递归优化可以提升效率(但Python不支持)# ~2 r/ R0 |9 J- _4 x+ y3 X6 i$ b
    # K0 X2 p1 t! J$ |7 ~: a
    递归 vs 迭代
    1 Z4 @+ {# A/ ?8 y- b2 M  n|          | 递归                          | 迭代               |9 ^- _, |$ b& P4 C3 c- P! b  c
    |----------|-----------------------------|------------------|. I3 I' `- {) w  h9 b2 W: d
    | 实现方式    | 函数自调用                        | 循环结构            |% L/ b- n8 t. |) [+ Y$ [
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 l! D* r( d0 `. M( p, s| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    1 Y1 @% [+ ^% g- n; H| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 w+ a  p$ o5 f
    5 o; R) t3 b. q# z2 K/ p 经典递归应用场景: Y& a! j+ F" b
    1. 文件系统遍历(目录树结构)5 _  v6 ]* R) f9 b  q" h/ \
    2. 快速排序/归并排序算法
    . D& ^4 X2 ]7 M5 ^" J: H3. 汉诺塔问题5 o# A4 c8 G0 X& x* z5 t8 }5 a! v
    4. 二叉树遍历(前序/中序/后序)
    0 {, Z* U0 }' X" b: ~5. 生成所有可能的组合(回溯算法)
    . P0 k! U3 H) n9 x. U3 o* |
    3 a6 P: m& Q5 X  s试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 07:11
  • 签到天数: 3150 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. ^9 L4 w. u8 t! y0 L
    我推理机的核心算法应该是二叉树遍历的变种。, a# h  w' X3 b1 ~* ^8 q: b+ D
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:. v+ ^) E. z% W& n) K: u
    Key Idea of Recursion4 M- F9 o8 r1 J  P+ a8 ~

    . J) e7 ]' E# ~1 o2 a6 s$ ~A recursive function solves a problem by:
    1 K2 u9 J9 u; q7 @9 l& a) f" N, G5 Z* [1 e1 W) K$ k0 v2 o
        Breaking the problem into smaller instances of the same problem.& g  m, T" v% I$ u3 |5 [3 p) H8 T" X

    4 A! p) ^$ p4 G' e5 @& }& `    Solving the smallest instance directly (base case).: ~/ E( x. Y; X3 T. R9 U0 o" o

    ( n6 n; o' V2 S3 ^6 E    Combining the results of smaller instances to solve the larger problem.
    * F; {7 X' q6 T, K
    " ?+ J1 w! b- P/ _$ @2 v$ t4 jComponents of a Recursive Function, y1 A) z" _2 F3 W

    6 I/ E1 I  e6 v# N    Base Case:
    ! p9 u! e  s7 T  h8 U3 I
    / }2 \1 I! U: l7 ]& c( L3 g( f# o        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & @7 Q( q- N' _6 o. k) G
    4 m; P  V# ^  j+ m% U0 N6 {        It acts as the stopping condition to prevent infinite recursion.
    1 {/ b- C( ]+ ]) K( O8 V/ X5 s( L* `, x
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    1 A) B9 x! e* Y- i" B+ {3 K$ ]8 @% p0 i; b, w1 p" V% g
        Recursive Case:6 ]: X1 y; F  V. r: F7 d

    + j* e% {$ `, A, b: f! p        This is where the function calls itself with a smaller or simpler version of the problem.6 p" R- M7 c! `) Y, S

    % l! q, X: V& M7 ^! \        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# S2 b/ ~: O& q* F3 N6 R
    ; ~4 r% f4 p! u& j( V
    Example: Factorial Calculation
    2 S! Z% y2 T, B% ]( ]8 j2 I' s
    9 `) l. e7 I9 O- KThe 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:$ u( U7 J! r( H$ i# B8 d

    , w! J1 x% `% i5 o    Base case: 0! = 1
    " w- i' p5 \) q4 }& f/ q
    " i4 N& p& y7 D    Recursive case: n! = n * (n-1)!
    $ r" S( ~& \9 z9 [+ c) {/ H" U& o( `1 Z5 z6 R+ c" r6 I
    Here’s how it looks in code (Python):5 Y& o9 T4 Q# o1 p' W- I- K$ n: `
    python
    3 X0 G# T1 u/ _! j6 C8 @+ p! p, `+ Z! B  e
    ( Z  K( K- t6 k* ]$ A% F& |3 u
    def factorial(n):
    4 _  o1 G! i# j% H9 n% j: ]% o2 q    # Base case2 \8 G/ e# C$ l4 D
        if n == 0:
    & z9 T# U. \- u4 X  i3 ~' @( r7 A- }# W        return 1; K" {$ w. h' Z) k, Q# ?
        # Recursive case
    5 A! z4 F( q, r/ F. F( }- z    else:
    - t6 q! v4 C4 A/ ^$ D, }' }        return n * factorial(n - 1)
    ( u- x3 B$ s8 h: K3 q
    , O+ G, f% q4 j/ F# A# Example usage+ ~* H& p/ s1 [
    print(factorial(5))  # Output: 120
    ( P1 B4 j! K, x; i/ _
    . {4 p- y) B5 K0 S* W1 }+ |! F* v  PHow Recursion Works
    " i# v: g  q$ z+ m; }. l" C: x
    " v! p# L3 W8 g2 j( o0 p, V    The function keeps calling itself with smaller inputs until it reaches the base case.$ z5 G, o& @! w, D3 |7 J* s

    9 D% Q( h8 k5 t& h8 A% c7 _9 u    Once the base case is reached, the function starts returning values back up the call stack.
    4 K% g# W9 k( T+ n  Y/ }3 n9 b' L0 o2 o$ T) _' j5 G/ `
        These returned values are combined to produce the final result.
    # t. p3 L% m; X5 h3 z. @. V5 Q
    & H4 U+ O, D1 J+ k3 F- [1 `( uFor factorial(5):1 Y1 C* }$ O6 a! ], r3 P

    . Q7 B, r# b1 c4 A9 Q1 j0 L3 ]
    1 F6 W& `! |, H& x2 Jfactorial(5) = 5 * factorial(4)/ y4 b8 ~& r7 D/ D
    factorial(4) = 4 * factorial(3)+ A. Y- j. U; l6 K' p: ^
    factorial(3) = 3 * factorial(2)( Y' @6 ^0 k& _1 ^# d
    factorial(2) = 2 * factorial(1)
    $ S. m% h. I9 \8 i  D, ]factorial(1) = 1 * factorial(0)
    4 _/ ]# t) v/ T$ {3 p/ A6 D! F. ]! afactorial(0) = 1  # Base case
    " o, r1 h: k1 i: V7 o9 e! u, L, P( `  \
    Then, the results are combined:% y0 g8 B8 I4 v' @8 m0 N8 w4 }

    ( G" w3 g) Z0 [3 R9 g, }
    & O8 |" b" Y# L& M: Wfactorial(1) = 1 * 1 = 1
    9 j$ A$ t+ @# s& @* t+ {factorial(2) = 2 * 1 = 25 m% f, R, y) N
    factorial(3) = 3 * 2 = 6! h. b+ V# q  Q2 y/ e, V0 j
    factorial(4) = 4 * 6 = 24
      R9 c" M6 r, @/ k+ lfactorial(5) = 5 * 24 = 120# }, z$ k+ C& ~8 _) K0 R

    " C5 M. ^: V- F7 BAdvantages of Recursion1 _. u; k% y9 h6 J5 S! ~% U

    4 i! Y. D# v( [! Z% B, J, v9 c    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).% P% x( C) [1 h$ s- t7 N' y' r
    * R; P9 `2 z- W% w8 p9 v1 V( X
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    9 J& S7 `5 ^6 x4 Y! R" e" B  s
    4 K+ k4 l: T# S5 K' ^4 N0 v9 dDisadvantages of Recursion( L1 @9 A& k6 I; y

    / Q2 W% u8 M, }2 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.
    4 J5 y8 I9 q! n4 Z' h: o% b* d# p5 ]8 H- a) ^4 h* b. e
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    4 s& g" p: z: U0 H, _
    - G4 Q3 h: y7 h  Y7 V) JWhen to Use Recursion
    9 l% g# M3 b1 A, m4 P) Z
    , v/ u* G0 g! O% v    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    & ]  {% c0 a5 M. e5 ~, g# X
    0 B0 R8 M8 X0 ^- a% j( u    Problems with a clear base case and recursive case.
    & }* \2 x; j1 S8 C2 x0 B& {
    ' e+ |1 X7 C) ?Example: Fibonacci Sequence" n  w4 h: h4 Q

    / G$ b: `3 `1 D, z; E+ M$ v' ]The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& Z& `7 \! A% G1 y0 u' C
    : p  b" w2 m; s6 p# @7 B, @- ~
        Base case: fib(0) = 0, fib(1) = 1
    ' G$ Z* u5 r9 g4 c/ p( X1 O" w4 X/ G* t
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
      b! u+ w1 f5 a& V0 h) P, M
    6 Q' |1 w: W, \7 m; vpython
    7 [* u/ A1 ]1 {' L# }& U7 _* W1 W8 B* ]: A

    " y- U( i# t, t) B" Z* @1 ]def fibonacci(n):
    $ m- A+ Y* A4 n- R8 W    # Base cases
    7 K- A3 T1 M, W* R: B8 \    if n == 0:
    * E5 \  y6 f* v3 \* N        return 0
    0 w2 s5 O% _; M- F6 ^    elif n == 1:/ q2 p7 d# Z0 R# B' J: V( U4 _  L9 @
            return 1- D9 Q; P- M6 L3 E' B" w$ B
        # Recursive case2 t, ]0 ]! R% k1 U* H+ I
        else:
    + K* J6 `& C7 ?+ K4 f) P& H        return fibonacci(n - 1) + fibonacci(n - 2)5 ]$ t5 C+ g3 C1 }( Z2 x
    ( N! |+ N4 g1 ^
    # Example usage3 B9 H/ A& t4 Z
    print(fibonacci(6))  # Output: 8
    4 `- u5 h: @+ }- j+ w
    * r7 \) K" h# FTail Recursion2 ?$ {8 ^1 m' u. O3 x" Y% }

    0 P# ~9 S( z- Q; UTail 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).
    7 ^- ~2 @' |3 j/ s3 ~, k0 L2 y( R, A( @; g
    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-1-21 23:27 , Processed in 0.060006 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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