mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-01 01:32:32 +08:00
Documentation for 341ed50da8
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
||||
<meta name="generator" content="Doxygen 1.9.1"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
||||
<meta name="generator" content="Doxygen 1.9.2"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Algorithms_in_C++: dynamic_programming/cut_rod.cpp File Reference</title>
|
||||
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
||||
@@ -17,9 +17,9 @@
|
||||
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
||||
<script type="text/javascript" src="../../search/search.js"></script>
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
||||
jax: ["input/TeX","output/HTML-CSS"],
|
||||
MathJax.Hub.Config({
|
||||
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
||||
jax: ["input/TeX","output/HTML-CSS"],
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
||||
@@ -32,8 +32,7 @@
|
||||
<tbody>
|
||||
<tr style="height: 56px;">
|
||||
<td id="projectalign" style="padding-left: 0.5em;">
|
||||
<div id="projectname">Algorithms_in_C++
|
||||
 <span id="projectnumber">1.0.0</span>
|
||||
<div id="projectname">Algorithms_in_C++<span id="projectnumber"> 1.0.0</span>
|
||||
</div>
|
||||
<div id="projectbrief">Set of algorithms implemented in C++.</div>
|
||||
</td>
|
||||
@@ -42,21 +41,22 @@
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.9.1 -->
|
||||
<!-- Generated by Doxygen 1.9.2 -->
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||||
var searchBox = new SearchBox("searchBox", "../../search",false,'Search','.html');
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
|
||||
/* @license-end */
|
||||
</script>
|
||||
<script type="text/javascript" src="../../menudata.js"></script>
|
||||
<script type="text/javascript" src="../../menu.js"></script>
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
$(function() {
|
||||
initMenu('../../',true,false,'search.php','Search');
|
||||
$(document).ready(function() { init_search(); });
|
||||
});
|
||||
/* @license-end */</script>
|
||||
/* @license-end */
|
||||
</script>
|
||||
<div id="main-nav"></div>
|
||||
</div><!-- top -->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||||
@@ -70,7 +70,7 @@ $(function() {
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
$(document).ready(function(){initNavTree('d6/d10/cut__rod_8cpp.html','../../'); initResizable(); });
|
||||
/* @license-end */
|
||||
</script>
|
||||
@@ -93,8 +93,7 @@ $(document).ready(function(){initNavTree('d6/d10/cut__rod_8cpp.html','../../');
|
||||
<div class="summary">
|
||||
<a href="#namespaces">Namespaces</a> |
|
||||
<a href="#func-members">Functions</a> </div>
|
||||
<div class="headertitle">
|
||||
<div class="title">cut_rod.cpp File Reference</div> </div>
|
||||
<div class="headertitle"><div class="title">cut_rod.cpp File Reference</div></div>
|
||||
</div><!--header-->
|
||||
<div class="contents">
|
||||
|
||||
@@ -111,16 +110,16 @@ Include dependency graph for cut_rod.cpp:</div>
|
||||
</div>
|
||||
</div>
|
||||
</div><table class="memberdecls">
|
||||
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="namespaces"></a>
|
||||
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
|
||||
Namespaces</h2></td></tr>
|
||||
<tr class="memitem:dd/d24/namespacedynamic__programming"><td class="memItemLeft" align="right" valign="top">  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></td></tr>
|
||||
<tr class="memitem:dd/d24/namespacedynamic__programming"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></td></tr>
|
||||
<tr class="memdesc:dd/d24/namespacedynamic__programming"><td class="mdescLeft"> </td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
|
||||
<tr class="separator:"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
<tr class="memitem:d8/d36/namespacecut__rod"><td class="memItemLeft" align="right" valign="top">  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d36/namespacecut__rod.html">cut_rod</a></td></tr>
|
||||
<tr class="memitem:d8/d36/namespacecut__rod"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d36/namespacecut__rod.html">cut_rod</a></td></tr>
|
||||
<tr class="memdesc:d8/d36/namespacecut__rod"><td class="mdescLeft"> </td><td class="mdescRight">Implementation of cutting a rod problem. <br /></td></tr>
|
||||
<tr class="separator:"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
</table><table class="memberdecls">
|
||||
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
||||
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
||||
Functions</h2></td></tr>
|
||||
<tr class="memitem:a1cc523a30c18c63eac58220c3c494cfa"><td class="memTemplParams" colspan="2">template<size_t T> </td></tr>
|
||||
<tr class="memitem:a1cc523a30c18c63eac58220c3c494cfa"><td class="memTemplItemLeft" align="right" valign="top">int </td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>< int, T > &price, const uint64_t &n)</td></tr>
|
||||
@@ -134,16 +133,16 @@ Functions</h2></td></tr>
|
||||
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2"> </td></tr>
|
||||
</table>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p>Implementation of cutting a rod problem. </p>
|
||||
<p>Given a rod of length n inches and an array of prices that contains prices of all pieces of size<=n. Determine the maximum profit obtainable by cutting up the rod and selling the pieces.</p>
|
||||
<div class="textblock"><p >Implementation of cutting a rod problem. </p>
|
||||
<p >Given a rod of length n inches and an array of prices that contains prices of all pieces of size<=n. Determine the maximum profit obtainable by cutting up the rod and selling the pieces.</p>
|
||||
<h3><a class="anchor" id="autotoc_md62"></a>
|
||||
Algorithm</h3>
|
||||
<p>The idea is to break the given rod into every smaller piece as possible and then check profit for each piece, by calculating maximum profit for smaller pieces we will build the solution for larger pieces in bottom-up manner.</p>
|
||||
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Anmol3299">Anmol</a> </dd>
|
||||
<p >The idea is to break the given rod into every smaller piece as possible and then check profit for each piece, by calculating maximum profit for smaller pieces we will build the solution for larger pieces in bottom-up manner.</p>
|
||||
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Anmol3299" target="_blank">Anmol</a> </dd>
|
||||
<dd>
|
||||
<a href="https://github.com/Pardeep009">Pardeep</a> </dd></dl>
|
||||
<a href="https://github.com/Pardeep009" target="_blank">Pardeep</a> </dd></dl>
|
||||
</div><h2 class="groupheader">Function Documentation</h2>
|
||||
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
||||
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
||||
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
||||
|
||||
<div class="memitem">
|
||||
@@ -161,11 +160,11 @@ Algorithm</h3>
|
||||
|
||||
<p>Main function. </p>
|
||||
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
|
||||
<div class="fragment"><div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  {</div>
|
||||
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  <span class="comment">// Testing</span></div>
|
||||
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  <a class="code" href="../../d6/d10/cut__rod_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
|
||||
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  <span class="keywordflow">return</span> 0;</div>
|
||||
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span> }</div>
|
||||
<div class="fragment"><div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> {</div>
|
||||
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> <span class="comment">// Testing</span></div>
|
||||
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> <a class="code hl_function" href="../../d6/d10/cut__rod_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
|
||||
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> <span class="keywordflow">return</span> 0;</div>
|
||||
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span>}</div>
|
||||
<div class="ttc" id="acut__rod_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d6/d10/cut__rod_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Function to test above algorithm.</div><div class="ttdef"><b>Definition:</b> cut_rod.cpp:71</div></div>
|
||||
</div><!-- fragment --><div class="dynheader">
|
||||
Here is the call graph for this function:</div>
|
||||
@@ -176,7 +175,7 @@ Here is the call graph for this function:</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<a id="a1cc523a30c18c63eac58220c3c494cfa"></a>
|
||||
<a id="a1cc523a30c18c63eac58220c3c494cfa" name="a1cc523a30c18c63eac58220c3c494cfa"></a>
|
||||
<h2 class="memtitle"><span class="permalink"><a href="#a1cc523a30c18c63eac58220c3c494cfa">◆ </a></span>maxProfitByCuttingRod()</h2>
|
||||
|
||||
<div class="memitem">
|
||||
@@ -225,26 +224,26 @@ template<size_t T> </div>
|
||||
</table>
|
||||
</dd>
|
||||
</dl>
|
||||
<div class="fragment"><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>  {</div>
|
||||
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>  <span class="keywordtype">int</span> *profit =</div>
|
||||
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>  <span class="keyword">new</span> <span class="keywordtype">int</span>[n + 1]; <span class="comment">// profit[i] will hold maximum profit for i inch rod</span></div>
|
||||
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>  </div>
|
||||
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>  profit[0] = 0; <span class="comment">// if length of rod is zero, then no profit</span></div>
|
||||
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>  </div>
|
||||
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>  <span class="comment">// outer loop will select size of rod, starting from 1 inch to n inch rod.</span></div>
|
||||
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>  <span class="comment">// inner loop will evaluate the maximum profit we can get for i inch rod by</span></div>
|
||||
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>  <span class="comment">// making every possible cut on it and will store it in profit[i].</span></div>
|
||||
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 1; i <= n; i++) {</div>
|
||||
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  <span class="keywordtype">int</span> q = INT_MIN;</div>
|
||||
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 1; j <= i; j++) {</div>
|
||||
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  q = <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(q, price[j - 1] + profit[i - j]);</div>
|
||||
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  }</div>
|
||||
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  profit[i] = q;</div>
|
||||
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  }</div>
|
||||
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <span class="keyword">const</span> int16_t <a class="code" href="../../d7/d35/matrix__exponentiation_8cpp.html#ad8389ed58fd0ec66df248014775ad1fa">ans</a> = profit[n];</div>
|
||||
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="keyword">delete</span>[] profit;</div>
|
||||
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <span class="keywordflow">return</span> <a class="code" href="../../d7/d35/matrix__exponentiation_8cpp.html#ad8389ed58fd0ec66df248014775ad1fa">ans</a>; <span class="comment">// returning maximum profit</span></div>
|
||||
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span> }</div>
|
||||
<div class="fragment"><div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> {</div>
|
||||
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> <span class="keywordtype">int</span> *profit =</div>
|
||||
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> <span class="keyword">new</span> <span class="keywordtype">int</span>[n + 1]; <span class="comment">// profit[i] will hold maximum profit for i inch rod</span></div>
|
||||
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> </div>
|
||||
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> profit[0] = 0; <span class="comment">// if length of rod is zero, then no profit</span></div>
|
||||
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> </div>
|
||||
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> <span class="comment">// outer loop will select size of rod, starting from 1 inch to n inch rod.</span></div>
|
||||
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> <span class="comment">// inner loop will evaluate the maximum profit we can get for i inch rod by</span></div>
|
||||
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="comment">// making every possible cut on it and will store it in profit[i].</span></div>
|
||||
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 1; i <= n; i++) {</div>
|
||||
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="keywordtype">int</span> q = INT_MIN;</div>
|
||||
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 1; j <= i; j++) {</div>
|
||||
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> q = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(q, price[j - 1] + profit[i - j]);</div>
|
||||
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> }</div>
|
||||
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> profit[i] = q;</div>
|
||||
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> }</div>
|
||||
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span> <span class="keyword">const</span> int16_t <a class="code hl_function" href="../../d7/d35/matrix__exponentiation_8cpp.html#ad8389ed58fd0ec66df248014775ad1fa">ans</a> = profit[n];</div>
|
||||
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> <span class="keyword">delete</span>[] profit;</div>
|
||||
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../d7/d35/matrix__exponentiation_8cpp.html#ad8389ed58fd0ec66df248014775ad1fa">ans</a>; <span class="comment">// returning maximum profit</span></div>
|
||||
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span>}</div>
|
||||
<div class="ttc" id="amatrix__exponentiation_8cpp_html_ad8389ed58fd0ec66df248014775ad1fa"><div class="ttname"><a href="../../d7/d35/matrix__exponentiation_8cpp.html#ad8389ed58fd0ec66df248014775ad1fa">ans</a></div><div class="ttdeci">ll ans(ll n)</div><div class="ttdef"><b>Definition:</b> matrix_exponentiation.cpp:91</div></div>
|
||||
<div class="ttc" id="amax_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a></div><div class="ttdeci">T max(T... args)</div></div>
|
||||
</div><!-- fragment --><div class="dynheader">
|
||||
@@ -256,7 +255,7 @@ Here is the call graph for this function:</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<a id="aa8dca7b867074164d5f45b0f3851269d"></a>
|
||||
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
|
||||
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">◆ </a></span>test()</h2>
|
||||
|
||||
<div class="memitem">
|
||||
@@ -281,40 +280,40 @@ Here is the call graph for this function:</div>
|
||||
|
||||
<p>Function to test above algorithm. </p>
|
||||
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
|
||||
<div class="fragment"><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  {</div>
|
||||
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>  <span class="comment">// Test 1</span></div>
|
||||
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>  <span class="keyword">const</span> int16_t n1 = 8; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n1></a> price1 = {1,2,4,6,8,45,21,9}; <span class="comment">// price array</span></div>
|
||||
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <span class="keyword">const</span> int64_t max_profit1 =</div>
|
||||
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  <a class="code" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price1, n1);</div>
|
||||
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  <span class="keyword">const</span> int64_t expected_max_profit1 = 47;</div>
|
||||
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  assert(max_profit1 == expected_max_profit1);</div>
|
||||
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n1 << <span class="stringliteral">" inch road is "</span> << max_profit1</div>
|
||||
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>  << <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  </div>
|
||||
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  <span class="comment">// Test 2</span></div>
|
||||
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  <span class="keyword">const</span> int16_t n2 = 30; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n2></a> price2 = {</div>
|
||||
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  1, 5, 8, 9, 10, 17, 17, 20, 24, 30, <span class="comment">// price array</span></div>
|
||||
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  31, 32, 33, 34, 35, 36, 37, 38, 39, 40,</div>
|
||||
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>  41, 42, 43, 44, 45, 46, 47, 48, 49, 50};</div>
|
||||
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>  </div>
|
||||
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>  <span class="keyword">const</span> int64_t max_profit2=</div>
|
||||
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>  <a class="code" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price2, n2);</div>
|
||||
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>  <span class="keyword">const</span> int32_t expected_max_profit2 = 90;</div>
|
||||
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>  assert(max_profit2 == expected_max_profit2);</div>
|
||||
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n2 << <span class="stringliteral">" inch road is "</span> << max_profit2</div>
|
||||
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  << <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  <span class="comment">// Test 3</span></div>
|
||||
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  <span class="keyword">const</span> int16_t n3 = 5; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n3></a> price3 = {2,9,17,23,45}; <span class="comment">// price array</span></div>
|
||||
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  <span class="keyword">const</span> int64_t max_profit3 =</div>
|
||||
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>  <a class="code" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price3, n3);</div>
|
||||
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  <span class="keyword">const</span> int64_t expected_max_profit3 = 45;</div>
|
||||
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  assert(max_profit3 == expected_max_profit3);</div>
|
||||
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n3 << <span class="stringliteral">" inch road is "</span> << max_profit3</div>
|
||||
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  << <a class="codeRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span> }</div>
|
||||
<div class="fragment"><div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> {</div>
|
||||
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> <span class="comment">// Test 1</span></div>
|
||||
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> <span class="keyword">const</span> int16_t n1 = 8; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n1></a> price1 = {1,2,4,6,8,45,21,9}; <span class="comment">// price array</span></div>
|
||||
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> <span class="keyword">const</span> int64_t max_profit1 =</div>
|
||||
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> <a class="code hl_function" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price1, n1);</div>
|
||||
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> <span class="keyword">const</span> int64_t expected_max_profit1 = 47;</div>
|
||||
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> assert(max_profit1 == expected_max_profit1);</div>
|
||||
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n1 << <span class="stringliteral">" inch road is "</span> << max_profit1</div>
|
||||
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span> << <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> </div>
|
||||
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> <span class="comment">// Test 2</span></div>
|
||||
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span> <span class="keyword">const</span> int16_t n2 = 30; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n2></a> price2 = {</div>
|
||||
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> 1, 5, 8, 9, 10, 17, 17, 20, 24, 30, <span class="comment">// price array</span></div>
|
||||
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,</div>
|
||||
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};</div>
|
||||
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> </div>
|
||||
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <span class="keyword">const</span> int64_t max_profit2=</div>
|
||||
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> <a class="code hl_function" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price2, n2);</div>
|
||||
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> <span class="keyword">const</span> int32_t expected_max_profit2 = 90;</div>
|
||||
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> assert(max_profit2 == expected_max_profit2);</div>
|
||||
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n2 << <span class="stringliteral">" inch road is "</span> << max_profit2</div>
|
||||
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> << <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> <span class="comment">// Test 3</span></div>
|
||||
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> <span class="keyword">const</span> int16_t n3 = 5; <span class="comment">// size of rod</span></div>
|
||||
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int32_t, n3></a> price3 = {2,9,17,23,45}; <span class="comment">// price array</span></div>
|
||||
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span> <span class="keyword">const</span> int64_t max_profit3 =</div>
|
||||
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span> <a class="code hl_function" href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a>(price3, n3);</div>
|
||||
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span> <span class="keyword">const</span> int64_t expected_max_profit3 = 45;</div>
|
||||
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span> assert(max_profit3 == expected_max_profit3);</div>
|
||||
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Maximum profit with "</span> << n3 << <span class="stringliteral">" inch road is "</span> << max_profit3</div>
|
||||
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span> << <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
|
||||
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span>}</div>
|
||||
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></div>
|
||||
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
|
||||
<div class="ttc" id="acut__rod_8cpp_html_a1cc523a30c18c63eac58220c3c494cfa"><div class="ttname"><a href="../../d6/d10/cut__rod_8cpp.html#a1cc523a30c18c63eac58220c3c494cfa">dynamic_programming::cut_rod::maxProfitByCuttingRod</a></div><div class="ttdeci">int maxProfitByCuttingRod(const std::array< int, T > &price, const uint64_t &n)</div><div class="ttdoc">Cuts the rod in different pieces and stores the maximum profit for each piece of the rod.</div><div class="ttdef"><b>Definition:</b> cut_rod.cpp:44</div></div>
|
||||
@@ -334,7 +333,7 @@ Here is the call graph for this function:</div>
|
||||
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||||
<ul>
|
||||
<li class="navelem"><a class="el" href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a class="el" href="../../d6/d10/cut__rod_8cpp.html">cut_rod.cpp</a></li>
|
||||
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
|
||||
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2 </li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user