查找PHP中总和的最接近值的算法

r_a_f

我有一个total_sum变量(例如20.00)和一个数组,它并不完全等于变量的组合,total_sum但非常接近:(array = [array(8=>1.49), array(1=>8.1)]数组可以有更多值)索引是一个乘数键值:(8*1.49 + 1*8.1) = 20.02 != total_sum我需要找到将数组值提高到等于的算法total_sum数组的键不能更改,只能更改值。值仅需保留两位小数(价格/货币)

因此,此示例数组的结果将是:array(8*1.49 + 1*8.8)[8.1更改为8.8,所以total_sum现在= 20.00]

有人知道这样的问题,或者这个问题有名字吗?

Mujnoi Gyula Tamas

因此,在过去一个小时中,我对此颇有兴趣:D。这是结果。希望这是您所期望的。从评论中我了解到您只希望具有双精度值。该函数将循环遍历,直到匹配双精度值。注意:可能会有更好的方法,但这是我想到的第一个方法。

function correction( $total, $input = array(), &$correction = null, $index = 0 ){
    if( count( $input ) < $index ){
        return;
    }
    $total_sum = 0;
    foreach( $input as $multiplier => $value ){
        $total_sum += $multiplier * $value;
    }
    $remains = 0;
    $i = 0;
    foreach( $input as $multiplier => $value ){
        if( $i !== $index ){
            $remains += $multiplier * $value;
        }
        $i++;
    }
    $rest = $total - $remains;
    reset( $input );
    $current_key = 0;
    for( $i = 0; $i < $index; $i++ ){
        next( $input );
    }
    $current_key = key( $input );
    if( $current_key !== null ){
        $value = $rest / $current_key;
        $precision = strlen( $value ) - strpos( $value, '.' ) - 1;
        if( $precision > 2 ){
            $index++;
            correction( $total, $input, $correction, $index );
        } else {
            $correction = array(
                'index' => $current_key,
                'value' => $value,
            );
        }
    }
}

一些样本数据:

$total = 20;
$input = array(
    8 => 1.49,
    1 => 8.1,
);
correction( $total, $input, $correction );
echo '<pre>'; print_r( $correction ); echo '</pre>';

结果:

Array
(
    [index] => 1
    [value] => 8.08
)

另一个示例:

$total = 20;
$input = array(
    8 => 1.49,
    1 => 8.1,
    3 => 2.1,
);

结果:

Array
(
    [index] => 1
    [value] => 1.78
)

这:

public static function correction( $total, $input = array(), &$correction = null, $index = 0 ){
    if( count( $input ) < $index ){
        return;
    }
    $total_sum = 0;
    foreach( $input as $data ){
        // if input is coming from user then
        // you may want to check if indexes are set
        $total_sum += $data['multiplier'] * $data['value'];
    }
    $remains = 0;
    $i = 0;
    foreach( $input as $data ){
        if( $i !== $index ){
            // same check here
            $remains += $data['multiplier'] * $data['value'];
        }
        $i++;
    }
    $rest = $total - $remains;
    $value = isset( $input[ $index ]['multiplier'] ) && $input[ $index ]['multiplier'] > 0 ?
                $rest / $input[ $index ]['multiplier'] : 0;
    $precision = strlen( $value ) - strpos( $value, '.' ) - 1;
    if( $precision > 2 ){
        $index++;
        self::correction( $total, $input, $correction, $index );
    } else {
        $correction = array(
            'index' => $index,
            'value' => $value,
        );
    }
}
$total = 68;
$input = array(
    array(
        'multiplier' => 1,
        'value'      => 1.2,
    ),
    array(
        'multiplier' => 8,
        'value'      => 5,
    ),
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从行内的 div 中查找最接近的值

来自分类Dev

在字典C#中查找与给定值最接近的值

来自分类Dev

优化在值列表中查找最接近的值

来自分类Dev

从我的值中查找最接近的列值

来自分类Dev

在数组中查找最接近的值使用linq?

来自分类Dev

在字典键列表中查找最接近的值Python

来自分类Dev

ios NSPredicate在NSDictionary中查找最接近的值

来自分类Dev

AngularJS:在多值对象中查找最接近的值

来自分类Dev

在矩阵中按列查找与参考最接近的值

来自分类Dev

在Pandas DataFrame的不同列中查找最接近的先前值

来自分类Dev

在MYSQL表列中查找最接近的匹配值

来自分类Dev

使用linq在C#的列表中查找最接近的值?

来自分类Dev

AngularJS:在多值对象中查找最接近的值

来自分类Dev

PHP-优化查找数组中的最接近点

来自分类Dev

如果算法未使用堆栈找到具有确切总和的子集,则查找与目标值最接近的子集

来自分类Dev

如果算法未使用堆栈找到具有确切总和的子集,则查找与目标值最接近的子集

来自分类Dev

在数字列表中查找数字对最接近的匹配项的算法

来自分类Dev

查找最接近的输入和设置值

来自分类Dev

查找最接近S中值的值

来自分类Dev

使用最接近的函数查找属性值

来自分类Dev

查找最接近给定值的索引

来自分类Dev

与最接近的日期相对应的查找值

来自分类Dev

Excel 在列中查找与给定查找值最接近的较大值

来自分类Dev

查找与给定数字最接近的数字总和

来自分类Dev

查找最接近特定数字的数字总和

来自分类Dev

近似最接近对算法

来自分类Dev

查找满足特定条件的最接近整数值的算法

来自分类Dev

查找满足特定条件的最接近整数值的算法

来自分类Dev

在Java集合中查找最接近的对象

Related 相关文章

  1. 1

    从行内的 div 中查找最接近的值

  2. 2

    在字典C#中查找与给定值最接近的值

  3. 3

    优化在值列表中查找最接近的值

  4. 4

    从我的值中查找最接近的列值

  5. 5

    在数组中查找最接近的值使用linq?

  6. 6

    在字典键列表中查找最接近的值Python

  7. 7

    ios NSPredicate在NSDictionary中查找最接近的值

  8. 8

    AngularJS:在多值对象中查找最接近的值

  9. 9

    在矩阵中按列查找与参考最接近的值

  10. 10

    在Pandas DataFrame的不同列中查找最接近的先前值

  11. 11

    在MYSQL表列中查找最接近的匹配值

  12. 12

    使用linq在C#的列表中查找最接近的值?

  13. 13

    AngularJS:在多值对象中查找最接近的值

  14. 14

    PHP-优化查找数组中的最接近点

  15. 15

    如果算法未使用堆栈找到具有确切总和的子集,则查找与目标值最接近的子集

  16. 16

    如果算法未使用堆栈找到具有确切总和的子集,则查找与目标值最接近的子集

  17. 17

    在数字列表中查找数字对最接近的匹配项的算法

  18. 18

    查找最接近的输入和设置值

  19. 19

    查找最接近S中值的值

  20. 20

    使用最接近的函数查找属性值

  21. 21

    查找最接近给定值的索引

  22. 22

    与最接近的日期相对应的查找值

  23. 23

    Excel 在列中查找与给定查找值最接近的较大值

  24. 24

    查找与给定数字最接近的数字总和

  25. 25

    查找最接近特定数字的数字总和

  26. 26

    近似最接近对算法

  27. 27

    查找满足特定条件的最接近整数值的算法

  28. 28

    查找满足特定条件的最接近整数值的算法

  29. 29

    在Java集合中查找最接近的对象

热门标签

归档