设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 w: n' o4 O2 `( _2 U8 k3 [

    7 N1 m0 f0 e3 `8 p  l解释的不错
      V; w9 p& x! `/ |
    : \' @3 u6 h+ B7 x: J% U递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。4 g% B3 v5 E- }* i" q) q) d
    5 S, S9 i3 W- E; ^& P' Y
    关键要素+ a& U! v  n: \* K) z# K
    1. **基线条件(Base Case)**
    2 Z3 e# F9 L; |& I1 ~) ^   - 递归终止的条件,防止无限循环
      Q) V3 L1 B4 h( _2 F   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1& N$ y) v0 \; s* I' F

    7 J# x0 m. O2 n1 s2. **递归条件(Recursive Case)**2 F7 H; F0 x2 w1 @* g% n- F( z
       - 将原问题分解为更小的子问题) {! F# o6 j2 ]5 D
       - 例如:n! = n × (n-1)!2 D: S# A7 M/ d! B; I
    0 |: t. h. Q1 K; r
    经典示例:计算阶乘! W$ v, |3 _1 |" ~; M, c
    python
    * T4 T9 g' r. E) D# j, {- y/ _def factorial(n):; F) P' z2 p' h+ J2 Q# D
        if n == 0:        # 基线条件
    & {9 v$ `$ @" D2 M9 f5 Z        return 1
    ; p3 \; V& t( J; R4 g! R6 ]+ g    else:             # 递归条件
    7 G6 D8 A  v& o( D% Z  _        return n * factorial(n-1)+ \. x' P9 N2 E' P" y, i% y
    执行过程(以计算 3! 为例):! r& `; g8 w# p+ q5 F
    factorial(3)) \5 l0 g) Q1 S
    3 * factorial(2)
    4 m5 J8 l5 M& w3 * (2 * factorial(1))
      S6 o) X/ R8 k4 n) W) D3 l; F  P6 S3 * (2 * (1 * factorial(0)))
    : {8 {4 P4 M5 e) S7 S8 h& ^3 * (2 * (1 * 1)) = 65 V+ w6 q% o. W9 ~- S7 x8 H* [
    ; X$ Q, _" Y% ?" h2 x
    递归思维要点- T8 g; N9 @( u
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑0 y5 {5 o3 t' J- x$ n& R+ x
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)/ @, V0 G; ^0 A$ m) U3 }, `/ A
    3. **递推过程**:不断向下分解问题(递)
    ; i% C+ X$ G; J7 t4. **回溯过程**:组合子问题结果返回(归)+ z/ b: x2 w) U5 O$ J- s* U

    & ?; a1 x6 Z; }5 G+ m- J 注意事项* J7 o# V2 G. Z4 l
    必须要有终止条件3 D4 g$ N9 E( D: h( D
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)3 L) d2 ]7 F. k8 w. h; \6 u
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ) i# e1 H: r2 G( D: B) p4 b尾递归优化可以提升效率(但Python不支持)# U& X2 [6 U) ^% `: W
    ! ~, P* ?8 H9 n$ J# r. Z- f* [4 w% [
    递归 vs 迭代+ b- A% F7 y  F8 \; G5 r1 V
    |          | 递归                          | 迭代               |
    - S  R" R! V. k|----------|-----------------------------|------------------|- W" t8 X/ U5 Q# d( e& [: z
    | 实现方式    | 函数自调用                        | 循环结构            |
    % {! J  Y: X2 a# ^. g9 o| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    & C: D; O4 g# T$ n| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    8 `! i  u  n, \' H| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |" |2 D2 m9 e/ d& y  ?8 @

    ! E7 |, p- H6 t% P 经典递归应用场景
    * \& @$ h$ C6 J0 }5 C7 p1. 文件系统遍历(目录树结构); {: l# G( V4 k
    2. 快速排序/归并排序算法
    ( i3 C$ Y: o/ \- y6 g3. 汉诺塔问题
    6 f4 a9 G' I9 a. S" p4. 二叉树遍历(前序/中序/后序)5 J$ {9 X+ f/ }! j" M" T
    5. 生成所有可能的组合(回溯算法)/ k- B0 D* Z! ~5 T5 X1 Z

    + O' ^5 C% Q! O3 P$ M试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    16 小时前
  • 签到天数: 3110 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    0 p$ H" g( [5 S& J我推理机的核心算法应该是二叉树遍历的变种。
      e. b) M) o7 `" {0 l3 L( l" q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ) k; L+ k/ \* d# p2 wKey Idea of Recursion! D$ o" t7 T/ T

    ; o. K% s( d( B- FA recursive function solves a problem by:
    2 S# |1 ?/ X' Z. z- z
    ( ~- m. |' `+ H$ N: P6 d    Breaking the problem into smaller instances of the same problem.& W/ s- ]& l  h8 R5 E$ J% Z
    6 x: n; Z: H  A
        Solving the smallest instance directly (base case).
    1 A) s3 d. b+ U
    - C0 @* O4 X2 c6 Y  `- M    Combining the results of smaller instances to solve the larger problem.6 o% Y( h6 ~4 T' g3 L3 ]( A) C

    . d; t& [; [! X2 l$ OComponents of a Recursive Function2 t" G6 o1 `) ?5 g% P# v3 N$ ~
    ! k$ J& y% b( V$ r6 r  w
        Base Case:) P* t' U# F+ Y5 g/ T, J( a
    + O4 O6 e5 b' u) c: |; i
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.8 s# P: o/ D0 U1 Z

    ) _3 J. |  a5 R4 m$ ^        It acts as the stopping condition to prevent infinite recursion.
    & W% v/ ~# `& [8 j3 i# c- U7 C! @% N/ [
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 j* q+ m& s6 q% ]
    2 r3 `! i8 a, K    Recursive Case:5 f- Q" x' |' u* Q7 w- ?+ f# H0 u! ]

    8 h2 j  K5 P( V$ |: [4 g5 n0 Q        This is where the function calls itself with a smaller or simpler version of the problem.
      t3 }/ A( T3 |# h
    + h. ^% ?( R  U4 ?, Y; |2 R% v3 d  F        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    5 I, ~0 U9 t+ V3 E5 h: [$ c. B/ B# E' F4 q2 J" F% j' ?8 I
    Example: Factorial Calculation
      a% ?& ~2 Q, }* p3 ~  v
    7 ?; I& m( ]* K- _5 d3 wThe 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:6 U, X  \1 [3 H
    8 P$ s2 t. X2 v$ j. B
        Base case: 0! = 10 b$ l% v5 K! ~! Y5 f; R
    + [6 [( k( v# d' ?
        Recursive case: n! = n * (n-1)!8 n5 |7 k! m1 l- s* `
    % k* @& _3 c+ f% y
    Here’s how it looks in code (Python):6 Q6 e3 f& O# O3 a$ y
    python
    # e7 q" z1 I  i* s- Y$ E. t' d' P: w1 j
    4 W2 }5 n& \, y' f$ v- p. o
    def factorial(n):3 G5 ^2 w4 b) E2 P
        # Base case* [+ T2 ?/ j- S8 l. ^3 R' g
        if n == 0:
      f3 j; I. `6 [+ C        return 18 R1 G3 ~2 o6 g- _8 u
        # Recursive case
    6 `! L2 j, ^9 b    else:- E, T; M! {, E9 Y7 k
            return n * factorial(n - 1)" d% o& S7 R/ K) |; B

    " S& }+ @! R6 f1 A. {/ I# Example usage
    4 L( u. u# C$ ?4 }5 Xprint(factorial(5))  # Output: 120& F/ t$ O" h7 S4 r! Q, ^' b8 w8 _

    ( @" @8 e/ o  T' H+ j$ tHow Recursion Works
    ' n* s( ?6 l! M, N& L: V/ x) q/ |: }: {2 d* e/ Q* F: f
        The function keeps calling itself with smaller inputs until it reaches the base case.- T$ ~% U: C7 O+ l+ w* j3 v) q

    % q! e; I7 ~. ]3 m+ q# h" u3 K& g    Once the base case is reached, the function starts returning values back up the call stack.6 z- v/ [( u2 ~- s
    % O5 T8 y: |6 \4 h, }
        These returned values are combined to produce the final result.3 e% S1 P6 y7 b

    , W( P! x. Z  n7 e2 IFor factorial(5):
    % Z. }' j9 l+ {: X  a% M9 N9 H% i
    ; ?% G$ f$ f1 {  m
      X% I6 J3 ~# a0 c; O. F; W0 Hfactorial(5) = 5 * factorial(4)1 H# U: ]$ G, j: a& N+ a* t
    factorial(4) = 4 * factorial(3)1 }4 y) r4 e  Q1 N
    factorial(3) = 3 * factorial(2)
    8 ~# u0 n- U/ Sfactorial(2) = 2 * factorial(1)2 ~' ]8 J: E( D$ y( s) ~$ s6 }2 Y
    factorial(1) = 1 * factorial(0)
    ( k" n5 ^& R8 i. X- S: @factorial(0) = 1  # Base case
    " m( z4 ?" s! }3 Z
    $ L8 Y  Y& K0 uThen, the results are combined:
    ( Y1 n: c5 t; x4 K! @6 u6 o5 H) g% X8 j) \5 y* H* ]/ B5 k  Y

    ' l! J9 I; x% W% Z' Nfactorial(1) = 1 * 1 = 1% j# b& G7 Y( J' V4 B( @0 x+ v
    factorial(2) = 2 * 1 = 2+ l& k1 y5 g# f+ b9 n5 _% p4 b
    factorial(3) = 3 * 2 = 69 h6 G  X. A  F
    factorial(4) = 4 * 6 = 24
    + R8 S% ~) I$ T8 k) Ofactorial(5) = 5 * 24 = 120
    3 y- _+ r0 e1 o, i0 s. l3 t$ L* V- ]9 G  R+ }% `
    Advantages of Recursion: s2 w: p1 R/ d# r# U5 ]: X+ k) Y

    ' u( G+ T/ O1 C9 `4 N# |1 U; \    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)., n$ L0 `$ z% P! _
    ' z' x- Z8 D) E
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ) |3 L2 f0 F3 z1 t! K% w2 o7 C* v" Q) ]. A1 D9 C
    Disadvantages of Recursion
    / q9 n! Y& ?! g( }# |' _% h0 n: J( C
        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.
    : K- Z% q  f: a, ]0 l# l! `
    & G! b7 T9 X, S3 @* J$ V    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 ~3 |: ~. x' m
    * r0 U* w6 E4 Q2 p6 P5 g6 k4 A
    When to Use Recursion
    7 j7 o- b3 J% j. x" z. `( I
    - {3 H  M# ?' |- Z% v! e2 K( |    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    $ v* {9 y7 F; C, c; ^7 d( [) C% e% {, }, t( [
        Problems with a clear base case and recursive case.
    3 w9 b, `0 v$ Z9 }* u1 ~  c# R1 m
    + Y5 J8 {0 q3 X. l$ rExample: Fibonacci Sequence
    % P, c4 x2 f6 ]4 e  h  U  z: N2 A9 e3 \
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    6 ^1 M8 b* F( w) M
    . i! t  m8 c2 W; Y8 R    Base case: fib(0) = 0, fib(1) = 1
    ' |; C8 I2 |7 l% ?' E
    3 ?' U* O, Q' l) O+ _    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    1 c& \! ]: D' I; f" g+ U- {* V1 y, Y( ?- k6 [5 P# m1 b
    python) h/ n" D3 y. N8 Y1 U# ~0 ^& v
    - T3 {2 X# @. u2 R

    7 `2 J$ x- `* J5 V, w* ~def fibonacci(n):5 N4 S8 o, I" |4 q5 H* w7 C
        # Base cases8 [6 U( E) X) X4 p/ b2 C
        if n == 0:3 ~* W; d2 k8 H
            return 0
    3 t. P4 _9 i$ A5 _  [. O    elif n == 1:! A( y0 ?6 ~$ `6 l5 Q1 k, {/ n
            return 17 d: Y& e  M1 |' X
        # Recursive case' z; c1 }0 R- i$ s( N" B0 X
        else:
      w6 r- a3 {. \: }+ a/ d        return fibonacci(n - 1) + fibonacci(n - 2)
    2 J; B6 ^1 P/ d( P! t9 _4 [: R$ F. i& T0 c8 j8 w2 ?$ D
    # Example usage
    * S+ z1 {/ {( ^, ^" {* xprint(fibonacci(6))  # Output: 8: _, _2 N3 o7 P

      N$ J- h6 O3 X" P+ ]4 K, JTail Recursion  u4 Q# S) I$ D7 |2 S( M$ }, [

    # a- R+ [- D8 X/ Z$ |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).
    & ~! d  ~: ^: H2 _4 V3 C7 ~
    7 O$ s1 K' U4 A& IIn 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-6 22:36 , Processed in 0.058242 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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