PHP implementation of the Joseph problem of the summary of methods, PHP implementation of Josef Summary _php tutorial

Source: Internet
Author: User

PHP Implementation Joseph Summary of the method of the problem, PHP implementation


This paper summarizes the methods of implementing the Josephson problem in PHP. Share to everyone for your reference. The specific analysis is as follows:

A group of monkeys in a circle, according to the,..., 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.

Analytical:

Joseph Ring is a mathematical application problem: known n individuals (denoted by number 1,2,3...N respectively) sit around a round table. From the person numbered K began to count, the number to m of the person out of the man, and his next person from 1 began to count, the number to m of the person again, according to this rule repeat until the round table all the people out.

Method One:

<?phpfunction Getleader ($n, $m) {   $res =0;   for ($i =2; $i <= $n; $i + +) {    $res = ($res + $m)% $i;   }  return $res +1; } $leader = Getleader (13,34); Echo $leader;? >

Method Two:

<?php//defines function getking ($monkeys, $m, $current = 0) {  $number = count ($monkeys);  $num = 1;  if (count ($monkeys) = = 1) {    echo ' is numbered '. $monkeys [0]. ' Monkey became Monkey King! ';    return;  } else{    while ($num + + < $m) {      $current + +;      $current = $current% $number;    }    echo "is numbered". $monkeys [$current]. " The monkeys were kicked off ...
"; Array_splice ($monkeys, $current, 1); Getking ($monkeys, $m, $current); }} $n =13; Total number of monkeys $m =; The first few monkeys were kicked out. $monkeys = Range (1, $n); Put the monkey number in the array getking ($monkeys, $m); Call Function?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/972649.html www.bkjia.com true http://www.bkjia.com/PHPjc/972649.html techarticle PHP Implementation of the Josephson problem method summary, PHP Implementation Joseph Summary This paper summarizes the method of implementing the Josephson problem in PHP. Share to everyone for your reference. The specific analysis is as follows: one ...

  • Related Article

    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.