💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 基础算法 ### 九九乘法表 ``` <pre class="calibre14">``` <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$i<span class="token"><</span><span class="token3">10</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">for</span><span class="token2">(</span>$j<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$j<span class="token"><=</span>$i<span class="token2">;</span>$j<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> echo $i<span class="token2">.</span><span class="token4">'*'</span><span class="token2">.</span>$j<span class="token2">.</span><span class="token4">'='</span><span class="token2">.</span>$i<span class="token">*</span>$j<span class="token2">.</span><span class="token4">"&nbsp;&nbsp;"</span><span class="token2">;</span> <span class="token2">}</span> echo <span class="token4">"<br>"</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` #### 一百钱买一百只鸡,公鸡5文一只,母鸡3文一只,小鸡1文三只;问有公鸡母鸡小鸡各多少只? ``` <pre class="calibre14">``` <span class="token5">for</span><span class="token2">(</span>$a<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$a<span class="token"><=</span><span class="token3">18</span><span class="token2">;</span>$a<span class="token">++</span><span class="token2">)</span><span class="token2">{</span><span class="token6">//18是因为全买公鸡</span> <span class="token5">for</span><span class="token2">(</span>$b<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$b<span class="token"><=</span><span class="token3">31</span><span class="token2">;</span>$b<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">for</span><span class="token2">(</span>$c<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$c<span class="token"><=</span><span class="token3">100</span><span class="token2">;</span>$c<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token2">(</span>$a<span class="token">+</span>$b<span class="token">+</span>$c<span class="token">==</span><span class="token3">100</span><span class="token2">)</span><span class="token">&&</span><span class="token2">(</span><span class="token3">5</span><span class="token">*</span>$a<span class="token">+</span><span class="token3">3</span><span class="token">*</span>$b<span class="token">+</span>$c<span class="token">/</span><span class="token3">3</span><span class="token">==</span><span class="token3">100</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> echo <span class="token4">"公鸡{$a}只,母鸡{$b}只,小鸡{$c}只<br>"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` ### 求和 ``` <pre class="calibre14">``` $s<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$i<span class="token"><</span><span class="token3">101</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $s<span class="token">=</span>$s<span class="token">+</span>$i<span class="token2">;</span> <span class="token2">}</span> echo <span class="token4">"1+2+3+...+100=$s"</span><span class="token2">;</span> ``` ``` ### 求阶乘 ``` <pre class="calibre14">``` $s<span class="token">=</span><span class="token3">1</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$i<span class="token"><=</span><span class="token3">10</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $s<span class="token">=</span>$s<span class="token">*</span>$i<span class="token2">;</span> <span class="token2">}</span> echo $s<span class="token2">;</span> ``` ``` ### 递归 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">jie</span><span class="token2">(</span>$n<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span>$n<span class="token">==</span><span class="token3">1</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token3">1</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token5">return</span> <span class="token1">jie</span><span class="token2">(</span>$n<span class="token">-</span><span class="token3">1</span><span class="token2">)</span><span class="token">*</span>$n<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> echo <span class="token1">jie</span><span class="token2">(</span><span class="token3">10</span><span class="token2">)</span><span class="token2">;</span> ``` ``` ### 输出控制算法 ``` <pre class="calibre16">``` $arr <span class="token">=</span> <span class="token2">[</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span> <span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">2</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">3</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">4</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">5</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">6</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">7</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">8</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">9</span><span class="token2">,</span><span class="token4">"title"</span><span class="token">=</span><span class="token">></span><span class="token4">"测试标题1"</span><span class="token2">]</span> <span class="token2">]</span><span class="token2">;</span> <span class="token6">/** * 每三列都不同的内容输出, */</span> $cl <span class="token">=</span><span class="token2">[</span><span class="token4">"a"</span><span class="token2">,</span><span class="token4">"b"</span><span class="token2">,</span><span class="token4">"c"</span><span class="token2">]</span><span class="token2">;</span> $i <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $str<span class="token">=</span><span class="token4">""</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$arr as $v<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">2</span><span class="token">==</span>$i<span class="token2">)</span><span class="token2">{</span> $str<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$cl<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> $i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">1</span><span class="token">==</span>$i<span class="token2">)</span><span class="token2">{</span> $str<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$cl<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> $i<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">0</span><span class="token">==</span>$i<span class="token2">)</span><span class="token2">{</span> $str<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$cl<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> $i<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> echo $str<span class="token2">;</span> <span class="token6">/** * 每两列都不同的内容输出,奇偶判断,方法1 */</span> $al <span class="token">=</span><span class="token2">[</span><span class="token4">"x"</span><span class="token2">,</span><span class="token4">"y"</span><span class="token2">]</span><span class="token2">;</span> $j<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> $str1<span class="token">=</span><span class="token4">""</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$arr as $v<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span><span class="token3">1</span><span class="token">==</span>$j<span class="token2">)</span><span class="token2">{</span> <span class="token6">//奇数</span> $str1<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$al<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> $j<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span><span class="token2">{</span> $str1<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$al<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> $j<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> echo $str1<span class="token2">;</span> <span class="token6">/** * 每两列都不同的内容输出,奇偶判断,方法2 */</span> $str2<span class="token">=</span><span class="token4">""</span><span class="token2">;</span> $k<span class="token">=</span><span class="token3">0</span><span class="token2">;</span> foreach <span class="token2">(</span>$arr as $v<span class="token2">)</span><span class="token2">{</span> <span class="token5">if</span><span class="token2">(</span>$k<span class="token">%</span><span class="token3">2</span><span class="token">==</span><span class="token3">0</span><span class="token2">)</span><span class="token2">{</span> <span class="token6">//偶数</span> $str2<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$al<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> $str2<span class="token2">.</span><span class="token">=</span><span class="token4">"<li >"</span><span class="token2">.</span>$v<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">.</span>$al<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"</li>"</span><span class="token2">;</span> <span class="token2">}</span> $k<span class="token">++</span><span class="token2">;</span> <span class="token2">}</span> echo $str2<span class="token2">;</span> ``` ```