php实现包裹分配算法实现

有如下2个数组:p代表人,b代表包裹

$arr1 = [p1, p2, p3, p4, p5, p6, p7];
$arr2 = [b1, b2, b3, b4, b5, b6 ,b7 ,b8, b9 ,b10];

将10个包裹一次分配给7个人,第一次分配将7个包裹分给了前7个人,剩下的3个再依次分给前3个人;如何实现?

阅读 4.2k
3 个回答
$arr1 = ['p1', 'p2','p3','p4','p5','p6','p7'];
$arr2 = ['b1', 'b2','b3','b4','b5','b6','b7','b8','b9','b10','b11', 'b12','b13','b14','b15','b16','b17','b18','b19','b20'];
       
$num = floor(count($arr2) / count($arr1));
$arr3 = [];
foreach ($arr1 as $key => $value) {
    for($i = 0;$i < $num;$i++){
        $ind = $key + $i * count($arr1);
            $arr3[$key][] = $arr2[$ind];
            
     }
 }

$yushu = count($arr2) % count($arr1);
for($n = 0;$n < $yushu;$n ++){
    $nu = $n  + $num *  count($arr1);
    $arr3[$n][] = $arr2[$nu];
}

var_dump($arr3);


/*array(7) {
              [0] => array(3) {
                [0] => string(2) "b1"
                [1] => string(2) "b8"
                [2] => string(3) "b15"
              }
              [1] => array(3) {
                [0] => string(2) "b2"
                [1] => string(2) "b9"
                [2] => string(3) "b16"
              }
              [2] => array(3) {
                [0] => string(2) "b3"
                [1] => string(3) "b10"
                [2] => string(3) "b17"
              }
              [3] => array(3) {
                [0] => string(2) "b4"
                [1] => string(3) "b11"
                [2] => string(3) "b18"
              }
              [4] => array(3) {
                [0] => string(2) "b5"
                [1] => string(3) "b12"
                [2] => string(3) "b19"
              }
              [5] => array(3) {
                [0] => string(2) "b6"
                [1] => string(3) "b13"
                [2] => string(3) "b20"
              }
              [6] => array(2) {
                [0] => string(2) "b7"
                [1] => string(3) "b14"
              }
            }*/
<?php 
/**
 * 分配函数
 *
 * @author Flc  <2017-12-07 22:14:12>
 * @link   http://flc.ren
 * @param  array  $couriers 快递员
 * @param  array  $packages 包裹
 * @return array            key为快递员;值为包裹
 */
function dist($couriers = [], $packages = [])
{
    $couriers      = array_values($couriers);
    $packages      = array_values($packages);
    $courier_count = count($couriers);

    $result = [];

    foreach ($packages as $key => $package) {
        $remainder = ($key + 1) % $courier_count;

        $sub = ($remainder ?: $courier_count) - 1;
            
        $result[$couriers[$sub]][] = $package;
    }

    return $result;
}

$arr1 = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7'];
$arr2 = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10'];

print_r(dist($arr1, $arr2));

结果如下:

Array
(
    [p1] => Array
        (
            [0] => b1
            [1] => b8
        )

    [p2] => Array
        (
            [0] => b2
            [1] => b9
        )

    [p3] => Array
        (
            [0] => b3
            [1] => b10
        )

    [p4] => Array
        (
            [0] => b4
        )

    [p5] => Array
        (
            [0] => b5
        )

    [p6] => Array
        (
            [0] => b6
        )

    [p7] => Array
        (
            [0] => b7
        )

)
<?php
$arr1 = ['p1', 'p2','p3','p4','p5','p6','p7'];
$arr2 = ['b1', 'b2','b3','b4','b5','b6','b7','b8','b9','b10','b11', 'b12','b13','b14','b15','b16','b17','b18','b19','b20'];

if(count($arr1) >= count($arr2)){  //若快递员人数比包裹多,直接循环分配
    foreach ($arr2 as $key => $value) {
        $arr3[$key][] = $value;
    }
}else{  //包裹比快递员人数多
    
    $num1 = floor(count($arr2)/count($arr1));   //取整
    $num2 = count($arr2)%count($arr1);   //得到剩余的包裹数量(求余数)
    for($i=1;$i<=$num1;$i++){
            $s_num = count($arr1)*($i-1); //切割起始位置
            $arr3[] = array_slice($arr2,$s_num,count($arr1)); //切割
    }

        $arr3[$num1][] = array_slice($arr2,$s_num+count($arr1),$num2); //剩余包裹分发

}


var_dump($arr3);
?>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏