Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Joseph Ring by PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "PHP how to solve the Joseph ring problem". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "PHP how to solve the Joseph ring problem".

Joseph's Ring problem (Monkey chooses King) PHP version

The Josephus problem is sometimes described as the Monkey King selection problem, with the following topics. (finally, the origin of Joseph's question will be posted.)

A group of monkeys line up in a circle, press 1 ~ 2, … , n is numbered sequentially.

Then count from the first, count to the m, kick it out of the circle, start counting from behind it, then count to the m, kick it out. It went on and on until there was only one monkey left, and that monkey was called the king.

Require programming to simulate this process, enter m, n, and output the number of the last king.

Use the program to simulate the process, the code is as follows:

Function monkeyKing ($n, $m) {$arr = range (1, $n); / / construct an array $I = 1; / / cycle while from the first (count ($arr) > 1) {/ / if the total is greater than 1 if ($I% $m! = 0) {$arr [] = $arr [$I-1] / / press into the end of the array} unset ($arr [$I-1]) if you don't get kicked out; / / press into the array and delete $iCombined; / / continue to cycle} return $arr [$I-1]; / / until the last one is the king} print_r (monkeyKing (5,3)); / the fourth is only the king.

The following is the process of simulation, in which monkeys that are not removed are constantly added to the tail of the array:

$n = 5$ m = 3$ arr = [1,2,3,4 5] $I $arr-1 x 2 3 4 5 1 2 x x 3 4 5 1 2 2 > 3 x x x 4 5 1 2 4 4 x x x x 5 1 2 4 5 x x x x x 1 2 4 5 > 6 x x x x x x 2 4 7 x x x x x x x 4 5 2 8 x x x x x x x x 5 24 > 9 x x x x x X x x x 2 4 10 x x x x x x x x x x 4 2 11 x x x x x x x x x x x 2 4 > 12 x x x x x x x x x x x x 4

The origin of Joseph's problem:

The question is named after Flavio Joseph, a Jewish historian in the first century. He wrote in his diary that he and his 40 comrades-in-arms were surrounded by Roman troops in the cave. They discussed whether to commit suicide or be captured, and finally decided to commit suicide and draw lots to decide who would kill whom. Josephs and the other man were the last two to stay. Josephus persuaded the man that they would surrender to the Roman army and not commit suicide. Joseph attributed his survival to luck or providence, which he did not know.

Thank you for your reading, the above is the content of "how PHP solves the Joseph ring problem". After the study of this article, I believe you have a deeper understanding of how PHP solves the Joseph ring problem, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report