2016-01-13 106 views
1

我需要從給定數組中計算與PHP中給定數字相等或更高且最接近的數字。例如:從數組中計算等於或大於給定數字的數字

號碼獲取:

6.85505196

陣列來計算:

3.11350000 
4.38350000 
4.04610000 
3.99410000 
2.86135817 
0.50000000 

唯一正確的組合應該是:

3.99410000 + 2.86135817 = 6.85545817 

有人可以幫助我嗎?已經過了3個小時我正在發瘋!

更新:我終於完成了我的代碼如下:

$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); 
     } 
    } 
} 
+2

請您發佈您迄今嘗試過的代碼。 MCVE會改善你的問題(沒有你自己的代碼,看起來你希望我們爲你編碼)。 –

+0

爲什麼只有3.99410000 + 2.86135817 = 6.85545817是正確的。我看到「與給定數字相等或更高」,因此上面的數組有很多合適的組合。 –

+0

因爲6.85545817是最接近的組合,因爲有更高的組合可能沒問題,但我需要最接近的組合。其實我可以編碼最接近的數字,但它仍然遠離我的需求。 – supermoney

回答

2

你應該做的兩個步驟:

一個。制定(或查找)算法來完成這項工作。

b。實施它。

你不說你已經在你對這個工作經過3小時管理什麼,所以這裏的「強力」(讀:啞)算法,將做的工作:

  1. 使用一個變量,將保持你迄今爲止最好的總和。它可以開始作爲零:

    $bestsum = 0; 
    
  2. 嘗試所有單號,然後兩個數字的所有款項,然後是三個數字的所有款項,等:每當你發現了許多符合您的標準比當前$bestsum更好,,設置爲$bestsum。還要將第二個變量$summands設置爲您用來獲得此結果的數字的數組。 (否則你不會知道你是如何得到解決方案的)。每當你找到更好的解決方案時,更新這兩個變量。

  3. 當您嘗試過每個數字組合時,您的兩個變量都包含最佳解決方案。打印出來。

就這樣。它保證正常工作,因爲它嘗試所有可能性。有各種各樣的細節可以填寫,但如果您遇到困難,您可以開始工作,並在此尋求有關特定任務的幫助。

+0

我已經添加了我的實際更新代碼,只是它錯過了做2個以上組合的部分。我想我必須做多個foreach,但不確定,你可以看看嗎?謝謝。 – supermoney

0

新更新的代碼。

<?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; 
    } 
} 

?> 
+0

感謝您的幫助,但我最近完成了它。你可以在上面看到我的代碼。 – supermoney

+0

你的腳本比我的腳本快,但有時不工作。例如:x = 506,數組是507,8,1。問題是由「1」引起的,爲什麼? – supermoney

+0

這是幹什麼的?爲什麼'$ iterations'設置爲這個值? '$ maxpossible'是什麼?我不明白爲什麼OP會接受它:當不清楚它的意圖時,不可能驗證代碼正常工作。請添加一個算法的解釋。 – alexis

3

我希望下面的例子可以幫助你。請試試這個

<?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 
+0

謝謝我已經更新並在您的計劃中添加了以上我的代碼,它完美地結合了兩個數字,但無法弄清楚如何計算更多的數字,如果我的數組有效超過2個數字。例如:10個數字和正確組合的數組由4個數字組成,依此類推。 – supermoney

0

謝謝大家的幫忙! 我的代碼工作很酷,當需要獲取一個或兩個數字(加法)只。但無法弄清楚如何添加更多的組合,直到給定數組中的元素總數。 我的意思是,如果有,我想說,我的陣列中的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); 
+0

請勿添加額外的循環;畢竟,你不知道你的數組有多久,所以你不知道你需要多少循環(並且寫8個嵌套循環是一個可怕的解決方案)。將它視爲一個單獨的問題,並編寫一個單獨的函數來解決它:如何獲得一組數字的[所有子集](http://stackoverflow.com/a/6092999/699305)?你如何獲得子集中所有數字的總和? – alexis

+0

但請不要使用答案來提供有關您的問題的更多信息 - 這違反了網站的規則。編輯您的問題以添加更多信息(但避免將整個答案放在問題中的誘惑)。 – alexis

+0

我很抱歉,但我不熟悉這個網站及其規則。無論如何,我發現這裏的解決方案 - > [在PHP中查找所有可能的數組元素的獨特組合](http://stackoverflow.com/questions/15624662/find-all-possible-unique-combinations-of-elements- of-an-an-array-in-php) – supermoney

相關問題