2015-10-13 68 views
2

我需要填写与数字的阵列,几个例子:填充阵列与具体的数字

  • nbreParChargement = 4; 10

    • 预期输出:$niveaux = array(4,4,2); 4 + 4 + 2 = 10
  • nbreParChargement = 6; 14

    • 预期输出:$niveaux = array(6,6,2); 6 + 6 + 2 = 14
  • nbreParChargement = 16; 13

    • 预期输出:$niveaux = array(13); 13 = 13
  • nbreParChargement = 4;对于30

    • 预期输出:$niveaux = array(4,4,4,4,4,4,4,2); 4 + 4 + 4 + 4 + 4 + 4 + 4 + 2 = 30

我试图解决,但我不知道完美的解决方案:

function chargement($nbre, $nbreParChargement){ 

    $niveaux = array(); 
    for($i=0; $i<=$nbre; $i++){ 
     $niveaux[$i] = $i + $nbreParChargement;   
    } 
} 
+0

这些数字的规则是什么?随机数字?符合公式的数字? – Bonatti

+0

请记住要将变量和函数名称翻译成英文,否则非法语的用户可能很难弄清楚您的代码。 –

回答

2

另一实现中,与两行代码,为PHP> = 5.6:

function chargement($nbre, $nbreParChargement){ 
    $niveaux = array_fill(0, floor($nbre/$nbreParChargement), $nbreParChargement); 
    $niveaux[] = $nbre - array_sum($niveaux); 

    return $niveaux; 
} 

var_dump(chargement(10, 4)); 
var_dump(chargement(14, 6)); 
var_dump(chargement(13, 16)); 
var_dump(chargement(30, 4)); 

array_fill用的值填充$nbre/$nbreParChargement条目的数组。最后一个值是$nbre$nbreParChargement * floor($nbre/$nbreParChargement)给出的其他元素之和之间的差值,该值总是大于0.

Demo

此作品仅适用于PHP> = 5.6,因为array_fill仅接受该版本的0作为num。以前,要求num大于零。

因此,对于任何版本的PHP:

function chargement($nbre, $nbreParChargement){ 
    $niveaux = array(); 
    if ($n = floor($nbre/$nbreParChargement)) { 
     $niveaux = array_fill(0, $n, $nbreParChargement); 
    } 

    $niveaux[] = $nbre - array_sum($niveaux); 
    return $niveaux; 
} 

Demo

-1

试试这个:

<?php 
function chargement($nbre, $nbreParChargement){ 

    $niveaux = array(); 
    if($nbreParChargement <= $nbre) { 
     $repetitionCount = floor($nbre/$nbreParChargement); 
     $remainder = $nbre % $nbreParChargement; 

     for($i = 0; $i < $repetitionCount; $i++){ 
      $niveaux[] = $nbreParChargement; 
     } 

     if(!empty($remainder)) { 
      $niveaux[] = $remainder; 
     } 
    } else { 
     $niveaux[] = $nbre; 
    } 
    return $niveaux; 
} 

$array = chargement(13, 16); 

echo '<pre>'; 
print_r($array); 
+0

为什么在代码适用于所有提到的情况时,这是低估的? –

+0

虽然此代码段可能会解决问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。明白了, –

+1

。很公平。谢谢(你的)信息。 –

1
<?php 
    function chargement($nbre, $nbreParChargement){ 
     $niveaux = array(); 
     $n=(int)($nbre/$nbreParChargement);//Calculating how many times to $nbreParChargement add number to an array. 
     for($i=0;$i<$n;$i++){ 
      $niveaux[]=$nbreParChargement;//Adding $nbreParChargement value to array 
     } 
     $diff=$nbre-array_sum($niveaux);//Calculating diff of given and our calculated value 
     if($diff!=0){//If the diff value is >0 then adding the remaining that value to an array 
      $niveaux[]=$diff; 
     } 
     return $niveaux; //Returning array 
    } 
    print_r(chargement(14,4)); 

Output in Eval

输出:

Array 
(
    [0] => 4 
    [1] => 4 
    [2] => 4 
    [3] => 2 
) 

按照波纹管的步骤来解决问题:

  1. 计算nbreParChargement将数字添加到数组的次数,
  2. 将该数字添加到数组中多次(n)。
  3. 计算给定值和我们的总值的差值
  4. 如果差值大于0,那么就意味着我们错过了差数,将该数加到数组并返回。
+0

这段代码有什么问题? :X,给-1然后留下评论,咆哮任何答案。如果你愿意这样做,不会回复你的问题。 –

+0

@Kyll,检查代码,我给每个发布代码的评论,在投票前检查一次,发布了什么人。 –

+0

@Kyll,新增说明 –