我必须从给定的整数序列中找到所有可能的解决方案,它等于给定的数字.
例如:1,2,3,4,5,6,7,8,9等于100回答:1 + 23 - 4 + 56 + 7 + 8 + 9 = 100
我的解决方案是示例25中的另一个总和:
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = count($arr);
$sum = 25;
function checkSol() {
global $arr;
global $n;
global $sum;
$tempSum = 0;
for ($i = 0; $i < $n; $i++) {
$tempSum += $arr[$i];
}
if ($tempSum == $sum) {
for ($i = 0; $i < $n; $i++) {
if ($arr[$i] > 0) {
printf("+%d ", …Run Code Online (Sandbox Code Playgroud)