题意:给定一个电阻值,求得到该电阻值所需单位电阻的最小个数。
思路:逆推,大于电阻值1时减至分数,小于1时取倒数,重复操作。
<pre class="sh-cpp sh-sourceCode" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 5px 5px 5px 7px; overflow: auto; font-size: 16.3636360168457px; line-height: 17.6000003814697px; font-family: 'Courier New', Courier, monospace;"><span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><cstdio></span> <span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><algorithm></span> <span class="sh-preproc" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 184, 0);">#include</span><span class="sh-string" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><iostream></span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">using</span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">namespace</span> std<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">long</span> <span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">long</span> n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>num<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> i<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>j<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>k<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>rcount<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>shu<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>flag<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">,</span>yu<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-type" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255);">int</span> <span class="sh-function" style="margin: 0px; padding: 0px; line-height: 1.1em; font-weight: bold;">main</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">()</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">while</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>cin<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">>></span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span> cin<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">>></span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> num<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span><span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">while</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">(</span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">)</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">{</span> num<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">+=</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">/</span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span>n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">%</span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> n<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span>m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> m<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">=</span>t<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span> cout<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><<</span>num<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);"><<</span>endl<span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span> <span class="sh-keyword" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(0, 0, 255); font-weight: bold;">return</span> <span class="sh-number" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(169, 0, 169);">0</span><span class="sh-symbol" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">;</span> <span class="sh-cbracket" style="margin: 0px; padding: 0px; line-height: 1.1em; color: rgb(255, 0, 0);">}</span>
原文地址:http://blog.csdn.net/dominating413421391/article/details/43536433