PHP Classic algorithm Collection, PHP algorithm collection _php tutorial

Source: Internet
Author: User

PHP Classic algorithm Collection, PHP algorithm collection


In this paper, some classical algorithms of PHP are described. Share to everyone for your reference, as follows:

5 people stole a bunch of apples and were ready to split the loot the next day. In the evening, one person walks out, all the vegetables fruit divides into 5 parts, but one more, conveniently throws this to the tree the monkey, oneself first takes 1/5 hides. I did not think of the other four people think so, all as the first person into 5 parts of the more that one threw to the monkey, stole 1/5. The next day, we divide the loot, is also divided into 5 parts more one throws to the monkey. The last person has a share. Q: How many apples are there?

for ($i = 1;; $i + +) {  if ($i%5 = = 1) {    ///The first person takes one-fifth and $t    $t = $i-round ($i/5)-1;    if ($t% = = 1)    {      //The second person takes one-fifth, remaining "      $r = $t-round ($t/5)-1;      if ($r% = = 1)      {        //The third person takes one-fifth, $s        $s = $r-round ($r/5)-1;        if ($s% = = 1)        {          //Fourth person fetch one-fifth, $x          $x = $s-round ($s/5)-1;          if ($x% = = 1)          {            //fifth person fetch one-fifth, $y            $y = $x-round ($x/5)-1;            if ($y% 5 = = 1) {              echo $i;              Break;}}}}}}  

A group of monkeys in a circle, according to 1,2,...,n sequentially numbered. Then, starting from the 1th, Count to M., Kick It Out of the loop, start counting from behind it, Count to M., and Kick It out ... and so on, until the last monkey is left, the monkey is called the King. Requires programming to simulate this process, enter M, N, and output the number of the last king.

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 Hanoi) is an ancient Indian legend. The epoch-making God Blama in a temple left three diamond rods, the first one with 64 round gold pieces, the largest in the bottom, the other one smaller than a small, in turn, the monks in the temple tirelessly to move them from this stick to another stick, the provisions can be used in the middle of a stick as a help, But only one can be moved at a time, and the big cannot be placed on the small. Please run the calculation yourself, the program see the tail. Faced with a huge number (the number of moving the circle) 18446744073709551615, it seems that the monks exhausted their life and energy will not be able to complete the movement of the gold pieces.

Later, the legend evolved into a Hanoi game:

1. There are three Poles a,b,c. There are several plates on the A-pole.
2. Each time you move a plate, the small one can only be stacked on top of the big one.
3. Move all the plates from the A to the C-bar

It is found that Hanoi's crack is simple: move the gold sheet in one Direction according to the moving rules.
such as the movement of the 3-step Hanoi: A→c,a→b,c→b,a→c,b→a,b→c,a→c

In addition, the problem of Hanoi Tower is also a classic recursive problem in program design.

function Hanoi ($n, $x, $y, $z) {  if ($n ==1) {    echo ' move disk 1 from '. $x. ' to '. $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 bubbling sort and quick 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
 
  

Using PHP to describe order lookup and binary lookup algorithms, sequential lookups must consider efficiency, an object can be an ordered array

Use two points to find an element in the array function Bin_sch ($array, $low, $high, $k) {  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 is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1071392.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071392.html techarticle PHP Classic Algorithm collection, PHP algorithm collection in this paper, some of the classic PHP algorithms are described. Share to everyone for your reference, as follows: 5 people stole a bunch of apples, ready for the next day ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.