Home > Backend Development > PHP Tutorial > Simple PHP algorithm questions (with expansion)

Simple PHP algorithm questions (with expansion)

WBOY
Release: 2016-08-08 09:21:10
Original
1441 people have browsed it

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>?>
Copy after login
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>?>
Copy after login
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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template