首頁  >  文章  >  後端開發  >  容易的PHP算法题(带扩充)

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

WBOY
WBOY原創
2016-06-13 12:22:51958瀏覽

简单的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>?>
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>?>
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>?>
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>?>
可处理加减乘除的四位一体计算函数

 

进阶:

  水仙花数

  冒泡排序法

1楼CorvoH
霍霍霍霍……原来这个最费时,却最有趣!
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn