首页 > 解决方案 > 所有可能集合(3 位)的排列,包括零

问题描述

我正在创建一个小型游戏程序,其中有两种类型的游戏。SingleDouble。每个游戏都有一个固定的图表集。

单图

        '128',   '129',  '120',  '130',  '140',   '123',  '124',  '125',  '126',  '127',
        '137',   '138',  '139',  '149',  '159',   '150',  '160',  '134',  '135',  '136',
        '146',   '147',  '148',  '158',  '168',   '169',  '179',  '170',  '180',  '145',
        '236',   '156',  '157',  '167',  '230',   '178',  '250',  '189',  '234',  '190',
        '245',   '237',  '238',  '239',  '249',   '240',  '269',  '260',  '270',  '235',
        '290',   '246',  '247',  '248',  '258',   '259',  '278',  '279',  '289',  '280',
        '380',   '345',  '256',  '257',  '267',   '268',  '340',  '350',  '360',  '370',
        '470',   '390',  '346',  '347',  '348',   '358',  '359',  '369',  '379',  '389',
        '489',   '480',  '490',  '356',  '357',   '349',  '368',  '378',  '450',  '460',
        '579',   '570',  '580',  '590',  '456',   '367',  '458',  '459',  '469',  '479',
        '560',   '589',  '670',  '680',  '690',   '457',  '467',  '468',  '478',  '569',
        '678',   '679',  '689',  '789',  '780',   '790',  '890',  '567',  '568',  '578',

双图

        '100',   '110',  '166',  '112',  '113',   '114',  '115',  '116',  '117',  '118',
        '119',   '200',  '229',  '220',  '122',   '277',  '133',  '224',  '144',  '226',
        '155',   '228',  '300',  '266',  '177',   '330',  '188',  '233',  '199',  '244',
        '227',   '255',  '337',  '338',  '339',   '448',  '223',  '288',  '225',  '299',
        '335',   '336',  '355',  '400',  '366',   '466',  '377',  '440',  '388',  '334',
        '344',   '499',  '445',  '446',  '447',   '556',  '449',  '477',  '559',  '488',
        '399',   '660',  '599',  '455',  '500',   '600',  '557',  '558',  '577',  '550',
        '588',   '688',  '779',  '699',  '799',   '880',  '566',  '800',  '667',  '668',
        '669',   '778',  '788',  '770',  '889',   '899',  '700',  '990',  '900',  '677',

当我选择一个Single并输入数字0123456789 (any sequence(sorted & unsorted), min 4 digits & max 10 digits from 0-9, repeated)时,它会返回与 相关的整个集合Single Chart。对于这种情况,它返回120集合。

如果我选择double并输入相同的数字0123456789,它将返回与Double ChartONLY 相关的所有集合。

目前我正在使用一种Single游戏解决方案,这里给出了permutations-all-possible-sets-of-numbers但在某些集合中它会返回,001但它必须是100

我也找不到Double游戏的解决方案。我尝试什么:

 public function insertSingleGameData($motorGameData)
{

    $chartvalidation    = config('chartValidation');
    $tempMotorSet       = str_split($motorGameData->Playgame->cane);
    $motorSet           = $this->arrayCombination(3, $tempMotorSet); // Get All Sets
    $motorRange         = array_unique($motorSet);
        foreach($motorRange as $cane)
        {

            if(in_array($cane, $chartvalidation['single'])){
                $tempGameData[]     =   ([
                    'playgame_id'   =>  $motorGameData->Playgame->id,
                    'user_id'       =>  $motorGameData->Playgame->user_id,
                    'cane'          =>  $cane,
                    'amount'        =>  $motorGameData->Playgame->amount,
                    'gamemaster_id' =>  $motorGameData->Playgame->gamemaster_id,
                    "created_at"    =>  \Carbon\Carbon::now(),  
                    "updated_at"    =>  \Carbon\Carbon::now(),  
                ]);
            }
        }
        Tempgame::insert($tempGameData);

}

function arrayCombination($le, $set){
    $lk = $this->combination_number($le, count($set));
    $ret = array_fill(0, $lk, array_fill(0, $le, '') );
    $temp = array();
    for ($i = 0 ; $i < $le ; $i++)
        $temp[$i] = $i;
        $ret[0] = $temp;
        for ($i = 1 ; $i < $lk ; $i++){
            if ($temp[$le-1] != count($set)-1){
                $temp[$le-1]++;
            } else {
                $od = -1;
                for ($j = $le-2 ; $j >= 0 ; $j--)
                    if ($temp[$j]+1 != $temp[$j+1]){
                        $od = $j;
                        break;
                    }
                if ($od == -1){
                    break;
                }
                $temp[$od]++;
                for ($j = $od+1 ; $j < $le ; $j++)    {
                    $temp[$j] = $temp[$od]+$j-$od;
                }
            }
            $ret[$i] = $temp;
        }
        for ($i = 0 ; $i < $lk ; $i++) {
            for ($j = 0 ; $j < $le ; $j++){
                $ret[$i][$j] = $set[$ret[$i][$j]];   
            }

        }
    $tempSet = array();
    foreach ($ret as $key => $value) {
        $tempSet[] = implode('',  $value);
    }
    return $tempSet;
    //print("<pre>".print_r($ret,true)."</pre>");
}

 function combination_number($k,$n){
    $n = intval($n);
    $k = intval($k);
    if ($k > $n){
        return 0;
    } elseif ($n == $k) {
        return 1;
    } else {
        if ($k >= $n - $k){
            $l = $k+1;
            for ($i = $l+1 ; $i <= $n ; $i++)
                $l *= $i;
            $m = 1;
            for ($i = 2 ; $i <= $n-$k ; $i++)
                $m *= $i;
        } else {
            $l = ($n-$k) + 1;
            for ($i = $l+1 ; $i <= $n ; $i++)
                $l *= $i;
            $m = 1;
            for ($i = 2 ; $i <= $k ; $i++)
                $m *= $i;            
        }
    }
    return $l/$m;
}

我怎样才能在一个功能中同时实现这一点Single和游戏?Double

标签: phppermutation

解决方案


您可以preg_match通过创建正则表达式来执行数字,例如:

/^[235046]+$/

这意味着我们正在尝试匹配从开始(插入符号)到结束(美元符号)具有数字235046(含义235等)的字符串。如果我们找到匹配项,我们将它们收集到另一个数组中。^$

片段:

<?php

$digits = '235046';

$single_chart_filtered = [];

foreach($single_chart as $chart_value){
    if(preg_match("/^[$digits]+$/",$chart_value) === 1){
        $single_chart_filtered[] = $chart_value;
    }
}

print_r($single_chart_filtered);

$double_chart_filtered = [];

foreach($double_chart as $chart_value){
    if(preg_match("/^[$digits]+$/",$chart_value) === 1){
        $double_chart_filtered[] = $chart_value;
    }
}

演示: https ://3v4l.org/jChvm


推荐阅读