This article illustrates several classical PHP algorithms. Share to everyone for your reference, specific as follows:
5 people stole a pile of apples and were ready to split the spoils the next day. In the evening, one person walks out, divides all vegetable fruit into 5, but one more, conveniently throws this to the tree monkey, oneself first takes 1/5 to hide. I didn't think the other four people also think so, as the first person is divided into 5 pieces of the one threw to the monkey, stole 1/5. The next day, we split the spoils, but also divided into 5 more one throw to the monkey. The last one was divided. Q: How many apples are there?
for ($i = 1; $i + +)
{
if ($i%5 = 1) {
//The first person takes one-fifth, the remaining $t
$t = $i-round ($i/5)-1;
if ($t% 5 = 1)
{
//second person takes one-fifth, left "
$r = $t-round ($t/5)-1;
if ($r% 5 = 1)
{
//The third person takes one-fifth, the remaining $s
$s = $r-round ($r/5)-1;
if ($s% 5 = 1)
{
//Fourth person take one-fifth, still left $x
$x = $s-round ($s/5)-1;
if ($x% 5 = 1)
{
//fifth person take one-fifth, still left $y
$y = $x-round ($x/5)-1;
if ($y% 5 = 1) {
echo $i;
Break;}}}}
A group of monkeys in a circle, press 1,2,...,n sequentially numbered. And then starting from the 1th number, count to M only, kick It out of the loop, start from behind it, Count to M only, kick It out ..., keep going until the last monkey is left, the monkey is called King. Requires programming to simulate this process, input m, n, output the last king's number.
function King ($n, $m) {
$monkeys = range (1, $n);
$i =0;
$k = $n;
while (count ($monkeys) >1) {
if ($i + 1)% $m ==0) {
unset ($monkeys [$i]);
} else {
Array_push ($monkeys , $monkeys [$i]);
Unset ($monkeys [$i]);
}
$i + +;
}
Return current ($monkeys);
$a = King (5, 2);
Var_dump ($a);
Hanoi (also known as Heneta) is an ancient legend in India. The epoch-making God Blama left three rods of diamond in a temple, the first with 64 round gold pieces, the biggest one is below, the other one is smaller than the other, and the monks in the temple have tirelessly moved them from the rod to the other, providing that a rod in the middle can be used as a help, But only one can be moved at a time, and the big can't be placed on the small. Answer the results please run your own calculations, the program see the tail. Faced with a huge number (the number of moves to the wafer) 18446744073709551615, it seems that the monks have spent their whole life to complete the movement of the gold piece.
Later, the legend evolved into a Hanoi game:
1. There are three Poles a,b,c. There are several plates on a pole.
2. Move a plate each time, the small can only be stacked on top of the big
3. Move all plates from a to C pole
After the study found that Hanoi's crack is very simple, that is, according to the moving rules in one direction to move the gold film:
such as 3-step Hanoi movement: A→c,a→b,c→b,a→c,b→a,b→c,a→c
In addition, the Hanoi tower problem is also a classical recursive problem in program design.
function Hanoi ($n, $x, $y, $z) {
if ($n ==1) {
echo ' move disk 1 from '. $x '. $z. " \ n ";
} else{
Hanoi ($n-1, $x, $z, $y);
echo ' Move disk ', $n. ' From ', $x ' to '. $z. " \ n ";
Hanoi ($n-1, $y, $x, $z);
}
Hanoi (3, ' A ', ' B ', ' C ');
Using PHP to describe the bubble sort and fast sort algorithm, an object can be an array
Array bubble sort
function Bubble_sort ($array) {
$count = count ($array);
if ($count <= 0) return
false;
For ($i =0 $i < $count; $i + +) {for
($j = $count-1; $j > $i; $j-) {
if ($array [$j] < $array [$j-1]) {
$tmp = $ array[$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}} return $array;
}
function Quick_sort ($array) {
if (count ($array) <= 1) return $array;
$key = $array [0];
$left _arr = Array ();
$right _arr = Array ();
For ($i =1 $i <count ($array); $i + +) {
if ($array [$i] <= $key)
$left _arr[] = $array [$i];
else
$right _arr[] = $array [$i];
}
$left _arr = Quick_sort ($left _arr);
$right _arr = Quick_sort ($right _arr);
Return Array_merge ($left _arr, Array ($key), $right _arr);
Using PHP to describe order lookup and binary lookup algorithm, sequential lookup must consider efficiency, object can be an ordered array
Use binary to find an element
function Bin_sch ($array, $low, $high, $k) in an array {
if ($low <= $high) {
$mid = intval ($low + $high )/2);
if ($array [$mid] = = $k) {return
$mid;
} ElseIf ($k < $array [$mid]) {return
Bin_sch ($array, $low, $mid-1, $k);
} else{return
Bin_sch ($array, $mid +1, $high, $k);
}
return-1;
}
Write a two-dimensional array sorting algorithm function, you can call PHP built-in functions, can have universal
function Array_sort ($arr, $keys, $order =0) {
if (!is_array ($arr)) {return
false;
}
$keysvalue = Array ();
foreach ($arr as $key => $val) {
$keysvalue [$key] = $val [$keys];
}
if ($order = = 0) {
asort ($keysvalue);
} else {
arsort ($keysvalue);
}
Reset ($keysvalue);
foreach ($keysvalue as $key => $vals) {
$keysort [$key] = $key;
}
$new _array = Array ();
foreach ($keysort as $key => $val) {
$new _array[$key] = $arr [$val];
}
return $new _array;
}
I hope this article will help you with your PHP programming.