Php 从数组中计算与给定数字相等或更高且最接近的数字
我需要从给定的数组中计算出与PHP中给定的数字相等或更高且最接近的数字。例如: 要获取的编号:Php 从数组中计算与给定数字相等或更高且最接近的数字,php,Php,我需要从给定的数组中计算出与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.
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);
}
}
}
$arr=array(3.1135,4.3835,4.0461,3.9941,2.86135817,0.5);
$fetch=6.85505196;
$bestsum=获取费用($arr,$fetch);
打印($bestsum);
函数获取费用($arr,$fetch){
$bestsum=9999999;
$combo=array();
$result=array();
对于($i=0;$i$arr){
$sum=0;
foreach($arr作为$value){
$result[$idx]+=$value;
}
如果($result[$idx]>=$fetch&&$result[$idx]<$bestsum)$bestsum=$result[$idx];
}
返回$bestsum;
}
函数组合($arr,$level,&$combo,$curr=array()){
对于($j=0;$j
新更新的代码
<?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;
}
}
?>
新更新的代码
<?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;
}
}
?>
我希望下面的示例可以帮助您。请试试这个
<?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/>";
}
?>
我希望下面的例子可以帮助您。请试试这个
<?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/>";
}
?>
您应该分两步进行: a。制定(或查找)一个算法来完成这项工作 b。实施它 你没有说你在这三个小时里做了什么,所以这里有一个“蛮力”(读:dumb)算法可以完成这项工作:
$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);
$bestsum
更好时,将$bestsum
设置为它。还将第二个变量$summands
设置为用于获取此结果的数字数组。(否则,您将不知道如何获得解决方案)。只要找到更好的解决方案,就更新这两个变量就这些。它保证正常工作,因为它尝试了所有的可能性。有各种各样的细节需要填写,但如果遇到困难,您可以开始工作并在这里寻求特定任务的帮助。您应该分两步完成: a。制定(或查找)一个算法来完成这项工作 b。实施它 你没有说你在这三个小时里做了什么,所以这里有一个“蛮力”(读:dumb)算法可以完成这项工作:
$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);
$bestsum
更好时,将$bestsum
设置为它。还将第二个变量$summands
设置为用于获取此结果的数字数组。(否则,您将不知道如何获得解决方案)。只要找到更好的解决方案,就更新这两个变量就这些。它保证正常工作,因为它尝试了所有的可能性。有各种各样的细节需要填写,但是如果您遇到困难,您可以开始工作并在这里寻求特定任务的帮助。谢谢大家的帮助! 当只需要获取一个或两个数字(加法)时,我的代码工作得非常好。但是我不知道如何将更多的组合添加到给定数组中的元素总数。 我的意思是,如果我的数组中有8个数字,我想尝试所有可能的组合(彼此相加)。 我的实际代码是:
$bestsum=1000000;
对于($i=0;$i=$POWENT&$txinfo[“vout”][$i][“value”]<$bestsum){
$bestsum=$txinfo[“vout”][$i][“value”];
}
}
对于($i=0;$i=$spend&$sum<$bestsum){
$bestsum=$sum;
}
}
}
$fee=bcsub($bestsum,$POWED,8);
打印(费用:.$Fee);
谢谢大家的帮助!
当只需要获取一个或两个数字(加法)时,我的代码工作得非常好。但是我不知道如何将更多的组合添加到给定数组中的元素总数。
我的意思是,如果我的数组中有8个数字,我想尝试所有可能的组合(彼此相加)。
我的实际代码是:
$bestsum=1000000;
对于($i=0;$i=$POWENT&$txinfo[“vout”][$i][“value”]<$bestsum){
$bestsum=$txinfo[“vout”][$i][“value”];
}
}
对于($i=0;$i=$spend&$sum<$bestsum){
$bestsum=$sum;
}
}
}
$