【问题标题】:PHP Looping in an array to find how many elements can I sum without surpassing an amountPHP循环在数组中查找我可以在不超过数量的情况下求和多少个元素
【发布时间】:2020-06-04 20:46:40
【问题描述】:

我正在尝试循环排序降序数组,如果值大于 30,我们取消设置元素并增加一个计数器。

如果元素小于 30,我们检查另一个元素,看看总和是大于还是小于 30。

如果它更小,我们添加以下元素,如果它更大,我们取消设置之前使用的元素并增加计数器。

数组的一个例子是 [35, 20 ,15, 14, 9, 5]

for($x = 0; $x < $arrayItemSize; $x++)
    {
        if($sortArrayItem[$x] >= 30)
        {
            unset($weight[$x]);
            $counter++;
        }
        else if($sortArrayItem[$x] < 30)
        {
            for($i = $x++; $i < $arrayItemSize; $i++)
            {

                $newWeight = $sortArrayItem[$x] + $sortArrayItem[$i];
                if($newWeight >= 30)
                {
                    unset($weight[$x]);
                    unset($weight[$i]);
                    $counter++;
                }
                else {
                    unset($weight[$x]);
                    $x++;
                    $i++;
                    if($newWeight + $weight[$i] > 30)
                    {
                        unset($weight[$x]);
                        $counter++;
                    }
                }
            }

        }
    }

这是我到目前为止所得到的,但我只是对如何进行感到困惑。

【问题讨论】:

    标签: php arrays for-loop


    【解决方案1】:

    最好使用while 语句,1 个循环就足够了。

    $source = array(35, 20 ,15, 14, 9, 5);
    $i = 0 ;
    $counter = 0;
    $len = count($source);
    while( $i < $len && $source[$i] > 14 ) {
        if($source[$i] >= 30 ) {
            unset($source[$i]);
            $counter++;
        } else if ($source[$i] < 30) {
            if($i + 1 < $len) {
                $sum = $source[$i] + $source[$i+1] ;
                    unset($source[$i]);
                    unset($source[$i + 1]);
                    $i++;
                    $counter += 2;
            }
        }
        $i++ ;
    }
    
    var_dump($source);
    var_dump( $counter );
    

    【讨论】:

    • 感谢您的意见,我想出了另一个解决方案来解决我的问题,如果您能看一下并告诉我您的想法,那将会很有帮助。谢谢
    【解决方案2】:

    因此,与@Nazeri 相比,我想出了一个不同的解决方案。我们检查总重量是大于还是小于指定的数量。如果更大,我们从总重量中删除最大的元素,取消设置并增加计数器。如果更小,我们取消设置整个数组并增加计数器。

    请随时留下代码中发现的任何意见或错误。

    if($arrayItemSize > 1)
    {
        for($x = 0; $x < $arrayItemSize - 1; $x++)
        {
            if($itemTotalWeight <= 30)
            {
                unset($sortArrayItem);
                $counter++;
                break;
            }
    
            else
            {
                $itemTotalWeight = $itemTotalWeight - $sortArrayItem[$x];
                unset($sortArrayItem[$x]);
                $counter++;
    
            }
        }
    
    }
    else {
                $counter++;
    
         }
    

    【讨论】:

    • itemTotalWeight变量的初始值如何设置?
    • 我认为它会删除数组的所有元素
    • 由于数组永远不会固定,我们可以在创建数组时使用 array_sum() 添加所有元素。我还修复了第二个 if 语句的权重。恢复原值。
    • 通过调用unset($sortArrayItem);,您将sortArrayItem 完全从内存中删除。它不起作用。
    • 我想我没有清楚地解释这个问题,或者可能是错误的,但这样做的目的是看看有多少盒子可以容纳 30 磅,我们是否需要将所有元素放入数组中.因此,如果从一开始总重量低于 30,则意味着我们只需要 1 个盒子。但是从另一个角度来看,似乎 unset 似乎对问题没有必要,因为如果我们输入 else 语句,$x 将递增并且永远不会返回。 @EhsanNazeri
    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2023-03-25
    • 2015-10-07
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多