从数组中计算等于或更高并且最接近给定数字的数字


Calculate from an array the number that is equal or higher and closest to a given number

我需要从给定的数组中计算出一个等于或更高并且最接近PHP中给定数字的数字。示例:

要提取的号码:

6.85505196

要计算的数组:

3.11350000
4.38350000
4.04610000
3.99410000
2.86135817
0.50000000

唯一正确的组合应该是:

3.99410000 + 2.86135817 = 6.85545817

有人能帮我吗?已经三个小时了,我快疯了!

更新:我终于完成了如下代码:

$arr = array(3.1135, 4.3835, 4.0461, 3.9941, 2.86135817, 0.5);
$fetch = 6.85505196;
$bestsum = get_fee($arr, $fetch);
print($bestsum);
function get_fee($arr, $fetch) {
    $bestsum = 999999999;
    $combo = array();
    $result = array();
    for ($i = 0; $i<count($arr); $i++) {
        combinations($arr, $i+1, $combo);
    }
    foreach ($combo as $idx => $arr) {
        $sum = 0;
        foreach ($arr as $value) {
            $result[$idx] += $value;
        }
        if ($result[$idx] >= $fetch && $result[$idx] < $bestsum) $bestsum = $result[$idx];
    }
    return $bestsum;
}
function combinations($arr, $level, &$combo, $curr = array()) {
    for($j = 0; $j < count($arr); $j++) {
        $new = array_merge($curr, array($arr[$j]));
        if($level == 1) {
            sort($new);
            if (!in_array($new, $combo)) {
                $combo[] = $new;          
            }
        } else {
            combinations($arr, $level - 1, $combo, $new);
        }
    }
}

我希望下面的例子能对您有所帮助。请尝试这个

<?php
    $array = array(
            "3.11350000",
            "4.38350000",
            "4.04610000",
            "3.99410000",
            "2.86135817",
            "0.50000000"
            );
echo "<pre>";
print_r($array);// it will print your array
for($i=0; $i<count($array); $i++)
{
    $j=$i+1;
    for($j;$j<count($array); $j++)
        {
            $sum = $array[$i] + $array[$j];
            // echo $array[$i]. " + ".$array[$j]." = ".$sum."<br>"; //this will display all the combination of sum
            if($sum >= 6.85505196 && ($sum <= round(6.85505196)) )//change the condition according to your requirement
             {
              echo "The correct combinations are:<br/><br/>";
              echo "<b>". $array[$i]. " + ".$array[$j]." = ".$sum."<b>";
              echo "<br/>";
             }
        }
        echo "<br/>";
        }
    ?>

我们将得到如下结果

Array
 (
  [0] => 3.11350000
  [1] => 4.38350000
  [2] => 4.04610000
  [3] => 3.99410000
  [4] => 2.86135817
  [5] => 0.50000000
 )
The correct combinations are:
4.04610000 + 2.86135817 = 6.90745817
3.99410000 + 2.86135817 = 6.85545817

您应该分两步完成:

a。算出(或查找)一个算法来做这项工作。

b。实施它。

你不会说你在三个小时内完成了什么,所以这里有一个"蛮力"(读作:愚蠢)算法可以完成任务:

  1. 使用一个变量来保持你迄今为止的最佳总和。它可以从零开始:

    $bestsum = 0;
    
  2. 尝试所有单个数字,然后是两个数字的所有和,然后是三个数字的全部和,等等。每次您找到一个符合您的标准并且比当前$bestsum更好的数字时,请将$bestsum设置为它。还可以将第二个变量$summands设置为用于获得此结果的数字数组。(否则你将不知道你是如何得到解决方案的)。无论何时找到更好的解决方案,都要更新这两个变量。

  3. 当您尝试了每个数字组合时,您的两个变量包含最佳解决方案。打印出来。

仅此而已。它保证能正常工作,因为它尝试了所有的可能性。有各种各样的细节需要填写,但如果你遇到困难,你可以开始工作,并在这里寻求具体任务的帮助。

感谢大家的帮助!当只需要获取一个或两个数字(加法)时,我的代码工作得很酷。但我不知道如何将更多的组合添加到给定数组中的元素总数中。我的意思是,如果我的数组中有8个数字,我也想尝试所有可能的组合(相互相加)。我的实际代码是:

    $bestsum = 1000000;
    for ($i = 0; $i < count($txinfo["vout"]); $i++) {
        if ($txinfo["vout"][$i]["value"] >= $spent && $txinfo["vout"][$i]["value"] < $bestsum) {
            $bestsum = $txinfo["vout"][$i]["value"];
        }
    }
    for($i = 0; $i < count($txinfo["vout"]); $i++) {
        $j = $i + 1;
        for($j; $j < count($txinfo["vout"]); $j++) {
            $sum = $txinfo["vout"][$i]["value"] + $txinfo["vout"][$j]["value"];
            if($sum >= $spent && $sum < $bestsum) {
                $bestsum = $sum;
            }
        }
    }
    $fee = bcsub($bestsum, $spent, 8);
    print("Fee: ".$fee);

新更新的代码。

<?php
$x = 6.85505196;
$num = array(3.1135, 4.3835, 4.0461, 3.9941, 2.86135817, 0.5);
asort($num); //sort the array
$low = $num[0]; // lowest value in the array
$maxpossible = $x+$low; // this is the maximum possible answer, as we require the number that is equal or higher and closest to a given number 
$num = array_values($num);
$iterations = $x/$num[0]; // possible combinations loop, to equate to the sum of the given number using the lowest number
$sum=$num;
$newsum = $sum;
$k=count($num);
for($j=0; $j<=$iterations; $j++){   
    $l = count($sum);
    for($i=0; $i<$l; $i++){
        $genSum = $sum[$j]+$sum[$i];
        if($genSum <= $maxpossible){
            $newsum[$k] = $genSum;
            $k++;
        }
    }
    $newsum = array_unique($newsum);
    $newsum = array_values($newsum);
    $k = count($newsum);
    $sum = $newsum;
}
asort($newsum);
$newsum = array_values($newsum);
for($i=0; $i<count($newsum); $i++){
    if($x<=$newsum[$i]){
        echo "'nMaximum Possible Number = ".$newsum[$i];
        break;
    } 
}
?>