Home > php教程 > php手册 > 枪毙猴子,也叫约瑟夫环。暴力解决。

枪毙猴子,也叫约瑟夫环。暴力解决。

WBOY
Release: 2016-06-06 19:33:19
Original
1831 people have browsed it

猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了 PHP ?phpfunction killMonkeys($monkeys, $m){ $k = $m; while (count($monkeys)){ $k = $k - 1; $monkey = array_shift($monkeys); if ($k) { $monkeys[] = $

猴子一群,都带着号码的,站好了一圈,数到m的枪毙,剩下的接着数。如此往复,死剩下的一个就疯了 PHP
<?php
function killMonkeys($monkeys, $m){
    $k = $m;
    while (count($monkeys)){
        $k = $k - 1;
        $monkey = array_shift($monkeys);
        if ($k) {
           $monkeys[] = $monkey;
        } else {
            echo "<font color='red'>" . $monkey . "</font>号猴子毙了!<br/>";
            $k = $m;
        }
                
    }
    echo $monkey . "号猴子斯巴达了";
}
$monkeys = range(0, 25);
unset($monkeys['0']);
$m = 5;
killMonkeys($monkeys, $m);
Copy after login
Related labels:
source:php.cn
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template