程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了生成数字组合大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决生成数字组合?

开发过程中遇到生成数字组合的问题如何解决?下面主要结合日常开发的经验,给出你关于生成数字组合的解决方法建议,希望对你解决生成数字组合有所启发或帮助;

这是一个递归解决方案:

$nbRank = 3;
$nbNumeric = 2;

function getCombinations ($length, $min, $max, $aStarTingCombinations)
{
    if ($length == 1)
    {
        return range ($min, $maX);
    }

    $final = array ();
    foreach (getCombinations ($length - 1, $min, $max, $aStarTingCombinations) as $combination)
    {
        for ($i = $min; $i <= $max; $i++)
        {
            $final [] = $combination . $i;
        }
    }
    return $final;
}

print_r (getCombinations ($nbRank, 0, $nbNumeric, array ()));

解决方法

我有一个包含两个变量的php页面:$nbRank$nbNumeric。根据这两个变量,我想生成一个包含所有现有组合的数组。例如:

如果$nbRank = 3$nbNumeric = 2我有:

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2

因此,我创建了不同的循环和公式来获得最终结果,但是它不起作用。这就是我所做的:

$result = array();

$nbIntRank = 0;
$nbIntNumeric = 0;
$nbRank = array();
$nbNumeric = array();

$nb_rangs = 3;
$nb_chiffres = 2;

for ($i = 1; $i <= $nb_rangs; $i++){
    $nbRank[$i] = 0;
}

$nbIntRank = count($nbRank);

for ($i = 0; $i <= $nb_chiffres; $i++){
    $nbNumeric[$i] = $i;
}

$nbIntNumeric = count($nbNumeric);

$algo = ($nb_rangs * ($nb_chiffres + 1)) * ($nb_rangs * ($nb_chiffres + 1));
$nbLine = $algo / ($nb_rangs);

$occ = 0;
for ($i = 0; $i < $nbLine; $i++){
    foreach ($nbRank as $nbrItem => $nbrvalue){
        $result[$i][] = $nbrValue;
        $occ++;
    }
}

echo '#############<br />';
echo '### DATAS ###<br />';
echo '#############<br /><br />';

echo '- Nb Elements : '.$algo.'<br />';
echo '- Nb Lines : '.$nbLine.'<br />';
echo '- Nb Valuable Occurency : '.$occ.'<br />';

echo '<br /><hr /><br />';
echo '##############<br />';
echo '### PARSER ###<br />';
echo '##############<br /><br />';

echo '<pre>';
var_dump($result);
echo '</pre>';

我设法用空值(81个值,在27行3个元素中)创建了最终数组,但其中只包含0。

大佬总结

以上是大佬教程为你收集整理的生成数字组合全部内容,希望文章能够帮你解决生成数字组合所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。