search
HomeDaily ProgrammingPHP KnowledgePHP random selection algorithm (3)

In the previous article "PHP Random Picking One Algorithm (2)", we introduced in detail the implementation idea of ​​PHP Random Picking One Algorithm, which is the "Fair Selection of Monkey King" interview question solution.

PHP random selection algorithm (3)

# Now we will combine the code methods in the above articles to show you the process of debugging and running the algorithm through Xdebug.

The question is as follows:

A group of monkeys line up in a circle and are numbered according to 1, 2,...,n. Then start counting from the 1st one, count to the mth one, kick it out of the circle, start counting from behind it, count to the mth one, kick it out..., and continue in this way until the end. Until there is only one monkey left, that monkey is called the king. Programming is required to simulate this process, input m, n, and output the number of the last king.

The code is as follows:

<?php

function king($n, $m){
    $monkeys = range(1, $n);         //创建1到n数组
    $i=0;
    while (count($monkeys)>1) {     //循环条件为猴子数量大于1
        if(($i+1)%$m==0) {     //$i为数组下标;$i+1为猴子标号
            unset($monkeys[$i]);  //余数等于0表示正好第m个,删除,用unset删除保持下标关系
        } else {
            array_push($monkeys,$monkeys[$i]);     //如果余数不等于0,则把数组下标为$i的放最后,形成一个圆形结构
            unset($monkeys[$i]);
        }
        $i++;//$i 循环+1,不断把猴子删除,或 push到数组
    }
    return current($monkeys);  //猴子数量等于1时输出猴子标号,得出猴王
}

echo king(10,3);

First we create a breakpoint before the fourth line of code.

PHP random selection algorithm (3)

#Then open the browser and run this code. The breakpoint successfully obtains focus, as follows.

PHP random selection algorithm (3)

#Create an array from 1 to n.

PHP random selection algorithm (3)

Then the while loop determines whether to delete the element.

PHP random selection algorithm (3)

This cycle leads to the "Monkey King".

Related recommendations: "How to configure and use the xdebug tool in PHPStorm? (Picture, text and video tutorial)

So that’s it for the introduction of PHP’s random selection algorithm. You can also test it locally. It's actually very simple. I hope it will be helpful to friends who need it!

The above is the detailed content of PHP random selection algorithm (3). For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.