簡體   English   中英

我該如何計算出一個列中每個值中需要多少才能在excel中累加一個總值?

[英]How do I work out how many of each value in a column i would need to add up to a total value in excel?

所以說

A1-A8包含值11、120、321、435、623、765、964、1039,B1包含值1375如何在Excel中使用公式或宏來計算需要多少才能獲得at的值至少B1

因此在此示例中:A8 + A3 + A2或A8 + A3 + A1 + A1

有什么辦法嗎?

抱歉,再舉一個例子,讓它更清楚一點。

我有一輛想要以35,000美元購買的汽車,有10、20、100、1,000和10,000美元的面額的紙幣,我需要使用最少數量的紙幣來組合這些紙幣的總價值。

快速瀏覽一下會說3 x $ 10,000和5 x $ 1,000是完全正確的,而4 x $ 10,000是最少數目的鈔票。 我希望能夠輸入任何值並獲得最少數量的賬單或完全匹配,但是我認為最少數量的賬單是最好的。

韋斯

好的,所以我整理了一些效率低下的代碼來管理此問題(在PHP中,因為這是最簡單的測試環境)。

    $w=array();
    $a=7000;
    $a1=$a;
    $b=array(10,100,500,750);/*9 x 750 + 2 x 100 + 10 x 5*/
    $t=$b;
    $r=0;
    $z=0;

    while ($r==0){
        $z++;
        $num1=0;
        $num2=0;
        if (count($t)>0){
            $c=max($t);
            array_pop ($t); 
        }else{
            $r=1;
        }
        $i=0;
        $d=0;
        $q=0;
        $e=0;
        $j=0;
        while ($q==0){
            if ($c>$a1){
                $num2=$a1;
                $q=1;
            }else{
                $num1=$a1 / $c; 
                $num2=$a1 % $c;
                $q=2;
            }
        }   
        if (($a1>$c) and ($num2==0)){
            //echo "f<br>";//$r=1;
            $w[$z][0]=intval($num1);
            $w[$z][1]=$num2;
            $w[$z][2]=$q;
            $w[$z][3]=$c;
            $r=1;
        }else{
            $w[$z][0]=intval($num1);
            $w[$z][1]=$num2;
            $w[$z][2]=$q;
            $w[$z][3]=$c;
            $a1=$num2;
        }
    }


    foreach ($w as $ww){
        echo $ww[0]."|".$ww[3]."<br>";
    }

所以這會打印

9 | 750

0 | 500

2 | 100

5 | 10

第一個數字是數組值進入定義值的次數。 第二個數字是它是哪個數組值。

因此7000 =(9 x 750)+(2 x 100)+(5 x 10);

花了點時間想出需要的東西。 歡迎提出任何使代碼更高效的建議!

我將其作為答案,因為注釋框太小,無法包含此說明。

您確定要獲取比B1大的數字嗎? 在這種情況下,您只需要Nx = roundup(B1/Ax) ,其中x是從1到8的任何數字。
如果您有興趣以最少的計算量(最小化Nx )獲得該數字,則需要設置x=8 (假設數字A1 - A8以升序排列)。
但是,如果您對B1上方最接近B1的總和感興趣(根據您的答案似乎正在尋找該總和),那么Cyber​​netic Nomad提供的鏈接可能會有所幫助。

換句話說:請澄清您的問題。

再次審視它並不能達到我滿意的水平,尤其是我也想對它進行加權,因此這是另一個選擇:

$w=array();
$dividend=455000000;
$remainder=$dividend;
$b=array(4100000,10000000,50000000,101000000);/*9 x 750 + 2 x 100 + 10 x 5*/
$b1=array(5,4.8,3.9,3.2);
$t=$b;
$r=0;
$y=0;
$z=count($t);
while ($z>0){
    $z--;
    $quotient=0;
    $remainder2=0;
    $divisor=$t[$z];
    $i=0;
    $d=0;
    $q=0;
    $e=0;
    $j=0;
    while ($q==0){
        if ($divisor>$remainder){
            $remainder2=$remainder;
            $q=1;
        }else{
            $quotient=$remainder / $divisor; 
            $remainder2=$remainder % $divisor;
            $q=2;
        }
    }   
    if ($remainder<$divisor){
        $w[$y][0]=intval($quotient);
        $w[$y][1]=$remainder2;
        $w[$y][2]=$q;
        $w[$y][3]=$divisor;
        $remainder=$remainder2;
    }else{
        if (($quotient>1) and ($z<count($t)-1)){
            $w[$y-1][0]++;
            $z=0;
            unset($w[$y]);
            $remainder2=0;
        }else{
            $w[$y][0]=intval($quotient);
            $w[$y][1]=$remainder2;
            $w[$y][2]=$q;
            $w[$y][3]=$divisor;
            $remainder=$remainder2;
        }

    }
    $y++;
}

if ($remainder2>0){
    $w[$y-1][0]++;
}


 $total=0;
 foreach ($w as $ww){
    echo $ww[0]."|".$ww[3]."<br>";
    $total+=$ww[0] * $ww[3];
}

echo $total."<br>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM