设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 # J7 N/ s! a8 C7 Y/ i

    ! |$ [6 v$ B6 _  g/ c解释的不错
    : d/ Y+ ?# E4 k, R# x& |6 g' r9 f; \5 m3 Y. n' I4 A# C
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。( _8 X* I/ c5 g& Z/ i+ ~' Z
    2 a. R* F4 p$ E
    关键要素
    9 y& a& H# k- T1. **基线条件(Base Case)**6 V) a7 ~% i( z, J) u5 J! A- ~" X
       - 递归终止的条件,防止无限循环+ N1 h2 p- j' c6 F" M
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' c$ k' w. V$ ]7 T, m: c( t1 C' A
    0 C- V' {9 s2 X# z+ S
    2. **递归条件(Recursive Case)**$ }& t$ g% ?0 L' W  y
       - 将原问题分解为更小的子问题' Q4 ]5 k5 q0 Z. J6 U8 U
       - 例如:n! = n × (n-1)!. F0 A- `+ [7 c' `" r/ d1 E6 ^

    % W2 l+ B* `: M- h: O 经典示例:计算阶乘: o' B; |5 ]3 ~# C. M1 [) c
    python
    2 r2 n9 e. f5 B% h! D7 C' ~! ]) ]def factorial(n):
    / W* `, S1 P# \+ L/ l* E    if n == 0:        # 基线条件
    8 }# _5 k5 _, A: H- Z- N0 G        return 1, l2 J: ?2 S( |: e8 S) A
        else:             # 递归条件" y( @3 Y* \0 x& q7 a* U& ^
            return n * factorial(n-1)
    ! q# x* ~: |; E2 P; R( b3 V执行过程(以计算 3! 为例):
    4 }% W6 G0 }6 v) p  t8 xfactorial(3)4 y2 ]: R2 J5 s6 L
    3 * factorial(2)
    9 K  V4 b1 E% U" t$ C3 * (2 * factorial(1))! z0 u7 a; U. i; H
    3 * (2 * (1 * factorial(0)))
    ! j+ x, E* G! y' j* A9 n/ f. j( p4 [3 * (2 * (1 * 1)) = 6- b  H1 g, K5 C1 X) o

    ! o' j9 N" g7 Y3 n5 e* Z4 D: v 递归思维要点
    & d  ^" @- [$ L* {1. **信任递归**:假设子问题已经解决,专注当前层逻辑+ O- o$ Q4 c' w3 C( u& l- d" {
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    4 y. A) B  j% J& ^1 O: N) O$ r3. **递推过程**:不断向下分解问题(递)
    # l( b- x$ H5 R3 |! H% ~1 K4. **回溯过程**:组合子问题结果返回(归)+ n4 \  F: k* n$ D+ |4 z$ D

    # N2 x3 `: U7 g0 C% r6 Q 注意事项
    % f  h& ?+ _4 w9 u  C; a! W必须要有终止条件+ g( e* e6 {- k9 ^- g/ s: w% I* M
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)( b: v. K5 o* x0 G- E
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ' a, ~, _9 q+ {4 m$ c3 G  @# }尾递归优化可以提升效率(但Python不支持)! {1 S  `% ^0 A/ l/ r* i0 v
    $ K/ `( d, M0 V# [! {; G  o
    递归 vs 迭代
      y1 y4 F# V: V|          | 递归                          | 迭代               |
    - H* O$ a& j3 `|----------|-----------------------------|------------------|) k( k, r& P5 i0 @# V
    | 实现方式    | 函数自调用                        | 循环结构            |/ O+ m8 S: C  w2 E- a( w8 h
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 d- J0 |  }7 E; k! R& V| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ M: @  P& `& ^
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. B+ a2 l# s  [: g* q8 T2 d

    ) I& F" \1 P4 W! O 经典递归应用场景/ K/ z1 z' [0 u# {8 U
    1. 文件系统遍历(目录树结构)
    ) A4 d: o" |# p3 Q2. 快速排序/归并排序算法, b) s% Z& C* n! [
    3. 汉诺塔问题
    5 R8 r6 `1 [( Q9 V# q4. 二叉树遍历(前序/中序/后序)
    2 ~3 p% M6 X+ Y8 V9 M/ l' c5. 生成所有可能的组合(回溯算法)
    ) p. C. ]4 N' ]0 b9 \7 J: M
    8 z( T0 v- A, ^- d& t! C) w1 S9 h试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    9 小时前
  • 签到天数: 3144 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ ?# i# K6 |" A: s7 B% H, N0 Z
    我推理机的核心算法应该是二叉树遍历的变种。8 C( o1 h4 d/ B! l9 V# ]+ f& @
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . g- b" P& a2 Q3 h' TKey Idea of Recursion
    ; N9 m* v' }: j$ }8 P2 H/ k+ ^6 ?7 c0 y
    A recursive function solves a problem by:4 H) F/ G; ]! ~1 p& T; Y  f) Q4 F0 R

    . I  J# `$ ~, {. e' f# e    Breaking the problem into smaller instances of the same problem./ v; g2 q; c6 i& J; O1 T: t& u
    ) S0 g  |$ m4 ]/ ~+ j
        Solving the smallest instance directly (base case).
    # W0 f7 Y  K7 ^' {7 O. Y
    8 ^3 x6 \7 r" `9 c1 d    Combining the results of smaller instances to solve the larger problem.
    % D+ a: R# x1 Q; g, k* L
    4 Q( g1 G0 Z9 ?& N6 |$ v* JComponents of a Recursive Function
    1 B' U1 n* ]+ g, @5 H. S& ^( G( E3 r
        Base Case:
    " Q# P& R! l2 v! A8 ~/ ~. v9 P2 q9 C* H) K% m
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( C- L0 \8 R. V# c: ^% ~- i3 C  F3 [
            It acts as the stopping condition to prevent infinite recursion.8 f9 b4 ^" f9 q+ |& M

    " C  a! I$ k7 y; L6 h        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.- W. E0 P. j4 m" D) V
    " _; f& I8 m% y4 A
        Recursive Case:
    1 w# W7 J# q' b3 j8 h4 A. Z1 j0 B' Z5 p& G6 W7 T+ C- q2 p: ^
            This is where the function calls itself with a smaller or simpler version of the problem.9 \# L% s3 K9 ]6 T

    + h$ @- K: C/ b' f$ o" Z# C        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).; M: H$ \7 w! ]4 E5 w) M
    / S7 e, A9 d9 F
    Example: Factorial Calculation
    / r; }/ C8 \! [9 `' P5 B2 j  R2 Y
    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:
    7 B2 B# U+ R. d4 O5 B' Q, n- S8 ^1 @) R, K, j& h
        Base case: 0! = 1
    . n! O$ z: e4 N- u( g3 \& k( E! e$ Y
    ( i5 B" p- M/ n4 C; T    Recursive case: n! = n * (n-1)!
    ' R( i/ \3 L" `$ i& H1 a. u, H$ |! W9 J  S% t1 G! k- V& {  g
    Here’s how it looks in code (Python):
    # M& h& I. i/ Y, c$ U( S$ o* a, _python- t1 \$ [, \. t5 F% z0 I
    2 R. ^$ e' W" n' D1 X
    " m" O4 O1 w5 N  J
    def factorial(n):
    : l, Y( @" p; J/ {" m    # Base case
    7 \) i. h, m/ {- h    if n == 0:
    1 g5 U' t! d0 h( e7 S% @        return 1
    2 F% f, f& A8 S5 u# H    # Recursive case
    0 E" q1 K* q4 x: \    else:" G6 ?7 P+ P5 a' U# \# `; |
            return n * factorial(n - 1)( M  g  J- K  X& p
    , f" j9 X$ \0 u3 L; l% Z/ a. \
    # Example usage1 _' i% o& p4 y
    print(factorial(5))  # Output: 120& v* A0 h" w3 x- q2 Q
    4 M9 E/ [+ i; \- v+ y2 y2 R4 E
    How Recursion Works
    " d1 g5 ]; K5 r' ^2 ~& Y
    3 I. z- j% S: {$ b. |) q5 x. l    The function keeps calling itself with smaller inputs until it reaches the base case.
    0 K- K) f8 V6 v, N
    : v6 s! p$ c) ^8 [    Once the base case is reached, the function starts returning values back up the call stack.0 Y) [4 n5 J& E8 ]
    ' |% p8 H/ K5 k8 ?. |
        These returned values are combined to produce the final result.7 l  a: X2 M# b2 s4 L2 Y6 L
    9 {' a% H3 z" F, ?7 e2 N
    For factorial(5):# V+ G* u. o8 e

    7 m( K+ C; |) l. J  e* O1 t5 b
    , ?- b1 u7 K1 a1 M* v8 F3 qfactorial(5) = 5 * factorial(4)3 w6 ~1 P9 R! m; L- ?% N( _& {
    factorial(4) = 4 * factorial(3)! Y6 l1 @/ T0 ?: H
    factorial(3) = 3 * factorial(2)
    3 [0 J, h9 |7 o4 W9 qfactorial(2) = 2 * factorial(1)
    1 B9 s: v& l0 p; u7 h5 yfactorial(1) = 1 * factorial(0)! q! V- n  `$ l* H
    factorial(0) = 1  # Base case
    4 w( H9 K( ~  Y0 P' B
    8 E1 T- D0 t( q3 zThen, the results are combined:
    ; K. ?9 ~, O0 K# x& R8 U" r  d9 b$ F/ Y
    3 x  z+ \$ Q+ q# Y
    factorial(1) = 1 * 1 = 17 r9 {! h% q% `8 @0 `) v
    factorial(2) = 2 * 1 = 2
    . \4 d3 H7 R0 a' x7 Y4 ~factorial(3) = 3 * 2 = 6
    + l' q2 N; Y9 N( q/ E* U" lfactorial(4) = 4 * 6 = 24
    " h, F) t$ K3 ?* U% q! Q* ffactorial(5) = 5 * 24 = 120
    5 N& R' @4 a, ~9 ^) ?. `) D
    - A* ]" V+ f# bAdvantages of Recursion
    $ T' y$ ?+ p, t3 u! P9 b
    5 K9 N" z5 `; X    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).
    5 E8 F% O; O. W' ~& P% j, k8 W; W& V0 Y
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    4 k0 t: O: j5 |+ ~' x
    # _& }+ ]! c5 \1 w' Z: A+ u4 kDisadvantages of Recursion  ]& @! O3 }2 D& b" n
    4 g/ k- L1 I7 a8 z6 `. O* [
        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 F2 b3 _- C1 W4 s! {. \3 d1 ~: o
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & p. r8 X$ V5 R) J# `
    + f" T& B  J, t6 P1 gWhen to Use Recursion
    $ N5 [0 [0 K+ O, m) J
    . p, K+ j& a6 Z& t% f' d    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ) u# d7 N& O1 Q8 j8 R
    7 C. i9 u, J& L4 p  C+ T( m. h    Problems with a clear base case and recursive case.( e1 t  d- f/ x/ C# \  H2 w

    * G9 n. E1 F# g7 ~% m7 [Example: Fibonacci Sequence
    % @- L: m  M2 A0 ]8 o3 C! A5 w! d( A7 a2 Z0 O
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:8 G+ R, n  n% y! y7 P

    ) b! ~8 G) e7 G" {) t. S% u. t* p    Base case: fib(0) = 0, fib(1) = 1
    3 L8 E+ q( K. h4 ?8 B$ O0 D9 [" y" w% l! l! i
        Recursive case: fib(n) = fib(n-1) + fib(n-2)/ q9 z  L3 ~* w1 q
    $ I. L- ~7 W3 i  ?; Y
    python. c  k: J; q9 ]+ X' I
    - Z1 L8 ^" D' H0 j- S. X2 E8 u

    * c: a9 H; E. c$ n) U( R. e% B2 b; Sdef fibonacci(n):0 l; b: b, M- m8 u
        # Base cases+ D' b* U- y; l: W( |# a- L
        if n == 0:: E3 F/ C! e" y" }/ w
            return 0
    # `4 c2 U7 }% F    elif n == 1:
    : r1 B3 i5 @7 N: C, {* a* e3 i        return 1
    # r5 S; O* F8 {0 Y  D" S    # Recursive case
    * ], ^  x6 c: G& w; ?* Q    else:
    " x5 ^. Z( q9 N  |, i: N' s# s$ c        return fibonacci(n - 1) + fibonacci(n - 2)
    ; ]- a! R$ N) W* ~2 u
    + G6 R' U9 l. ^# Example usage
    + q! y1 @8 Y6 B: E5 g+ Cprint(fibonacci(6))  # Output: 8" l) A% q1 a7 P* g3 a* f

    . A' P$ K& w) l4 s" d# vTail Recursion
    7 X( m; X& F$ l8 e
    % }* K9 `7 \4 d' ]1 MTail 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).
    3 a/ p3 R/ D' z1 x. C! P+ I" V* z* J' E% t& l
    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-12 16:59 , Processed in 0.029270 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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