PHP約瑟夫小游戲的代碼示例
來源:不言
發(fā)布時間:2019-03-23 16:13:26
閱讀量:1328
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于PHP約瑟夫小游戲的代碼示例,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
30 個人在一條船上,超載,需要 15 人下船。于是人們排成一隊,排隊的位置即為他們的編號。報數(shù),從 1 開始,數(shù)到 9 的人下船。如此循環(huán),直到船上僅剩 15 人為止,問都有哪些編號的人下船了呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | $numberStart = 30;
$numberLive = 15;
$numKill = 9;
$peopleList = [];
for($i=1; $i<=$numberStart; $i++){
$peopleList[$i]=1;
}
$numSay=0;
$i =1;
$maxIndex = $numberStart;
while (true){
if($i > $maxIndex ){
$i = 1;
}
if($numberStart==$numberLive){
break;
}
if($peopleList[$i]==0){
$i++;
continue;
}
$numSay++;
if($numSay==$numKill){
$peopleList[$i] = 0;
$numSay = 0;
echo $i.'號下船了'.PHP_EOL;
$numberStart--;
}
$i++;
}
print_r($peopleList);
|