- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要实现的是结合N骰子发布的经典结果之一,但是将结果保存在具有MN字段的矩阵中(其中N是骰子数,M是可能的总数组合-通过6 ^ N获得)。到目前为止,我已经编写了以下代码:
function Dice (commonFace, singleFace){
this.diceFaces = ["critical", commonFace, commonFace, singleFace, "support1", "support2"]
this.numCases = function(){
return Math.pow(this.diceFaces.length, numberDices)
}
}
//create the attack dice
var attackDice = new Dice("smash", "fury");
//create the defence dice
var defenceDice = new Dice("block", "dodge");
//create a function that rolls the dice results and returns the number of results results
function rollDiceResults(diceTypeRolled, numberDicesRolled) {
//total possible results of the rolls of that number of dices
var totalPossibilites = diceTypeRolled.numCases(numberDicesRolled);
//store the dice results
var diceResults = new Array;
function rollDice(diceType, iteration, array) {
if (iteration == 1) {
//return the base case
for (i = 0; i < diceType.diceFaces.length; i++) {
array[i] = (diceType.diceFaces[i]);
}
} else {
//continue
for (i = 0; i < diceType.diceFaces.length; i++) {
array[i] = diceType.diceFaces[i];
rollDice(diceType, iteration - 1, tempResult);
}
}
}
for (i = 0; i < numberDicesRolled; i++) {
rollDice(diceTypeRolled, numberDicesRolled, diceResults);
}
}
最佳答案
定长组合
Recursion是一种功能性遗产,因此将其与功能性风格结合使用将产生最佳效果。递归就是将大问题分解为较小的子问题,直到达到基本情况为止。
下面,我们使用建议的Array.prototype.flatMap
,但为尚未支持它的环境提供了一个polyfill。当n = 0
达到基本情况时,我们返回空结果。归纳的情况是n > 0
,其中choices
将添加到较小问题combination (choices, n - 1)
的结果中–我们说这里的问题较小,因为n - 1
更接近n = 0
的基本情况
Array.prototype.flatMap = function (f)
{
return this.reduce ((acc, x) => acc.concat (f (x)), [])
}
const combinations = (choices, n = 1) =>
n === 0
? [[]]
: combinations (choices, n - 1) .flatMap (comb =>
choices .map (c => [ c, ...comb ]))
const faces =
[ 1, 2, 3 ]
// roll 2 dice
console.log (combinations (faces, 2))
// [ [ 1, 1 ], [ 2, 1 ], [ 3, 1 ], [ 1, 2 ], ..., [ 2, 3 ], [ 3, 3 ] ]
// roll 3 dice
console.log (combinations (faces, 3))
// [ [ 1, 1, 1 ], [ 2, 1, 1 ], [ 3, 1, 1 ], [ 1, 2, 1 ], ..., [ 2, 3, 3 ], [ 3, 3, 3 ] ]
combinations
rollDice
看起来像这样
const rollDice = (dice, numberOfDice) =>
combinations (dice.diceFaces, numberOfDice)
console.log (rollDice (attackDice, 2))
// [ [ 'critical', 'critical' ]
// , [ 'smash', 'critical' ]
// , [ 'smash', 'critical' ]
// , [ 'fury', 'critical' ]
// , [ 'support1', 'critical' ]
// , [ 'support2', 'critical' ]
// , [ 'critical', 'smash' ]
// , [ 'smash', 'smash' ]
// , ...
// , [ 'critical', 'support2' ]
// , [ 'smash', 'support2' ]
// , [ 'smash', 'support2' ]
// , [ 'fury', 'support2' ]
// , [ 'support1', 'support2' ]
// , [ 'support2', 'support2' ]
// ]
flatMap
和
map
的工作方式,我们可以自己实现它们。完全递归。
const None =
Symbol ()
const map = (f, [ x = None, ...xs ]) =>
x === None
? []
: [ f (x), ...map (f, xs) ]
const flatMap = (f, [ x = None, ...xs ]) =>
x === None
? []
: [ ...f (x), ...flatMap (f, xs) ]
const combinations = (choices = [], n = 1) =>
n === 0
? [[]]
: flatMap ( comb => map (c => [ c, ...comb ], choices)
, combinations (choices, n - 1)
)
const faces =
[ 1, 2, 3 ]
// roll 2 dice
console.log (combinations (faces, 2))
// [ [ 1, 1 ], [ 2, 1 ], [ 3, 1 ], [ 1, 2 ], ..., [ 2, 3 ], [ 3, 3 ] ]
// roll 3 dice
console.log (combinations (faces, 3))
// [ [ 1, 1, 1 ], [ 2, 1, 1 ], [ 3, 1, 1 ], [ 1, 2, 1 ], ..., [ 2, 3, 3 ], [ 3, 3, 3 ] ]
combinations
允许我们确定重复的固定选择集的可能组合。如果我们有2个独特的骰子并想获得所有可能的掷骰子怎么办?
const results =
rollDice (attackDice, defenceDice) ???
rollDice (attackDice, 1)
,然后呼叫
rollDice (defenceDice, 1)
,然后以某种方式组合答案。但是有更好的方法。一种允许任意数量的唯一骰子的方法,即使每个骰子的侧面数量不同也是如此。下面,我向您展示了我们编写的
combinations
的两个版本以及为获得未开发的潜力而进行的必要更改
// version 1: using JS natives
const combinations = (choices, n = 1) =>
const combinations = (choices = None, ...rest) =>
n === 0
choices === None
? [[]]
: combinations (choices, n - 1) .flatMap (comb =>
: combinations (...rest) .flatMap (comb =>
choices .map (c => [ c, ...comb ]))
// version 2: without dependencies
const combinations = (choices = [], n = 1) =>
const combinations = (choices = None, ...rest) =>
n === 0
choices === None
? [[]]
: flatMap ( comb => map (c => [ c, ...comb ], choices)
, combinations (choices, n - 1)
, combinations (...rest)
)
combinations
,我们可以滚动任意大小的任意数量的骰子-即使在程序中也可以使用物理上不可能的三面骰子^ _ ^
// version 3: variadic dice
const combinations = (choices = None, ...rest) =>
choices === None
? [[]]
: flatMap ( comb => map (c => [ c, ...comb ], choices)
, combinations (...rest)
)
const d1 =
[ 'J', 'Q', 'K' ]
const d2 =
[ '♤', '♡', '♧', '♢' ]
console.log (combinations (d1, d2))
// [ [ 'J', '♤' ], [ 'Q', '♤' ], [ 'K', '♤' ]
// , [ 'J', '♡' ], [ 'Q', '♡' ], [ 'K', '♡' ]
// , [ 'J', '♧' ], [ 'Q', '♧' ], [ 'K', '♧' ]
// , [ 'J', '♢' ], [ 'Q', '♢' ], [ 'K', '♢' ]
// ]
console.log (combinations (d1, d1, d1))
// [ [ 'J', 'J', 'J' ]
// , [ 'Q', 'J', 'J' ]
// , [ 'K', 'J', 'J' ]
// , [ 'J', 'Q', 'J' ]
// , [ 'Q', 'Q', 'J' ]
// , [ 'K', 'Q', 'J' ]
// , [ 'J', 'K', 'J' ]
// , ...
// , [ 'K', 'Q', 'K' ]
// , [ 'J', 'K', 'K' ]
// , [ 'Q', 'K', 'K' ]
// , [ 'K', 'K', 'K' ]
// ]
rollDice
编写为
const rollDice = (...dice) =>
combinations (...dice.map (d => d.diceFaces))
console.log (rollDice (attackDice, defenceDice))
// [ [ 'critical', 'critical' ]
// , [ 'smash', 'critical' ]
// , [ 'smash', 'critical' ]
// , [ 'fury', 'critical' ]
// , [ 'support1', 'critical' ]
// , [ 'support2', 'critical' ]
// , [ 'critical', 'block' ]
// , [ 'smash', 'block' ]
// , ...
// , [ 'support2', 'support1' ]
// , [ 'critical', 'support2' ]
// , [ 'smash', 'support2' ]
// , [ 'smash', 'support2' ]
// , [ 'fury', 'support2' ]
// , [ 'support1', 'support2' ]
// , [ 'support2', 'support2' ]
// ]
const rollDice = (...dice) =>
combinations (...dice.map (d => d.diceFaces))
console.log (rollDice (defenceDice, attackDice, attackDice, attackDice))
// [ [ 'critical', 'critical', 'critical', 'critical' ]
// , [ 'block', 'critical', 'critical', 'critical' ]
// , [ 'block', 'critical', 'critical', 'critical' ]
// , [ 'dodge', 'critical', 'critical', 'critical' ]
// , [ 'support1', 'critical', 'critical', 'critical' ]
// , [ 'support2', 'critical', 'critical', 'critical' ]
// , [ 'critical', 'smash', 'critical', 'critical' ]
// , [ 'block', 'smash', 'critical', 'critical' ]
// , [ 'block', 'smash', 'critical', 'critical' ]
// , [ 'dodge', 'smash', 'critical', 'critical' ]
// , [ 'support1', 'smash', 'critical', 'critical' ]
// , ...
// ]
const dice =
[ attackDice, attackDice, attackDice, attackDice, attackDice, attackDice, attackDice ]
rollDice (...dice)
// => ...
combinations
的实现,如果它不会导致您的环境无限期挂起,则将导致堆栈溢出错误
combinations
,但这一次使用与生成器交互所需的命令式样式。
const None =
Symbol ()
const combinations = function* (...all)
{
const loop = function* (comb, [ choices = None, ...rest ])
{
if (choices === None)
return
else if (rest.length === 0)
for (const c of choices)
yield [ ...comb, c ]
else
for (const c of choices)
yield* loop ([ ...comb, c], rest)
}
yield* loop ([], all)
}
const d1 =
[ 'J', 'Q', 'K', 'A' ]
const d2 =
[ '♤', '♡', '♧', '♢' ]
const result =
Array.from (combinations (d1, d2))
console.log (result)
// [ [ 'J', '♤' ], [ 'J', '♡' ], [ 'J', '♧' ], [ 'J', '♢' ]
// , [ 'Q', '♤' ], [ 'Q', '♡' ], [ 'Q', '♧' ], [ 'Q', '♢' ]
// , [ 'K', '♤' ], [ 'K', '♡' ], [ 'K', '♧' ], [ 'K', '♢' ]
// , [ 'A', '♤' ], [ 'A', '♡' ], [ 'A', '♧' ], [ 'A', '♢' ]
// ]
Array.from
急切地将所有组合收集到单个
result
中。使用发电机时,这通常不是必需的。相反,我们可以在生成值时使用它们
for...of
与每个组合从生成器中直接进行交互。在此示例中,我们显示了包含
J
或
♡
的任何组合
const d1 =
[ 'J', 'Q', 'K', 'A' ]
const d2 =
[ '♤', '♡', '♧', '♢' ]
for (const [ rank, suit ] of combinations (d1, d2))
{
if (rank === 'J' || suit === '♡' )
console.log (rank, suit)
}
// J ♤ <-- all Jacks
// J ♡
// J ♧
// J ♢
// Q ♡ <-- or non-Jacks with Hearts
// K ♡
// A ♡
for
块中编写任何内容。在下面,我们添加了附加条件以使用
Q
跳过皇后区
continue
const d1 =
[ 'J', 'Q', 'K', 'A' ]
const d2 =
[ '♤', '♡', '♧', '♢' ]
for (const [ rank, suit ] of combinations (d1, d2))
{
if (rank === 'Q')
continue
if (rank === 'J' || suit === '♡' )
console.log (rank, suit)
}
// J ♤
// J ♡
// J ♧
// J ♢
// K ♡ <--- Queens dropped from the output
// A ♡
break
生成组合。下面,如果遇到King
K
,我们将立即停止生成器
const d1 =
[ 'J', 'Q', 'K', 'A' ]
const d2 =
[ '♤', '♡', '♧', '♢' ]
for (const [ rank, suit ] of combinations (d1, d2))
{
if (rank === 'K')
break
if (rank === 'J' || suit === '♡' )
console.log (rank, suit)
}
// J ♤
// J ♡
// J ♧
// J ♢
// Q ♡ <-- no Kings or Aces; generator stopped at K
for (const [ a, b, c, d, e ] of combinations (d2, d2, d2, d2, d2))
{
if (a === '♡' && e === '♡')
console.log (a, b, c, d, e)
}
// ♡ ♤ ♤ ♤ ♡
// ♡ ♤ ♤ ♡ ♡
// ♡ ♤ ♤ ♧ ♡
// ...
// ♡ ♢ ♢ ♡ ♡
// ♡ ♢ ♢ ♧ ♡
// ♡ ♢ ♢ ♢ ♡
const d1 =
[ 1, 2, 3, 4, 5, 6 ]
Array.from (combinations (d1, d1, d1, d1, d1, d1, d1)) .length
// 6^7 = 279936
Array.from (combinations (d1, d1, d1, d1, d1, d1, d1, d1)) .length
// 6^8 = 1679616
filter
函数。在下面,我们发现形成
Pythagorean triple的三个20边骰子的所有组合-3个整数组成一个有效直角三角形的边长
const filter = function* (f, iterable)
{
for (const x of iterable)
if (f (x))
yield x
}
const d20 =
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
const combs =
combinations (d20, d20, d20)
const pythagoreanTriple = ([ a, b, c ]) =>
(a * a) + (b * b) === (c * c)
for (const c of filter (pythagoreanTriple, combs))
console.log (c)
// [ 3, 4, 5 ]
// [ 4, 3, 5 ]
// [ 5, 12, 13 ]
// [ 6, 8, 10 ]
// [ 8, 6, 10 ]
// [ 8, 15, 17 ]
// [ 9, 12, 15 ]
// [ 12, 5, 13 ]
// [ 12, 9, 15 ]
// [ 12, 16, 20 ]
// [ 15, 8, 17 ]
// [ 16, 12, 20 ]
Array.from
与映射功能一起使用,以将每个组合同时转换为新结果并将所有结果收集到数组中
const allResults =
Array.from ( filter (pythagoreanTriple, combs)
, ([ a, b, c ], index) => ({ result: index + 1, solution: `${a}² + ${b}² = ${c}²`})
)
console.log (allResults)
// [ { result: 1, solution: '3² + 4² = 5²' }
// , { result: 2, solution: '4² + 3² = 5²' }
// , { result: 3, solution: '5² + 12² = 13²' }
// , ...
// , { result: 10, solution: '12² + 16² = 20²' }
// , { result: 11, solution: '15² + 8² = 17²' }
// , { result: 12, solution: '16² + 12² = 20²' }
// ]
const None =
Symbol ()
// Array Applicative
Array.prototype.ap = function (args)
{
const loop = (acc, [ x = None, ...xs ]) =>
x === None
? this.map (f => f (acc))
: x.chain (a => loop ([ ...acc, a ], xs))
return loop ([], args)
}
// Array Monad (this is the same as flatMap above)
Array.prototype.chain = function chain (f)
{
return this.reduce ((acc, x) => [ ...acc, ...f (x) ], [])
}
// Identity function
const identity = x =>
x
// math is programming is math is ...
const combinations = (...arrs) =>
[ identity ] .ap (arrs)
console.log (combinations ([ 0, 1 ], [ 'A', 'B' ], [ '♡', '♢' ]))
// [ [ 0, 'A', '♡' ]
// , [ 0, 'A', '♢' ]
// , [ 0, 'B', '♡' ]
// , [ 0, 'B', '♢' ]
// , [ 1, 'A', '♡' ]
// , [ 1, 'A', '♢' ]
// , [ 1, 'B', '♡' ]
// , [ 1, 'B', '♢' ]
// ]
关于javascript - javascript递归骰子组合并将结果存储在矩阵中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553944/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!