Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

php - Generate a combination of numbers

I have a PHP page with two variables: $nbRank and $nbNumeric. Depending of these two variables, I want to generate an array containing all combinations existing. For example:

If $nbRank = 3 and $nbNumeric = 2 I would have:

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

So, I create different loop and formulas to get the final result, but it doesn't works. This is what I did :

$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>';

I managed to create my final array with empty values (81 values, in 27 lines of 3 elements) but it only contains 0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You indicated you'll be fine with pseudo-code.. Sorry I cannot offer specific correction for your php code [if these answers appear - they might be more educating], but I'd chose a recursive solution for this problem.

In each level of the recursion, try all possibilities, and call the same function to find all combinations of one smaller size.

Pseudo-Code:

findCombinations(range,size,sol,resultList):
  if (size ==0): #base clause
     resultList.append(copy(sol)) #making a copy of sol and appending it as a solution
     return
  for each i in (0,range):
     sol.append(i)
     findCombinations(range,size-1,sol,resultList) #recursive invokation, with smaller size
     sol.deleteLast() #clean up environment before next calls

Invoke with findCombinations(3,3,[],resultList) where [] is just empty list, and resultList will hold the list of combination when the algorithm is done. this invokation will get all combinations of size 3 with elemenets 0,1,2.

Complexity note: The number of possibilities is growing exponentially [O(rangesize)], so if you try to invoke it with 20,20 for instance - it might take some [very long] time, for any solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...