如何在PHP中生成具有3位数字且每个数字中没有双数字的唯一数字

我想在 120 对中生成 3 位数字 没有重复和重复的数字 示例:012013、132 有效 011,333、022,202210无效 注意:冗余数字都出现因此012无效210

我正在使用 PHP 版本 5.6.28 Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28 这是我迄今为止尝试过的,但没有运气。

数计数:120 027 028 029 036 038 046 069 074 079 093 105 106 109 123 143 145 149 152 153 162 167 175 182 195 198 204 208 213 216 217 219 235 243 275 287 294 301 302 310 327 341 342 347 352 357 358 365 369 376 378 380 384 392 402 408 415 423 428 431 453 465 467 468 490 493 496 506 509 510 512 524 537541 561 573591 597 598 609 629 631 634 639 647 689 691 694 697 703 719 723 743 745 750 752 759 761 813 819 820 824 829 840 865 875 890 905 907 912 916 917 921 930 941 945 947 963 965 973 984

注意这里537573无效的

/*----Numbers.php----*/


class Numbers{

    private $num_set = array();//get 3 digit

    private $num_basket = array(); //container

    public $codeNum = "0123456789";



    public function get_basket(){

        return $this->num_basket;

    }

    public function put_basket($num){

        $this->num_basket[] = $num;

    }


    public function is_exist($num_taken){

        if(in_array($num_taken, $this->num_basket)){

            return true;

        }else{

            return false;

        }        

    }


    public function generate_num(){

        while(count($this->num_set) < 3){

            $get_one_digit = $this->getToken(1);

            if(!in_array($get_one_digit, $this->num_set)){

                $this->num_set[] = $get_one_digit;


            }

        }

        $three_digit = implode($this->num_set);

        $this->num_set = array();

        return $three_digit;

    }


    protected function getToken($length)

    {

        $token = "";


        $max = strlen($this->codeNum); // edited


        for ($i=0; $i < $length; $i++) {

            $token .= $this->codeNum[$this->crypto_rand_secure(0, $max-1)];

        }   

        return $token;

    }

}


代码工作正常,不显示错误,但不显示预期结果。我真的需要帮助。我将不胜感激任何帮助。


人到中年有点甜
浏览 165回答 1
1回答

宝慕林4294392

我重写了代码,因为我认为类和类之外的代码组合太多,这会产生很多依赖(恕我直言)。在这段代码中,您创建一个类并说出您想要生成多少个数字,然后调用该generate()方法为您完成所有工作。为了检查之前是否使用过这些数字,我将数字编码到一个位字段并存储它(该encode()方法只是根据使用的数字添加二进制数字)。如果同样的模式再次发生,它只会尝试另一个模式。class NumberGenerator{&nbsp; &nbsp; private $numbersUsed = [];&nbsp; &nbsp; private $basket = [];&nbsp; &nbsp; private $numbersToGenerate = 0;&nbsp; &nbsp; public function __construct( int $numbersToGenerate )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; $this->numbersToGenerate = $numbersToGenerate;&nbsp; &nbsp; }&nbsp; &nbsp; public function generate(){&nbsp; &nbsp; &nbsp; &nbsp; $numbers = range(0,9);&nbsp; &nbsp; &nbsp; &nbsp; for ( $i = 0; $i < $this->numbersToGenerate; $i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shuffle($numbers);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $number = array_slice($numbers, 0,3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $encoded = $this->encode($number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( in_array($encoded, $this->numbersUsed) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->basket[] = implode($number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->numbersUsed[] = $encoded;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->basket;&nbsp; &nbsp; }&nbsp; &nbsp; protected&nbsp; function encode(array $num)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $numbers = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];&nbsp; &nbsp; &nbsp; &nbsp; $sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $num as $digit )&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sum += $numbers[$digit];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $sum;&nbsp; &nbsp; }}$generator = new NumberGenerator(120);print_r($generator->generate());
打开App,查看更多内容
随时随地看视频慕课网APP