Home > Backend Development > PHP Tutorial > PHP Joseph game code example

PHP Joseph game code example

不言
Release: 2023-04-05 08:12:02
forward
3788 people have browsed it

This article brings you code examples about the PHP Joseph game. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

30 people in a boat, overloaded, 15 people needed to disembark. So people line up, and the position in the line is their number. Count, starting from 1, and those who count to 9 get off the boat. This cycle continues until there are only 15 people left on the boat. Which numbered people have disembarked?

$numberStart = 30; //开始的人数
$numberLive  = 15; //最终剩下的人数
$numKill     = 9;  //"中奖"号码
$peopleList  = [];
for($i=1; $i<=$numberStart; $i++){
	$peopleList[$i]=1; //1表示生 0表示死
}

$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.&#39;号下船了&#39;.PHP_EOL;
		$numberStart--;
	}
	$i++;
}
print_r($peopleList);
/*
9号下船了
18号下船了
27号下船了
6号下船了
16号下船了
26号下船了
7号下船了
19号下船了
30号下船了
12号下船了
24号下船了
8号下船了
22号下船了
5号下船了
23号下船了
*/
Copy after login

The above is the detailed content of PHP Joseph game code example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template