This article describes the use of a circular list for PHP to solve the Joseph problem. Share it for your reference, as follows:
Joseph's question:
The Josephu problem is: Suppose n people with numbers 1, 2,...n sit in a circle, and the person with number k (1<=k<=n) starts to count from 1, the person with number to m is delisted, its next one starts to count from 1, the person with number to m is delisted, and so on, until everyone is delisted, thus generating a sequence of dequeue numbers. And find out which person is the last one to be listed?
PHP implements circular linked lists and solves Joseph's problem:
/** * Linked list structure */ class Child{ public $no; public $next=null; public function __construct($no=null){ $this->no = $no; } } /** * Linked List Operation */ class CycleLink{ private $nodeNum = 0; /** * Add node */ public function addNode($head,$node) { $currentNode = $head; while ($currentNode->next!=null && $currentNode->next!=$head) { $currentNode = $currentNode->next; } $currentNode->next = $node; $currentNode->next->next = $head; $this->nodeNum++; } /** * Delete nodes */ public function delNode($head,$no) { $currentNode = $head; while ($currentNode->next!=$head) { if($currentNode->next->no==$no){ $currentNode->next = $currentNode->next->next; $this->nodeNum--; break; } $currentNode = $currentNode->next; } } /** * Get the number of nodes */ public function getNodeNum(){ return $this->nodeNum; } /** * Find nodes */ public function findNode($head,$no){ $node = null; $currentNode = $head; while ($currentNode->next!=$head) { if($currentNode->next->no==$no){ $node = $currentNode->next; break; } $currentNode = $currentNode->next; } return $node; } public function getNextNode($head,$node){ if($node->next==$head){ return $node->next->next; } return $node->next; } /** * Display node */ public function showNode($head) { echo "<br/><br/>"; $currentNode = $head; while ($currentNode->next!=$head){ $currentNode = $currentNode->next; echo 'Part '.$currentNode->no.'A child<br/>'; } } } /* //Create a head head, the head is just a header, and does not put data in $head = new Child(); $childList = new CycleLink(); $child_1 = new Child(1); $child_2 = new Child(2); $child_3 = new Child(3); $child_4 = new Child(4); $childList->addNode($head,$child_1); $childList->addNode($head,$child_2); $childList->addNode($head,$child_3); $childList->addNode($head,$child_4); $childList->showNode($head); echo "<pre>"; var_dump($head); $findNode = $childList->findNode($head,3); echo "<pre>"; var_dump($findNode); $childList->delNode($head,2); $childList->showNode($head); echo $childList->getNodeNum(); exit(); */ /** * Joseph's question * Suppose n people with numbers 1, 2,...n sit in a circle, and the person with number k (1<=k<=n) starts to count from 1, and the person with number m is out of the list. * Its next one starts to count from 1, the person who counts to m is delisted again, and so on, until everyone is delisted, thus generating a sequence of dequeue numbers. * And find out which person is the last one to be listed? */ class Josephu{ private $head; private $childList; private $k; private $m; private $n; public function __construct($n,$k,$m){ $this->k = $k; $this->m = $m; $this->n = $n; $this->createList($n); // Create a child echo "<br/><br/>Currently there {$n} A little kid,From {$k} A little kid开始报数,Count to {$m} quit<br/><br/>"; } // Count public function exec(){ $currentNode = $this->childList->findNode($this->head,$this->k); // Get the first person to start counting $_num = 0; // The value currently reached $surplus_num = $this->n; // Start counting while ($surplus_num>1) { // As long as the number of people is greater than 1, continue to report the number // Current value $_num++; $nextNode = $this->childList->getNextNode($this->head,$currentNode); // Count to mth number and remove it. The report count returns to 0 and re-cycle. if( $_num==$this->m ){ $_num = 0; $surplus_num--; // The current child quits $this->childList->delNode($this->head,$currentNode->no); echo '<br/>Exit child number:' . $currentNode->no; } // Move to the next child $currentNode = $nextNode; } echo '<br/>The last child number:' . $currentNode->no; } // Create a child private function createList($n){ $this->childList = new CycleLink(); $this->head = new Child(); for ($i=1;$i<=$n;$i++){ $node = new Child($i); $this->childList->addNode($this->head,$node); } $this->childList->showNode($this->head); } } $josephu = new Josephu(4, 1, 2); $josephu->exec();
Running results:
No. 1 child
No. 2 Child
No. 3 Child
No. 4 Child
There are currently 4 children, start counting from the first child, and count to 2 and exit
For more information about PHP related content, please check out the topic of this site:PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of common traversal algorithms and techniques for PHP"and"Summary of PHP mathematical operation skills》
I hope this article will be helpful to everyone's PHP programming.