【问题标题】:How to get and generate repetitive numbers (subset) arrays in PHP如何在 PHP 中获取和生成重复数字(子集)数组
【发布时间】:2019-09-04 03:52:47
【问题描述】:

我检查了代码,发现它不能显示重复的数字

我的代码

      <?php

      /* Designated level for each exp
      Level 2 - 23 exp
      Level 3 - 34 exp
      Level 4 - 45 exp
      Level 5 - 56 exp
      Level 6 - 68 exp
      Level 7 - 79 exp
      Level 8 - 90 exp
      Level 9 - 101 exp
      Level 10 - 112 exp
      Level 11 - 123 exp
      Level 12 - 134 exp
      Level 13 - 145 exp
      Level 14 - 156 exp
      Level 15 - 168 exp
      Level 16 - 179 exp
      */

      $limit =  100000-99370;

      // Level
      $arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');

      // Exp
      $array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);

      $array = array_filter($array, function($var) use ($limit) {
      return ($var <= $limit);
      });

      $num = count($array);
      $total = pow(2, $num);
      $out = array();

      for ($i = 0; $i < $total; $i++) {

      $comb = array();
      for ($j = 0; $j < $num; $j++) {
      // is bit $j set in $i?
      if (pow(2, $j) & $i){
      $comb[] = $array[$j];
      }
      }

      if (array_sum($comb) == $limit)
      {
      $out[] = $comb;
      }
      }

      array_multisort(array_map('count', $out), SORT_ASC, $out);

      $out = array_unique($out, SORT_REGULAR);
      $m = 1;
      $mapper = [
      23 => "Level 2",
      34 => "Level 3",
      45 => "Level 4",
      56 => "Level 5",
      68 => "Level 6",
      79 => "Level 7",
      90 => "Level 8",
      101 => "Level 9",
      112 => "Level 10",
      123 => "Level 11",
      134 => "Level 12",
      145 => "Level 13",
      156 => "Level 14",
      168 => "Level 15",
      179 => "Level 16",
      ];

      foreach($out as $result)
      echo "<b>Possible Answer ". $m++. " : </b><br> " .implode(' , ', array_map(function($x) use ($mapper) {
      return $mapper[$x] . " - " . $x;
      }, $result))." 
      <br><br>";

我的输入和输出

如果我输入 99318
输出是这样的
可能的答案 1:
10 - 112 级 , 11 - 123 级 , 12 - 134 级 , 13 - 145 级 , 15 - 168 级
我也想生成重复的数字

但它不能像这样显示一些重复的数字答案
可能的答案:
4 - 45 级 , 10 - 112 级 , 11 - 123 级 , 11 - 123 级 , 12 - 134 级 , 13 - 145 级

你可以看到有两个 Level 11 - 123



我想要这样的输出

可能的答案:
等级 4 - 45 , 等级 10 - 112 , 等级 11 (x2) - 246 , 等级 12 - 134 , 等级 13 - 145
我想将所有重复的数字分组并总结它们

【问题讨论】:

  • 提供的代码不会在输入 99318 时生成重复数字。请检查您的代码并解决问题。
  • 我的代码甚至没有生成任何重复的数字。我知道该怎么做。我也问这个问题

标签: php arrays


【解决方案1】:

获得结果的一个选项是将另一个值传递给 array_map,结果为array_count_values

然后在映射中,您可以像映射器一样根据$countValues[$x] 之类的索引确定显示数字的计数。

例如

foreach($out as $result) {
    $countValues = array_count_values($result);
    echo "<b>Possible Answer " . $m++ . " : </b><br> " . implode(' , ',
            array_map(function ($x) use ($mapper, $countValues) {
                $strCount = $countValues[$x] > 1 ? " (" . $countValues[$x] . ")" : "";
                return $mapper[$x] . $strCount . " - " . $x;
            }, array_unique($result))) . "
      <br><br>";
}

这会给你一个类似的结果

Possible Answer 1 :
Level 2 - 23 , Level 6 - 68 , Level 7 (x2) - 79 , Level 9 - 101 , Level 10 - 112 , Level 15 - 168 

Php demo 作为测试,$array 的重复值 79

【讨论】:

  • 如果我的限制是这个 $limit = 100000-99730。此操作的结果是 270。程序的结果是: 可能的答案 2 : 级别 2 - 23 , 级别 7 - 79 , 级别 15 - 168 可能的答案 3 : 级别 6 - 68 , 级别 7 - 79 , 级别 11 - 123 可能的答案 4:第 7 级 (x2) - 79,第 10 级 - 112。
  • 但我想看到一些可能的结果,例如:可能的答案:8 级 (x3) - 270 和可能的答案:8 级 (x2) - 180,4 级 (x2) - 90,总共也是270。以及如何获得乘以计数的结果,因为在您的代码输出中它只显示级别 7 (x2) - 79 并且必须是级别 7 (x2) - 158
  • 输出和可能的结果是这样的:可能的答案 1:2 - 23 级,6 - 68 级,16 - 179 级可能的答案 2:2 - 23 级,7 - 79 级,级别 15 - 168 可能的答案 3:级别 7 (x2) - 158,级别 10 - 112 可能的答案 4:级别 8 (x3) - 270 可能的答案 5:级别 8 (x2) - 180,级别 4 (x2) - 90等等……
  • 就像这样 - geeksforgeeks.org/combinational-sum 。但是是用 C++ 制作的——
  • 从问题中得到这个逻辑并不明显。您需要的结果是否可以仅从 $result 数组派生?我的意思是,您想要的输出是否仅基于如何表示 $result 中的数据?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多