【问题标题】:Finding out the total number of possible combinations from five arrays of items从五个项目数组中找出可能组合的总数
【发布时间】:2014-12-24 06:31:55
【问题描述】:

我有五个巨大的数组,每个数组都用字符串填充,每个数组/列表中有不同数量的项目。
这是列表的一个示例:

List 1

    "Jeffrey the Great",
    "Bean-man",
    "Joe",
    "Charles",
    "Flamur",
    "Leka",
    "the defender of men",

List 2

    "awesome",
    "claustrophobic",
    "sad",
    "very masculine",
    "stinky",
    "outrageous",
    "underage",
    "endangered",
    "filthy",
    "kinda smooth",
    "overly threatening",

List 3

    "corpse",
    "skeleton",
    "average joe",
    "mafia boss",
    "murderer",
    "butcher",
    "dog",
    "muslim",
    "fish salesman",

List 4

    "impregnate",
    "punch",
    "eat",
    "kill",
    "hunt down",
    "outrun",
    "touch",


List 5

    "Steve",
    "Stalin",
    "el Chupacabra",
    "everyone",
    "a donut",
    "a fish",
    "the Americans",
    "your neighbors",

我想找出这五个列表的所有可能组合 例如:

Jeffery the Great, awesome, corpse, impregnate, steve
Bean-man, awesome, corpse, impregnate, steve
Joe, awesome, corpse, impregnate, steve
Charles, awesome, corpse, impregnate, steve
Jeffery the Great, claustrophobic, corpse, impregnate, steve
Bean-man, claustrophobic, corpse, impregnate, steve
Joe, claustrophobic, corpse, impregnate, steve
Charles, claustrophobic, corpse, impregnate, steve

...对于每个列表中的每个项目,依此类推。

每个组合必须包含每个数组/列表中的一个。 我不想生成可能的组合,只有每个可能组合的 count! 示例:

List 1
    a
    b
    c

List 2
    1
    2
    3

结果:

9 possible combinations

任何线索我怎么能找到这个?

【问题讨论】:

  • 不是5个长度的乘积吗?
  • 不就是(列表 1 中的项目总数)*(列表 2 中的项目总数)吗?

标签: javascript arrays


【解决方案1】:

简单的排列组合数学和javascript

var num_of_combination = list1.length * list2.length * list3.length * list4.length * list5.length;

【讨论】:

  • 哦!没想到这么简单!起初我确实尝试过,但它返回了超过 10 亿的数字。没想到这么高感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多