Home>

5 people stole a bunch of apples,Ready to share the booty the next day.At night, a man came out,Divide all the fruits and vegetables into five, but one more,Drop this to the monkey on the tree,Take 1/5 of your own hide. I didn't expect the other four to think the same way,They were divided into 5 parts like the first one and the more one was thrown to the monkey.Stolen 1/5. The next day, everyone shared the loot,It is also divided into 5 more and thrown to the monkey.The last one divided.Q:How many apples are there?

for ($i=1;;$i ++)
{
  if ($i%5 == 1) {
    //The first person takes one fifth,$T left
    $t=$i-round ($i/5)-1;
    if ($t%5 == 1)
    {
      //The second person takes one fifth,$R left
      $r=$t-round ($t/5)-1;
      if ($r%5 == 1)
      {
        //The third person takes one fifth,$S left
        $s=$r-round ($r/5)-1;
        if ($s%5 == 1)
        {
          //The fourth person takes one fifth,$X left
          $x=$s-round ($s/5)-1;
          if ($x%5 == 1)
          {
            //The fifth person takes one fifth,$Y left
            $y=$x-round ($x/5)-1;
            if ($y%5 == 1) {
              echo $i;
              break;
            }
          }
        }
      }
    }
  }
}

A group of monkeys lined up,Number 1, 2, ..., n in order.Then start counting from the first one,Count to the mth, kick it out of the circle,Start counting from behind,Counting to the mth, kicking it out ... and so on and on,Until the last monkey left,That monkey is called King.Requires programming to simulate this process,Enter m and 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);

The Hanoi Tower (also known as Hanoi Tower) is an ancient legend in India.The groundbreaking god Brahma left three diamond rods in a temple,The first one has 64 round gold pieces on it,The biggest one is underneath,The other one is smaller than one,Stacked up one by one,The monks in the temple tirelessly moved them one by one from this stick to another,Stipulated that a middle stick could be used as a help,But you can only move one at a time,And the big ones cannot be placed on the small ones.Please answer the calculation results by yourself.See the end of the procedure.Faced with a huge number (the number of times the wafer was moved) 18446744073709551615, it seems that the monks exhausted their lives and could not complete the movement of gold.

Later, this legend evolved into a game of Hanoi:

1. There are three poles a, b, c. a number of plates on pole a

2. Move one dish at a time,Small ones can only stack on top

3. Move all the dishes from pole a to pole c

After research found thatHanoi ’s crack is simple,Is to move the sequins in one direction according to the rules of movement:

Such as the movement of the third-order Hanoi tower:a → c, a → b, c → b, a → c, b → a, b → c, a → c

In addition, the Hanoi Tower problem is also a classic recursion problem in programming.

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");

Use php to describe bubble sort and quick sort algorithms,Object can be an array

//Bubble sort the array
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);
}

Use PHP to describe sequential and binary search algorithms,Sequential search must consider efficiency,Object can be an ordered array

//Use a binary search for 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,Can call php built-in functions,Can be 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;
}
php
  • Previous A complete instance of the PHP wrapper class implementing PHP redis
  • Next Basic concepts and use of thread pool in Java programming