<div class=”output_wrapper” id=”output_wrapper_id” style=”font-size: 15px; color: rgb(62, 62, 62); line-height: 1.8; word-spacing: 2px; letter-spacing: 2px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 有些算法题目,只有把握了思路就能够用很短的代码来实现它。比方上面这几道题目:</p>
<h3 id=”h” style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 二的幂 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-1″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 判断一个数字是否是 2 的 n 次方 </p>
<h4 id=”h-2″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 遇到 2 的幂次方,要建设位移操作的思维,如果 n 是二的幂,即最高位为 1 其余地位为 0,那么 n - 1 就是最高位为 0,其余地位为 1,那么 n &(n-1) 就是 0 </p>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">boolean</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(153, 0, 0); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">isPowerOfTow</span><span class="hljs-params" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> n)</span></span>{
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> (n><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span>)%%(n&(n-<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">1</span>))==<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span>;
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>}
<h3 id=”h-3″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 三的幂 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-4″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 判断一个数字是否为 3 的 n 次方 </p>
<h4 id=”h-5″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”>int 类型中不溢出状况下 3 的最高次方即为 1162261467,那么只有这个数去约 n 为 0,那么阐明 n 的约数只有三,即为 3 的 n 次方.</p>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 同样的做法也能够实用于 5 的幂,7 的幂,X 的幂(X 是一个素数)之类的题目。</p>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">boolean</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(153, 0, 0); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">isPowerOfThree</span><span class="hljs-params" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> n)</span></span>{
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> (n><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span>)&&(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">116261467</span>%n)==<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span>;
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>}
<h3 id=”h1″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 二进制 1 的个数 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-6″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 给定一个数字 n,求 n 转换为二进制示意中 1 的个数 </p>
<h4 id=”h-7″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”>JDK 大法好,间接应用自带的办法:</p>
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>bitCount:返回二进制中 1 的个数 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>lowestOneBit:返回二进制示意中最低位 1 的地位 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>highestOneBit:返回二进制示意中最高位 1 的地位 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>numberOfLeadingZeros:返回二进制示意中高位 0 的数目 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>numberOfTrailingZeros:返回二进制示意中低位 0 的数目 </span></li>
</ul>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> bitCount(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>) {
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> Integer.bitCount(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>);
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> }
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 或者也能够不应用 jdk 类库的解法 </p>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> bitOfOne(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>) {
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span> == <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span> ? <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span> : <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">1</span> + bitOfOne(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span> & (<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span> - <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">1</span>));
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> }
<h3 id=”h-8″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 阶乘后的零 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-9″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 给定一个数字 n,求 n!位数中的 0 的个数。</p>
<h4 id=”h-10″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 这种问题咱们要剖析 0 的产生起因,因为 10=52,对于一个 n!的数字而言,开端为 0 的数字即 n!的约数中蕴含的 5 2 的次数,因为 2 的次数是足够的,也就是说这道题等价于求 n!中约数 5 的个数(留神诸如 25 这种数字,约数中包含了 2 个 5)。</p>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> zeroAtTheEndCount(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>) {
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span> / <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">5</span> + zeroAtTheEndCount(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span> / <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">5</span>);
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> }
<h3 id=”h-11″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 燃烧的灯 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-12″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 有 x 个灯,一个开关管制一个灯,按下开关会使灯燃烧或者关上,当初灯都是燃烧的状态,咱们进行 n 次操作,每次操作都把编号为 i 的倍数的开关全副按下,即 </p>
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 将编号为 1 的倍数的开关全副按下 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 将编号为 2 的倍数的开关全副按下 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>…</span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 将编号为 x 的倍数的开关全副按下 </span></li>
</ul>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 已知灯的总数为 x,求最初灯亮起来状态的数量 </p>
<h4 id=”h-13″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 首先任意取灯的编号 n,会有几次操作会影响这个灯的状态呢?答案是 n 的所有约数的个数次。</p>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 好比说编号为 12 的灯,12 的约数有 1,2,3,4,6,12。也就是说编号为 12 的灯的状态会被切换 6 次,别离在第 1,2,3,4,6,12 次切换开关的时候。</p>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 那么从上面 2 种状况思考:</p>
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 假如 n 是一个非齐全平方的数:因为为一个非平方的数的约数个数为偶数个,也就是说会切换偶数次状态,也就是说状态不会变。</span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 假如 n 是一个齐全平方数:齐全平方数的约数是奇数个,其状态会产生扭转。</span></li>
</ul>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 那么依照题目的要求,一开始全是燃烧状态的,求亮起来状态的灯数目,就是说求会发生变化的编号的数目,也就是相当于求 1~x 之间齐全平方数的个数。</p>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(153, 0, 0); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">lightCount</span><span class="hljs-params" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> num)</span> </span>{
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span>) Math.<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">sqrt</span>(num);
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> }
<h3 id=”h-14″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 奇怪的数 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-15″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 给定一个非空数组,其中除了一个元素只呈现一次外,其余元素都呈现了两次,求这个只呈现一次的元素 </p>
<h4 id=”h-16″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 这里的一个要害运算符是 ^ 异或运算符,次要是利用到了异或运算 a^b 满足的如下法则:</p>
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>a^b=b^a 交换律 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>a^b^c=a^(b^c) 结合律 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>a^a=0, 同时 0^0=0</span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>0^a=a</span></li>
</ul>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span> loseNumber(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">int</span>[] <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>) {
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> Arrays.stream(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 134, 179); word-wrap: inherit !important; word-break: inherit !important;">num</span>).reduce(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">0</span>, (n1, n2) -> n1 ^ n2);
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> }
<h3 id=”h-17″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 约瑟夫环问题 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<h4 id=”h-18″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 问题 </span></h4>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 编号为 1 - n 的 n 个兵士围成一圈,从编号 1 的兵士开始顺次报数,数到 m 的兵士会淘汰,当前的兵士再从新报数,直到剩下一个士兵,求这个士兵的编号 </p>
<h4 id=”h-19″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 解答 </span></h4>
删除前(old) | 删除后(new) |
---|---|
m-2 | n-2 |
m-1 | n-1 |
m | 无 |
m+1 | 1 |
m+2 | 2 |
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”> 假如 old 为删除前的节点编号,new 为删除后的节点编号 </p></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”>- 关系:old=(new+m-1)%n+1</p></li>
</ul>
<blockquote style=”line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(0, 0, 0); border-left: 5px solid rgb(239, 112, 96); background: rgb(239, 235, 233); overflow: auto; overflow-wrap: normal; word-break: normal;”>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;”> 留神:并不是 old=(new+m)%n,因为编号是从 1 开始的而不是从 0 开始的,如果 new+m== n 的话,会导致最初的计算结果 old=0.</p>
</blockquote>
<pre style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(153, 0, 0); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">f</span><span class="hljs-params" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> n,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">int</span> m)</span></span>{
</pre>
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">if</span>(n==<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">1</span>)
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> n;
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-weight: bold; word-wrap: inherit !important; word-break: inherit !important;">return</span> (f(n<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">-1</span>,m)+m<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">-1</span>)%n+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">1</span>;
<span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 128, 128); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span>}
<h3 id=”h-20″ style=”color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;”><span style=”font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;”> 最初 </span><span style=”display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;”> </span></h3>
<ul style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;”>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 如果感觉看完有播种,心愿能关注一下,顺便给我点个赞,这将会是我更新的最大能源,感激各位的反对 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 欢送各位关注我的公众号【java 冢狐】,专一于 java 和计算机基础知识,保障让你看完有所播种,不信你打我 </span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 求一键三连:点赞、转发、在看。</span></li>
<li style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”> 如果看完有不同的意见或者倡议,欢送多多评论一起交换。感激各位的反对以及厚爱。</span></li>
</ul>
<p style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;”>——我是冢狐,和你一样酷爱编程。</p>
<figure style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”><img src=”https://cdn.nlark.com/yuque/0/2020/jpeg/677480/1602294384027-9449b36e-c1e7-4dc4-9251-ba59be6e4157.jpeg” alt=”image” title=”image” style=”font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;”><figcaption style=”line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;”>image</figcaption></figure></div>
欢送关注公众号“Java 冢狐”,获取最新消息