代码之家  ›  专栏  ›  技术社区  ›  Umair Shah

单个数值之和应如何等于数组中的父值?

  •  0
  • Umair Shah  · 技术社区  · 7 年前

    我试图设置一个逻辑并计算一些值,但计算似乎是错误的。

    让我解释一下,例如,我在一个数组中有[23,11,39]等一系列值,然后在下一个数组中,我将这些值分为3个值,第一个数组元素的每个值为:对于23,我将其分为3个值,作为10,10,3,因此这3个值的总和等于后面的23,然后与其他值相同,然后我计算所有这些值相对于40,即23在40中是多少,剩下的是多少,对于除法值也是一样的,所以问题是除法值的总和应该等于40的父计算值,但当我将它们相加时,它们不是。这就是问题所在!

    你们能让我知道我做错了什么以及如何修复吗?

    $data = [76,190,120,39,56,11,17,67,45,23,76,54];
    $data_divided = [[32,14,30],[100,80,10],[50,40,30],[20,10,9],[6,40,10],[3,5,3],[10,4,3],[50,10,7],[30,10,5],[10,20,3],[30,40,6],[4,25,25]];
    
        $min = min($data);
        $max = max($data) - $min;
        $n_data = array();
        $i = 0;
        foreach ($data_divided as $value) {
            $n_value = (($data[$i] - $min) / $max)*40;
            $n0 = (($value[0] - $min) / $max)*40;
            $n1 = (($value[1] - $min) / $max)*40;
            $n2 = (($value[2] - $min) / $max)*40;
            $n_data[] = [$n_value => [abs($n0),abs($n1),abs($n2)], abs($n0)+abs($n1)+abs($n2) ];
            echo "Parent Value : ".$n_value."<br>";
            echo "n0 : ".abs($n0)."<br>";
            echo "n1 : ".abs($n1)."<br>";
            echo "n2 : ".abs($n2)."<br>";
            echo "n0+n1+n2 : ".(abs($n0)+abs($n1)+abs($n2))."<br><br><br>";
            $i++;
        }
    

    以下是其输出:

    Parent Value : 14.525139664804
    n0 : 4.6927374301676
    n1 : 0.67039106145251
    n2 : 4.2458100558659
    n0+n1+n2 : 9.608938547486
    
    
    Parent Value : 40
    n0 : 19.888268156425
    n1 : 15.418994413408
    n2 : 0.22346368715084
    n0+n1+n2 : 35.530726256983
    
    
    Parent Value : 24.357541899441
    n0 : 8.7150837988827
    n1 : 6.4804469273743
    n2 : 4.2458100558659
    n0+n1+n2 : 19.441340782123
    
    
    Parent Value : 6.2569832402235
    n0 : 2.0111731843575
    n1 : 0.22346368715084
    n2 : 0.44692737430168
    n0+n1+n2 : 2.6815642458101
    
    
    Parent Value : 10.055865921788
    n0 : 1.1173184357542
    n1 : 6.4804469273743
    n2 : 0.22346368715084
    n0+n1+n2 : 7.8212290502793
    
    
    Parent Value : 0
    n0 : 1.7877094972067
    n1 : 1.340782122905
    n2 : 1.7877094972067
    n0+n1+n2 : 4.9162011173184
    
    
    Parent Value : 1.340782122905
    n0 : 0.22346368715084
    n1 : 1.5642458100559
    n2 : 1.7877094972067
    n0+n1+n2 : 3.5754189944134
    
    
    Parent Value : 12.513966480447
    n0 : 8.7150837988827
    n1 : 0.22346368715084
    n2 : 0.89385474860335
    n0+n1+n2 : 9.8324022346369
    
    
    Parent Value : 7.5977653631285
    n0 : 4.2458100558659
    n1 : 0.22346368715084
    n2 : 1.340782122905
    n0+n1+n2 : 5.8100558659218
    
    
    Parent Value : 2.6815642458101
    n0 : 0.22346368715084
    n1 : 2.0111731843575
    n2 : 1.7877094972067
    n0+n1+n2 : 4.0223463687151
    
    
    Parent Value : 14.525139664804
    n0 : 4.2458100558659
    n1 : 6.4804469273743
    n2 : 1.1173184357542
    n0+n1+n2 : 11.843575418994
    
    
    Parent Value : 9.608938547486
    n0 : 1.5642458100559
    n1 : 3.1284916201117
    n2 : 3.1284916201117
    n0+n1+n2 : 7.8212290502793
    

    Parent Value : 14.525139664804
    n0 : 4.6927374301676
    n1 : 0.67039106145251
    n2 : 4.2458100558659
    n0+n1+n2 : 14.525139664804
    
    
    Parent Value : 40
    n0 : 19.888268156425
    n1 : 15.418994413408
    n2 : 0.22346368715084
    n0+n1+n2 : 40
    
    
    Parent Value : 24.357541899441
    n0 : 8.7150837988827
    n1 : 6.4804469273743
    n2 : 4.2458100558659
    n0+n1+n2 : 24.357541899441
    
    
    Parent Value : 6.2569832402235
    n0 : 2.0111731843575
    n1 : 0.22346368715084
    n2 : 0.44692737430168
    n0+n1+n2 : 6.2569832402235
    
    
    Parent Value : 10.055865921788
    n0 : 1.1173184357542
    n1 : 6.4804469273743
    n2 : 0.22346368715084
    n0+n1+n2 : 10.055865921788
    
    
    Parent Value : 0
    n0 : 1.7877094972067
    n1 : 1.340782122905
    n2 : 1.7877094972067
    n0+n1+n2 : 0
    
    
    Parent Value : 1.340782122905
    n0 : 0.22346368715084
    n1 : 1.5642458100559
    n2 : 1.7877094972067
    n0+n1+n2 : 1.340782122905
    
    
    Parent Value : 12.513966480447
    n0 : 8.7150837988827
    n1 : 0.22346368715084
    n2 : 0.89385474860335
    n0+n1+n2 : 12.513966480447
    
    
    Parent Value : 7.5977653631285
    n0 : 4.2458100558659
    n1 : 0.22346368715084
    n2 : 1.340782122905
    n0+n1+n2 : 7.5977653631285
    
    
    Parent Value : 2.6815642458101
    n0 : 0.22346368715084
    n1 : 2.0111731843575
    n2 : 1.7877094972067
    n0+n1+n2 : 2.6815642458101
    
    
    Parent Value : 14.525139664804
    n0 : 4.2458100558659
    n1 : 6.4804469273743
    n2 : 1.1173184357542
    n0+n1+n2 : 14.525139664804
    
    
    Parent Value : 9.608938547486
    n0 : 1.5642458100559
    n1 : 3.1284916201117
    n2 : 3.1284916201117
    n0+n1+n2 : 9.608938547486
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   mickmackusa    7 年前

    这是我的建议。。。

    0 40 $data )和分割值之和( $data_divided ),我建议将因子四舍五入到下一个整数( ceil() $divided_data 带减法或 abs() .

    我的演示将存储并显示初始值、计算值以及它们之间的数学差。正如您将在我的注释和输出中看到的,只有带有索引的子阵列 9

    所有计算数据均在可接受范围内,最大值为 38 和一分钟 0.6 这种方法不能保证最大值为 但这将给你的数据提供一个很好的机会,让我准确地加起来。如果您删除 来自的函数 $factor 40 变得可能,但也有一些价值观“摩擦”(一些比其他更糟糕)。

    Demo )

    $data = [76,190,120,39,56,11,17,67,45,23,76,54];
    // notice flawed data-----------------^^
    
    $data_divided = [[32,14,30],[100,80,10],[50,40,30],[20,10,9],[6,40,10],[3,5,3],[10,4,3],[50,10,7],[30,10,5],[10,20,3],[30,40,6],[4,25,25]];
    // notice flawed data----------------------------------------------------------------------------------------^^^^^^^
    
    $factor=ceil(max($data)/40); // 5 in this case (rounds up to a clean integer for future dividing)
    
    $n_data = array();
    foreach ($data_divided as $i=>$a) {
        $n_value=$data[$i]/$factor;  // reduce expected sum value
        $a=array_map(function($v)use($factor){return $v/$factor;},$a);  // reduce split values
        $sum=array_sum($a);  // sum the reduced split values
        $n_data[]=[
                   "N_Value"=>$n_value,
                   "Split"=>$a,
                   "Sum"=>$sum,
                   "Diff"=>$n_value-$sum  // find difference in calculated values
                   ];
    }
    $data = $n_data;
    var_export($data);
    

    array (
      0 => 
      array (
        'N_Value' => 15.2,
        'Split' => 
        array (
          0 => 6.4,
          1 => 2.8,
          2 => 6.0,
        ),
        'Sum' => 15.2,
        'Diff' => 0.0,
      ),
      1 => 
      array (
        'N_Value' => 38.0,
        'Split' => 
        array (
          0 => 20.0,
          1 => 16.0,
          2 => 2.0,
        ),
        'Sum' => 38.0,
        'Diff' => 0.0,
      ),
      2 => 
      array (
        'N_Value' => 24.0,
        'Split' => 
        array (
          0 => 10.0,
          1 => 8.0,
          2 => 6.0,
        ),
        'Sum' => 24.0,
        'Diff' => 0.0,
      ),
      3 => 
      array (
        'N_Value' => 7.8,
        'Split' => 
        array (
          0 => 4.0,
          1 => 2.0,
          2 => 1.8,
        ),
        'Sum' => 7.8,
        'Diff' => 0.0,
      ),
      4 => 
      array (
        'N_Value' => 11.2,
        'Split' => 
        array (
          0 => 1.2,
          1 => 8.0,
          2 => 2.0,
        ),
        'Sum' => 11.2,
        'Diff' => 0.0,
      ),
      5 => 
      array (
        'N_Value' => 2.2,
        'Split' => 
        array (
          0 => 0.6,
          1 => 1.0,
          2 => 0.6,
        ),
        'Sum' => 2.2,
        'Diff' => 0.0,
      ),
      6 => 
      array (
        'N_Value' => 3.4,
        'Split' => 
        array (
          0 => 2.0,
          1 => 0.8,
          2 => 0.6,
        ),
        'Sum' => 3.4,
        'Diff' => 0.0,
      ),
      7 => 
      array (
        'N_Value' => 13.4,
        'Split' => 
        array (
          0 => 10.0,
          1 => 2.0,
          2 => 1.4,
        ),
        'Sum' => 13.4,
        'Diff' => 0.0,
      ),
      8 => 
      array (
        'N_Value' => 9.0,
        'Split' => 
        array (
          0 => 6.0,
          1 => 2.0,
          2 => 1.0,
        ),
        'Sum' => 9.0,
        'Diff' => 0.0,
      ),
      9 => 
      array (
        'N_Value' => 4.6,
        'Split' => 
        array (
          0 => 2.0,
          1 => 4.0,
          2 => 0.6,
        ),
        'Sum' => 6.6,
        'Diff' => -2.0,
      ),
      10 => 
      array (
        'N_Value' => 15.2,
        'Split' => 
        array (
          0 => 6.0,
          1 => 8.0,
          2 => 1.2,
        ),
        'Sum' => 15.2,
        'Diff' => 0.0,
      ),
      11 => 
      array (
        'N_Value' => 10.8,
        'Split' => 
        array (
          0 => 0.8,
          1 => 5.0,
          2 => 5.0,
        ),
        'Sum' => 10.8,
        'Diff' => 0.0,
      ),
    )