将数组元素的最小组合返回到总数

时间:2017-11-10 15:13:42

标签: php arrays

我有一个$array和一个$number,我希望找到$array个元素的最小组合(元素数量最少),它们总和为$number,但我无法弄清楚如何使用PHP代码执行此操作。

测试用例:

$array = [
    'a' => '1',
    'b' => '3',
    'c' => '5',
    'd' => '5',
    'e' => '1',
    'f' => '2',
    'g' => '2',
];

如果$number = 10,则输出应为'c', 'd'

如果$number = 1,则输出应为'a''e'

如果$number = 4,则输出应为'a', 'b''b', 'e''f', 'g'

如果$number = 9输出应为'a', 'b', 'c''a', 'b', 'd''c', 'f', 'g'等。

我如何在代码中写这个?

3 个答案:

答案 0 :(得分:0)

试试这个?

<?php
$array= array("a"=>"1", "b"=>"3", "c"=>"5", "d"=>"5", "e"=>"1", "f"=>"2", "g"=>"2");
$num = 4;
foreach ($array as $key => $value) {
    $n1 = (int) $value;
    if ($n1 === $num) {
        echo $key.'<br/>';
        break;
    }

    if ($n1 < $num) {
        $n2 = $num - $n1;
        if ($n2Key = isN2Exists("$n2", $array)) { // this probably can also be done using some PHP built-ins, but I just couldnt find the right one
            echo "$key,$n2Key <br/>";
        }
    }
}

function isN2Exists($value, $array) {
    foreach ($array as $k => $v) {
        if ($v === $value) {
            return $k;
        }
    }
    return false;
}

如果您需要超过2个号码,这将不起作用。例如,如果数字为9,则无法在数组中生成带有两个数字的9。你需要a,b,c或c,f,g。如果你想这样,我认为最好的方法是使用递归。

<强>更新

如果您想通过递归来完成,请尝试以下代码。这应该为您提供您正在寻找的确切输出:

<?php
$array= array("a"=>"1", "b"=>"3", "c"=>"5", "d"=>"5", "e"=>"1", "f"=>"2", "g"=>"2");
$num = 9;

function getLeastNumbers($num, $array, $sum = 0, $level = '', $resultSet = [], $combination = []) {
    if ($sum > $num) {
        return false;
    }

    foreach ($array as $key => $value) {
        $n1 = (int) $value;

        if (($n1 + $sum) == $num) {

            $newCombination = $combination;
            $newCombination[] = $key;
            sort($newCombination);

            $resultSet[] = $newCombination;
        }

        $combinationToSend = $combination;
        $combinationToSend[] = $key;
        sort($combinationToSend);
        $array2 = $array;
        unset($array2[$key]);
        if ($return = getLeastNumbers($num, $array2, $n1 + $sum, "$level .", $resultSet, $combinationToSend)) {
            $resultSet = array_unique(array_merge($resultSet, $return), SORT_REGULAR);
        }
    }

    return $resultSet;
}

$list = getLeastNumbers($num, $array);

foreach($list as $item) {
    $length = count($item);
    $finalArray[$length][] = $item;
}


print_r($finalArray[min(array_keys($finalArray))]);

答案 1 :(得分:0)

试试这个(并使用递归算法)

output <- t(sapply(as.character(ptuples),
                   function(x) sapply(1:3, function(i) key[substr(x,i,i),])))

答案 2 :(得分:0)

您可以尝试这样的事情

<?php
    $array = ["a"=>"1", "b"=>"3", "c"=>"5", "d"=>"5", "e"=>"1", "f"=>"2", "g"=>"2"];
    $new = $array; 
    $number = 19;//Change here to check all condition 
    $flag = false;
    if(array_sum($new) == $number){
      $temp = array_keys($new);
      echo implode(",",$temp);
      $flag = true;
      exit;
    }
    if($flag)
      exit;

    foreach($array as $key=>$value){
      if($value == $number){
        echo $key."\n";
        $flag = true;
        exit;
      }
    }
    if($flag)
      exit;

    foreach($array as $key=>$value){
      $new = $array;
      array_pop($new);
      foreach($new as $key2=>$value2){
        if($key!=$key2){
          if(($value + $value2) == $number){
            echo "$key , $key2 \n";
            $flag = true;
            exit;
          }
        }
      }
    }
    if($flag)
      exit;  

    $new = $array;
    foreach($array as $key1=>$value1){
      foreach($array as $key=>$value){
        $new = $array;
        unset($new[$key]);
        if(array_sum($new) == $number){
          $temp = array_keys($new);
          echo implode(",",$temp);
          exit;
        }
        array_pop($new);
      }
      array_pop($array);
    }

?>

现场演示:https://eval.in/897632