deploy
After Width: | Height: | Size: 49 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 54 KiB |
|
@ -2110,7 +2110,7 @@
|
|||
<h1 id="11">1.1. 算法无处不在<a class="headerlink" href="#11" title="Permanent link">¶</a></h1>
|
||||
<p>当我们听到“算法”这个词时,很自然地会想到数学。然而实际上,许多算法并不涉及复杂数学,而是更多地依赖于基本逻辑,这些逻辑在我们的日常生活中处处可见。</p>
|
||||
<p>在正式探讨算法之前,有一个有趣的事实值得分享:<strong>实际上,你已经学会了许多算法,并习惯将他们应用到日常生活中了</strong>。下面,我将举两个具体例子来证实这一点。</p>
|
||||
<p><strong>例一:组装积木</strong>。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。</p>
|
||||
<p><strong>例一:拼装积木</strong>。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。</p>
|
||||
<p>从数据结构与算法的角度来看,积木的各种形状和连接方式代表数据结构,而组装说明书上的一系列步骤则是算法。</p>
|
||||
<p><img alt="拼装积木" src="../algorithms_are_everywhere.assets/assembling_blocks.jpg" /></p>
|
||||
<p align="center"> Fig. 拼装积木 </p>
|
||||
|
@ -2124,19 +2124,19 @@
|
|||
<div class="tabbed-set tabbed-alternate" data-tabs="1:5"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1"><1></label><label for="__tabbed_1_2"><2></label><label for="__tabbed_1_3"><3></label><label for="__tabbed_1_4"><4></label><label for="__tabbed_1_5"><5></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="查字典步骤" src="../algorithms_are_everywhere.assets/look_up_dictionary_step_1.png" /></p>
|
||||
<p><img alt="查字典步骤" src="../algorithms_are_everywhere.assets/binary_search_dictionary_step_1.png" /></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="look_up_dictionary_step_2" src="../algorithms_are_everywhere.assets/look_up_dictionary_step_2.png" /></p>
|
||||
<p><img alt="binary_search_dictionary_step_2" src="../algorithms_are_everywhere.assets/binary_search_dictionary_step_2.png" /></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="look_up_dictionary_step_3" src="../algorithms_are_everywhere.assets/look_up_dictionary_step_3.png" /></p>
|
||||
<p><img alt="binary_search_dictionary_step_3" src="../algorithms_are_everywhere.assets/binary_search_dictionary_step_3.png" /></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="look_up_dictionary_step_4" src="../algorithms_are_everywhere.assets/look_up_dictionary_step_4.png" /></p>
|
||||
<p><img alt="binary_search_dictionary_step_4" src="../algorithms_are_everywhere.assets/binary_search_dictionary_step_4.png" /></p>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<p><img alt="look_up_dictionary_step_5" src="../algorithms_are_everywhere.assets/look_up_dictionary_step_5.png" /></p>
|
||||
<p><img alt="binary_search_dictionary_step_5" src="../algorithms_are_everywhere.assets/binary_search_dictionary_step_5.png" /></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -2146,9 +2146,13 @@
|
|||
<li>可选项是比 <span class="arithmatex">\(31\)</span> 元面值更小的货币,包括 <span class="arithmatex">\(1\)</span> , <span class="arithmatex">\(5\)</span> , <span class="arithmatex">\(10\)</span> , <span class="arithmatex">\(20\)</span> 元。</li>
|
||||
<li>从可选项中拿出最大的 <span class="arithmatex">\(20\)</span> 元,剩余 <span class="arithmatex">\(31 - 20 = 11\)</span> 元。</li>
|
||||
<li>从剩余可选项中拿出最大的 <span class="arithmatex">\(10\)</span> 元,剩余 <span class="arithmatex">\(11 - 10 = 1\)</span> 元。</li>
|
||||
<li>从剩余可选项中拿出最大的 <span class="arithmatex">\(1\)</span> 元,剩余 <span class="arithmatex">\(1 - 1 = 0\)</span> 元。完成找零。</li>
|
||||
<li>从剩余可选项中拿出最大的 <span class="arithmatex">\(1\)</span> 元,剩余 <span class="arithmatex">\(1 - 1 = 0\)</span> 元。</li>
|
||||
<li>完成找零,方案为 <span class="arithmatex">\(20 + 10 + 1 = 31\)</span> 元。</li>
|
||||
</ol>
|
||||
<p>在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案 <span class="arithmatex">\(20 + 10 + 1 = 31\)</span> 元。从数据结构与算法的角度看,以上方法本质上是「贪心算法」。</p>
|
||||
<p>在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是「贪心算法」。</p>
|
||||
<p><img alt="货币找零" src="../algorithms_are_everywhere.assets/greedy_change.png" /></p>
|
||||
<p align="center"> Fig. 货币找零 </p>
|
||||
|
||||
<p>小到烹饪一道菜,大到星际航行,几乎所有问题的解决都离不开算法。计算机的出现使我们能够通过编程将数据结构存储在内存中,同时编写代码调用 CPU 和 GPU 执行算法。这样一来,我们就能把生活中的问题转移到计算机上,以更高效的方式解决各种复杂问题。</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
|
|
|
@ -130,6 +130,11 @@
|
|||
<lastmod>2023-06-30</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dynamic_programming_solution_framework/</loc>
|
||||
<lastmod>2023-06-30</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
||||
<lastmod>2023-06-30</lastmod>
|
||||
|
|