PHP Joseph's problem

Source: Internet
Author: User
Problem Description: there are n people in a circle, starting from any one person, report the number in sequence. when the number is m, * the m person is out of the circle (that is, out of the circle ), the next person in the field continues to report data starting from 1. when the number of m is reached, * The number of m members is the same as that in the field, and so on. Header ("content-type: text/html; charset = utf-8 ");
Set_time_limit (0 );
/**
* Joseph's problem
*
* Also known as handkerchief loss
* Problem description: there are n people in a circle, starting from any one person, reporting the number in sequence. when the number is m,
* When the number of m People goes out of the circle (that is, when the number of m People goes out of the circle), the next person in the field continues to report data starting from 1. when the number of m People reaches m,
* The m number is also out of bounds, and so on.
*
* Q: the order of departure of each person and the order of departure of the last person
**//**
* Construct an array
*
* @ Param int $ n number of array elements
* @ Return array
**/
$ Arr = array (); // n people use an array with n elements.
Function initarray ($ n ){
Global $ arr;
For ($ I = 1; $ I <= $ n; $ I ++ ){
$ Arr [$ I] = array (0 );
}
}
/**
* Find out the order in which each person goes out of bounds
*
* @ Param int $ n number of elements in the array
* @ Param int $ m the maximum number of reports
* @ Param int $ p starts to report the number of p individuals (in the order of the original circle ).
* @ Param int $ k number $ k output
* @ Return void
**/
Function joseph ($ n, $ m, $ p, $ k ){
Global $ arr;
If ($ n <= 1 ){
Echo "no less than 2 ";
}
For ($ j = 1; $ j <= $ m; $ j ++ ){
If ($ p> $ n ){
$ P = 1;
}
If ($ arr [$ p] [0]! = 0 ){
$ J --;
}
$ P ++;
}
$ Arr [$ P-1] [0] = $ k;
$ Y = 0; // counter of the person already exists
Foreach ($ arr as $ value ){
If ($ value [0]! = 0 ){
$ Y ++;
}
}
If ($ y <$ n ){
Joseph ($ n, $ m, $ p, ++ $ k );
}
}
Initarray (41 );
Echo joseph );
Foreach ($ arr as $ k => $ value ){
If ($ value [0] = count ($ arr )){
$ Last = $ k;
}
Echo "the $ k person will be the first
". $ Value [0]." times out of bounds
";
}
Echo "the last person out of the world is the first". $ last. "person ";
?>

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.