Home > Article > Backend Development > Simple PHP algorithm questions (with expansion)
Simple PHP algorithm question (to be improved...)
Only print 0
The specific number is determined by the input parameter n
If n=5, print 00000
Print n 0s according to the n value
Print a line of 0101010101010101010101
The specific number is determined by the input parameter n
For example, test.php?n=3 prints 010
and prints 010101 according to the n value...
to achieve 1 00 111 0000 1 1111
for if implementation
<?<span>php </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < 10; <span>$i</span>++<span>) { </span><span>for</span> (<span>$j</span> = 0; <span>$j</span> <= <span>$i</span>; <span>$j</span>++<span>) { </span><span>if</span> (<span>$i</span> % 2 == 0<span>) { </span><span>echo</span> '0'<span>; } </span><span>else</span><span> { </span><span>echo</span> '1'<span>; } } </span><span>echo</span> '<br/>'<span>; } </span>?>for&if statement implementation
for switch implementation
while if implementation
while switch implementation
<?<span>php </span><span>$i</span> = 0<span>; </span><span>while</span> (<span>$i</span> < 10<span>) { </span><span>$j</span> = 0<span>; </span><span>while</span> (<span>$j</span> <= <span>$i</span><span>) { </span><span>switch</span> (<span>$i</span> % 2<span>) { </span><span>case</span> 0: <span>echo</span> '0'<span>; </span><span>break</span><span>; </span><span>case</span> 1: <span>echo</span> '1'<span>; </span><span>break</span><span>; } </span><span>$j</span>++<span>; } </span><span>echo</span> '<br/>'<span>; </span><span>$i</span>++<span>; } </span>?>while&switch statement implementation
Achieve 0 01 010 0101……
Realize 0 01 012 0123 3210 210 10 0
Make a calculator
For example, test.php?a=1&b=2&operator=jia outputs 3
For example, test.php?a=5&b=2&operator=jian outputs 3
For example test.php?a=2&b=5&operator=cheng outputs 10
For example, test.php?a=6&b=3&operator=chu outputs 2
Quarterine calculation function that can handle addition, subtraction, multiplication and division
Advanced:
Number of narcissus
Bubble sort method
The above introduces simple PHP algorithm questions (with expansion), including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.