search
Homephp教程php手册php解决约瑟夫环示例

这篇文章主要介绍了php解决约瑟夫环示例,需要的朋友可以参考下

约瑟夫问题(有时也称为约瑟夫斯置换,,是一个出现在计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。又称“丢手绢问题”.)

猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了

复制代码 代码如下:


function killMonkeys($monkeys, $m){
    $k = $m;
    while (count($monkeys)){
        $k = $k - 1;
        $monkey = array_shift($monkeys);
        if ($k) {
           $monkeys[] = $monkey;
        } else {
            echo "" . $monkey . "号猴子毙了!
";
            $k = $m;
        }

    }
    echo $monkey . "号猴子斯巴达了";
}
$monkeys = range(0, 25);
unset($monkeys['0']);
$m = 5;
killMonkeys($monkeys, $m);

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools