把总数amount拆分以标准差最小的标准,平均拆分成count个整数

    public function tt_add(){
        $res = $this->arr_avg(20,14);
        echo array_sum($res);
        echo '----' . count($res);
        dump($res);
        exit;
    }


    /***
     * @param $amount '总数'
     * @param $count '需要拆分数据个数'
     * @return array|mixed '把总数amount拆分已标准差最小的标准,平均拆分成count个整数'
     */
    public function arr_avg($amount, $count)
    {
        $avg_num  = $amount / $count;
        $ceil_num = ceil($amount / $count);

        if ($avg_num == $ceil_num) {
            $avg_res = array_fill(0, $count, (int)$ceil_num);
        } else {
            if ($amount < $count) {
                $avg_res_1 = array_fill(0, $amount, (int)$ceil_num);      // 这里只会填充1
                $avg_res_2 = array_fill($amount - 1, $count - $amount, 0);
                $avg_res   = dealed_array_merge($avg_res_1, $avg_res_2);
            } else {
                $floor_num = floor($amount / $count);
                $avg_res   = array_fill(0, $count, (int)$floor_num);

                $left_num = $amount - array_sum($avg_res);
                if (1 <= $left_num) {
                    $left_avg_res = $this->arr_avg($left_num, $count);

                    foreach ($left_avg_res as $k => $v){
                        if ($v == 0){
                            unset($left_avg_res[$k]);
                        }
                    }
                    unset($v);

                    $left_avg_res = array_values($left_avg_res);
                    $avg_res = $this->array_add($avg_res,$left_avg_res);
                }

            }
        }

        return $avg_res;
    }

    /***
     * @param $a
     * @param $b
     * @return mixed '2个一维数组相加,键名相同相加,不同的均保留'
     */
    public function array_add($a,$b)
    {
        //根据键名获取两个数组的交集
        $arr = array_intersect_key($a, $b);

        //遍历第二个数组,如果键名不存在与第一个数组,将数组元素增加到第一个数组
        foreach ($b as $key => $value) {
            if (!array_key_exists($key, $a)) {
                $a[$key] = $value;
            }
        }

        //计算键名相同的数组元素的和,并且替换原数组中相同键名所对应的元素值
        foreach ($arr as $key => $value) {
            $a[$key] = $a[$key] + $b[$key];
        }

        //返回相加后的数组
        return $a;
    }

例:amount:20 count:14

20----8

<pre>array(8) {
[0] =&gt; int(3)
[1] =&gt; int(3)
[2] =&gt; int(3)
[3] =&gt; int(3)
[4] =&gt; int(2)
[5] =&gt; int(2)
[6] =&gt; int(2)
[7] =&gt; int(2)
}
</pre>

例:amount: 20, count : 20

20----20

<pre>array(20) {
[0] =&gt; int(1)
[1] =&gt; int(1)
[2] =&gt; int(1)
[3] =&gt; int(1)
[4] =&gt; int(1)
[5] =&gt; int(1)
[6] =&gt; int(1)
[7] =&gt; int(1)
[8] =&gt; int(1)
[9] =&gt; int(1)
[10] =&gt; int(1)
[11] =&gt; int(1)
[12] =&gt; int(1)
[13] =&gt; int(1)
[14] =&gt; int(1)
[15] =&gt; int(1)
[16] =&gt; int(1)
[17] =&gt; int(1)
[18] =&gt; int(1)
[19] =&gt; int(1)
}
</pre>

例: amount: 20, count: 22

20----22

<pre>array(22) {
[0] =&gt; int(1)
[1] =&gt; int(1)
[2] =&gt; int(1)
[3] =&gt; int(1)
[4] =&gt; int(1)
[5] =&gt; int(1)
[6] =&gt; int(1)
[7] =&gt; int(1)
[8] =&gt; int(1)
[9] =&gt; int(1)
[10] =&gt; int(1)
[11] =&gt; int(1)
[12] =&gt; int(1)
[13] =&gt; int(1)
[14] =&gt; int(1)
[15] =&gt; int(1)
[16] =&gt; int(1)
[17] =&gt; int(1)
[18] =&gt; int(1)
[19] =&gt; int(1)
[20] =&gt; int(0)
[21] =&gt; int(0)
}
</pre>







原文地址:https://www.cnblogs.com/pansidong/p/11649547.html