我会尝试做这样的事情:(你也必须添加三元组,但这就是我要开始的方式......)你必须有很多可用的内存来使用数组......它吃掉了很多。我尝试了 170 并且 php 已经筋疲力尽了......内存限制。你应该意识到这一点。如果您的服务器允许,这不是真正的问题。
<?php
$score = 50;
if ($score >170 || $score <=1 ) {
$message = 'No outs possible';
} else {
$outs = array();
//Create an array with values from 0 to {value of $score}
for($v=0;$v<$score;$v++) {
$outs[] = $v;
}
// Get combinations of values (like 0 0 1, 0 0 2, 0 0 3 etc with space between values)
$combinations = getCombinations($outs,3);
//Create an array when combinations are equal to set score
$ascArr = array();
$possibleOuts = array();
foreach($combinations as $key=>$c) {
$tmpArr = explode(' ', $c); //Create array of each combination (value1, value2, value3)
//Value3 has to be a double
$tmpArr[2] *= 2;
//Get ascii-value of whole string (without spaces)
$tc = trim($c);
$ascValue = getAsciiValueOfString($tc);
//If sum of array-values is the same as score, add it to outs array
//Also make sure that ascii-value of string occurence cannot be repeated ('0 1 49' or '0 49 1' is actually same)
if (array_sum($tmpArr) == $score && !in_array($ascValue, $ascArr)) {
$possibleOuts[] = $c . ' double';
$ascArr[] = $ascValue;
}
}
//Could some more part of outs array (besides last) be a double?
$doubleOuts = array();
$do = array();
foreach($possibleOuts as $value) {
$tmpArr = explode(' ', $value);
$do[0] = $tmpArr[0];
$do[1] = $tmpArr[1];
$do[2] = $tmpArr[2];
$do[3] = $tmpArr[3];
//Check first value. If first value can be divided into 2 and the second value as well
//then set double on first value, and reduce second value
if ($tmpArr[0] % 2 === 0 && $tmpArr[0]>0 && $tmpArr[1] % 2 === 0) {
$do[0] = $tmpArr[0] . ' double';
$do[1] = $tmpArr[1] - $tmpArr[0];
}
$doubleOuts[] = implode(' ', $do);
}
//Merge outs and doubleouts-array
$posOuts = array_merge($possibleOuts, $doubleOuts);
// Print it out
foreach($posOuts as $value) {
echo $value . ' <br />';
}
}
//Function for getting value of chars
function getAsciiValueOfString($string)
{
$ascii = 0;
for ($i = 0; $i < strlen($string); $i++)
{
$ascii += ord($string[$i]);
}
return($ascii);
}
//Recursive functions for getting differet combinations
function getCombinations($arr,$n)
{
$res = array();
foreach ($arr as $w)
{
if ($n==1) $res[] = $w;
else
{
$perms = getCombinations($arr,$n-1);
foreach ($perms as $p)
{
$res[] = $w." ".$p;
}
}
}
return $res;
}
?>
输出:
0 0 25 double
0 2 24 double
0 4 23 double
0 6 22 double
0 8 21 double
0 10 20 double
0 12 19 double
0 14 18 double
0 16 17 double
0 18 16 double
0 20 15 double
0 22 14 double
0 24 13 double
0 26 12 double
0 28 11 double
0 30 10 double
0 32 9 double
0 34 8 double
0 36 7 double
0 38 6 double
0 46 2 double
0 48 1 double
1 19 15 double
1 39 5 double
3 9 19 double
3 19 14 double
3 29 9 double
3 39 4 double
5 19 13 double
5 29 8 double
5 39 3 double
7 19 12 double
7 29 7 double
7 39 2 double
9 19 11 double
9 29 6 double
10 10 15 double
10 12 14 double
10 14 13 double
10 16 12 double
10 18 11 double
10 20 10 double
10 40 0 double
11 19 10 double
13 19 9 double
15 19 8 double
17 19 7 double
19 19 6 double
19 29 1 double
0 0 25 double
0 2 24 double
0 4 23 double
0 6 22 double
0 8 21 double
0 10 20 double
0 12 19 double
0 14 18 double
0 16 17 double
0 18 16 double
0 20 15 double
0 22 14 double
0 24 13 double
0 26 12 double
0 28 11 double
0 30 10 double
0 32 9 double
0 34 8 double
0 36 7 double
0 38 6 double
0 46 2 double
0 48 1 double
1 19 15 double
1 39 5 double
3 9 19 double
3 19 14 double
3 29 9 double
3 39 4 double
5 19 13 double
5 29 8 double
5 39 3 double
7 19 12 double
7 29 7 double
7 39 2 double
9 19 11 double
9 29 6 double
10 double 0 15 double
10 double 2 14 double
10 double 4 13 double
10 double 6 12 double
10 double 8 11 double
10 double 10 10 double
10 double 30 0 double
11 19 10 double
13 19 9 double
15 19 8 double
17 19 7 double
19 19 6 double
19 29 1 double