php使用環(huán)形鏈表解決約瑟夫問題完整示例
本文實(shí)例講述了php使用環(huán)形鏈表解決約瑟夫問題。分享給大家供大家參考,具體如下:
約瑟夫問題:
Josephu問題為:設(shè)編號為1,2,...n的n個(gè)人圍坐一圈,約定編號為k(1<=k<=n)的人從1開始報(bào)數(shù),數(shù)到m的那個(gè)人出列,它的下一位又從1開始報(bào)數(shù),數(shù)到m的那個(gè)人又出列,依次類推,直到所有人出列為止,由此產(chǎn)生一個(gè)出隊(duì)編號的序列。并求出最后出列的人是哪個(gè)?
PHP實(shí)現(xiàn)環(huán)形鏈表以及約瑟夫問題的解決:
/** * 鏈表結(jié)構(gòu) */ class Child{ public $no; public $next=null; public function __construct($no=null){ $this->no = $no; } } /** * 鏈表操作 */ class CycleLink{ private $nodeNum = 0; /** * 添加節(jié)點(diǎn) */ 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++; } /** * 刪除節(jié)點(diǎn) */ 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; } } /** * 獲取節(jié)點(diǎn)數(shù)量 */ public function getNodeNum(){ return $this->nodeNum; } /** * 查找節(jié)點(diǎn) */ 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; } /** * 顯示節(jié)點(diǎn) */ public function showNode($head) { echo "<br/><br/>"; $currentNode = $head; while ($currentNode->next!=$head){ $currentNode = $currentNode->next; echo '第 '.$currentNode->no.' 名小孩<br/>'; } } } /* //創(chuàng)建一個(gè)head頭,該head 只是一個(gè)頭,不放入數(shù)據(jù) $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(); */ /** * 約瑟夫問題 * 設(shè)編號為1,2,...n的n個(gè)人圍坐一圈,約定編號為k(1<=k<=n)的人從1開始報(bào)數(shù),數(shù)到m的那個(gè)人出列, * 它的下一位又從1開始報(bào)數(shù),數(shù)到m的那個(gè)人又出列,依次類推,直到所有人出列為止,由此產(chǎn)生一個(gè)出隊(duì)編號的序列。 * 并求出最后出列的人是哪個(gè)? */ 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); // 創(chuàng)建小孩 echo "<br/><br/>當(dāng)前有 {$n} 個(gè)小孩,從第 {$k} 個(gè)小孩開始報(bào)數(shù),數(shù)到 {$m} 退出<br/><br/>"; } // 數(shù)數(shù) public function exec(){ $currentNode = $this->childList->findNode($this->head,$this->k); // 獲取第一個(gè)開始報(bào)數(shù)的人 $_num = 0; // 當(dāng)前數(shù)到的值 $surplus_num = $this->n; // 開始報(bào)數(shù) while ($surplus_num>1) { // 只要人數(shù)大于1,就繼續(xù)報(bào)數(shù) // 當(dāng)前報(bào)數(shù)值 $_num++; $nextNode = $this->childList->getNextNode($this->head,$currentNode); // 數(shù)至第m個(gè)數(shù),然后將其移除。報(bào)數(shù)恢復(fù)到0,重新循環(huán)。 if( $_num==$this->m ){ $_num = 0; $surplus_num--; // 當(dāng)前小孩退出 $this->childList->delNode($this->head,$currentNode->no); echo '<br/>退出小孩編號:' . $currentNode->no; } // 移動(dòng)到下一個(gè)小孩 $currentNode = $nextNode; } echo '<br/>最后一個(gè)小孩編號:' . $currentNode->no; } // 創(chuàng)建小孩 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();
運(yùn)行結(jié)果:
第 1 名小孩
第 2 名小孩
第 3 名小孩
第 4 名小孩
當(dāng)前有 4 個(gè)小孩,從第 1 個(gè)小孩開始報(bào)數(shù),數(shù)到 2 退出
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用PHPMailer實(shí)現(xiàn)郵件的實(shí)時(shí)發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了如何使用PHPMailer 實(shí)現(xiàn)一個(gè)接收詢盤并實(shí)時(shí)同步到指定郵箱的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-12-12php使用redis的有序集合zset實(shí)現(xiàn)延遲隊(duì)列應(yīng)用示例
這篇文章主要介紹了php使用redis的有序集合zset實(shí)現(xiàn)延遲隊(duì)列,結(jié)合具體實(shí)例形式分析了PHP基于redis的有序集合zset實(shí)現(xiàn)延遲隊(duì)列的具體原理、應(yīng)用場景及相關(guān)操作技巧,需要的朋友可以參考下2020-02-02PHP在innodb引擎下快速代建全文搜索功能簡明教程【基于xunsearch】
這篇文章主要介紹了PHP在innodb引擎下快速代建全文搜索功能的方法,可基于開源搜索引擎xunsearch實(shí)現(xiàn),簡明扼要的講述了安裝與使用的步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10php中拷貝構(gòu)造函數(shù)、賦值運(yùn)算符重載
php中拷貝構(gòu)造函數(shù)、賦值運(yùn)算符重載方法, 需要的朋友可以參考下2012-07-07PHP實(shí)現(xiàn)判斷數(shù)組是一維、二維或幾維的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)判斷數(shù)組是一維、二維或幾維的方法,涉及php遞歸操作及數(shù)組相關(guān)判定技巧,需要的朋友可以參考下2017-02-02php中利用post傳遞字符串重定向的實(shí)現(xiàn)代碼
php中利用post傳遞字符串重定向的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-04-04php從csv文件讀取數(shù)據(jù)并輸出到網(wǎng)頁的方法
這篇文章主要介紹了php從csv文件讀取數(shù)據(jù)并輸出到網(wǎng)頁的方法,涉及php中fgetcsv函數(shù)及數(shù)組遍歷的使用技巧,需要的朋友可以參考下2015-03-03php Notice: Undefined index 錯(cuò)誤提示解決方法
字面意思就是未定義的索引,一般情況下是因?yàn)槌绦蜷_發(fā)作者判斷不嚴(yán)謹(jǐn)導(dǎo)致。一般不會影響程序的運(yùn)行,具體的解決方法可以參考下。2010-08-08