简单的PHP算法题(带扩充)
简单的PHP算法题(待完善…)
只打印0
具体个数由输入的参数n决定
如n=5就打印00000
打印一行 0101010101010101010101
具体个数由输入的参数n决定
如test.php?n=3打印010
实现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>?>可处理加减乘除的四位一体计算函数
进阶:
水仙花数
冒泡排序法