search
Homephp教程php手册约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数

约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数

Jun 13, 2016 pm 12:13 PM
phpsviewuseinternalfunctionaccomplishpointeroperatearraydetailedthis

来看看这个问题的详细描述:
view sourceprint?一群猴子排成一圈,按 1,2,...,n 依次编号。然后从第 1 只开始数,数到第 m 只,把它踢出圈,从它后面再开始数, 再数到第 m 只,在把它踢出去...,如此不停的进行下去, 直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入 m、n, 输出最后那个大王的编号。
刚开始构思的时候想使用 PHP 数组来实现(当然最后还是使用的数组),然后模拟一个数组的内部指针,结果发现想模拟一个“数组指针”不是那么的容易,因为涉及到很多“指针”的操作,最后猛然想到,PHP 的数组本身就是有内部指针的,为什么还要去“造车轮子”呢?!于是乎~看代码:

复制代码 代码如下:


function getKingMonkey($n, $m)
{
$a = array();//声明内部数组
for($i = 1; $i {
$a[$i] = $i;//这一步是对号入座
}
reset($a);//为了严谨,我们来一个 reset() 函数,其实也可以省去
while(count($a) > 1)//主循环开始,这里使用的判别条件是数组元素的个数等于 1 的时候停止循环
{
for($counter = 1; $counter {
if(next($a)){//如果存在 next 元素
if($counter == $m)
{
unset($a[array_search(prev($a), $a)]);//当数到 m 时,使用 unset() 删除数组元素
}
}
else//如果不存在 next 元素
{
reset($a);//则数组的第一个元素充当 next 元素
if($counter == $m)
{
unset($a[array_search(end($a), $a)]);//当数到 m 时,使用 unset() 删除数组元素,注意这里是 end()
reset($a);//记得让数组内部指针“归位”
}
}
}
}
return current($a);
}


测试一下下:
echo "猴子大王的编号为:" . getKingMonkey(100, 17);
输出为:
view sourceprint?猴子大王的编号为:53
The End~
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.