容易的PHP算法题(带扩充)

WBOY
Freigeben: 2016-06-13 12:22:51
Original
958 Leute haben es durchsucht

简单的PHP算法题(带扩充)

简单的PHP算法题(待完善…)

 

只打印0

  具体个数由输入的参数n决定

  如n=5就打印00000

根据n值打印n个0

 

打印一行 0101010101010101010101 

  具体个数由输入的参数n决定

  如test.php?n=3打印010

根据n值打印010101…

 

实现1 00 111 0000 11111

  for if 实现

<span style="color: #000000;">php</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $i++<span style="color: #000000;">) {    </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0; <span style="color: #800080;">$j</span> $i; <span style="color: #800080;">$j</span>++<span style="color: #000000;">) {        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$i</span> % 2 == 0<span style="color: #000000;">) {            </span><span style="color: #0000ff;">echo</span> '0'<span style="color: #000000;">;        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {            </span><span style="color: #0000ff;">echo</span> '1'<span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;}</span>?>
Nach dem Login kopieren
for&if语句实现

  for switch 实现

<span style="color: #000000;">php</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $i++<span style="color: #000000;">) {    </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0; <span style="color: #800080;">$j</span> $i; <span style="color: #800080;">$j</span>++<span style="color: #000000;">) {        </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$j</span> % 2<span style="color: #000000;">) {            </span><span style="color: #0000ff;">case</span> '0':            <span style="color: #0000ff;">echo</span> "0"<span style="color: #000000;">;            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">case</span> '1':            <span style="color: #0000ff;">echo</span> "1"<span style="color: #000000;">;            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;}</span>?>
Nach dem Login kopieren
for&switch语句实现

  while if 实现

  while switch 实现

<span style="color: #000000;">php</span><span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;</span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$i</span> ) {    <span style="color: #800080;">$j</span> = 0<span style="color: #000000;">;    </span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$j</span> $i<span style="color: #000000;">) {        </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$i</span> % 2<span style="color: #000000;">) {            </span><span style="color: #0000ff;">case</span> 0:                <span style="color: #0000ff;">echo</span> '0'<span style="color: #000000;">;                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> 1:                <span style="color: #0000ff;">echo</span> '1'<span style="color: #000000;">;                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        }        </span><span style="color: #800080;">$j</span>++<span style="color: #000000;">;    }    </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;    </span><span style="color: #800080;">$i</span>++<span style="color: #000000;">;}</span>?>
Nach dem Login kopieren
while&switch语句实现

 

实现0 01 010 0101……

 

实现0 01 012 0123 3210 210 10 0

 

做个计算器

  如test.php?a=1&b=2&operator=jia输出3

  如test.php?a=5&b=2&operator=jian输出3

  如test.php?a=2&b=5&operator=cheng输出10

  如test.php?a=6&b=3&operator=chu输出2

<span style="color: #000000;">php    </span><span style="color: #800080;">$a</span> = <span style="color: #800080;">$_GET</span>['a'<span style="color: #000000;">];    </span><span style="color: #800080;">$b</span> = <span style="color: #800080;">$_GET</span>['b'<span style="color: #000000;">];    </span><span style="color: #800080;">$operator</span> = <span style="color: #800080;">$_GET</span>['operator'<span style="color: #000000;">];    </span><span style="color: #0000ff;">function</span> calculate(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span>,<span style="color: #800080;">$operator</span><span style="color: #000000;">) {        </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$operator</span><span style="color: #000000;">) {            </span><span style="color: #0000ff;">case</span> 'jia':                <span style="color: #800080;">$result</span> = <span style="color: #800080;">$a</span> + <span style="color: #800080;">$b</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> 'jian':                <span style="color: #800080;">$result</span> = <span style="color: #800080;">$a</span> - <span style="color: #800080;">$b</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> 'cheng':                <span style="color: #800080;">$result</span> = <span style="color: #800080;">$a</span> * <span style="color: #800080;">$b</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">case</span> 'chu':                <span style="color: #800080;">$result</span> = <span style="color: #800080;">$a</span> / <span style="color: #800080;">$b</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">echo</span> calculate(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span>,<span style="color: #800080;">$operator</span><span style="color: #000000;">);</span>?>
Nach dem Login kopieren
可处理加减乘除的四位一体计算函数

 

进阶:

  水仙花数

  冒泡排序法

1楼CorvoH
霍霍霍霍……原来这个最费时,却最有趣!
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage