查找数组中的所有系列
如何找到所有系列的编号(至少具有 3 个连续值的数组组合,例如 [7,8,9])并且具有最长的值数?
从 [3,4,1,2,2] 它将是 2 - ([1,2,3,4] 两次,但忽略 [1,2,3]*2 和 [2,3,4]*2 )
从 [9,6,7,5,8] 它将是 1 - ([5,6,7,8,9])
从 [1,2,3,1,2] 它将是 4 ([1 ,2,3] * 3)
谢谢
编辑,
这样做的目的是计算婴儿床手中的跑数。只要计算的系列不与所有卡片重叠,数组中系列的顺序并不重要。
编辑 1
var $cards:Array = [9, 4, 3, 2, 2];
var $ranks:Array = [];
var $c:int;
for each ($c in $cards) {
if ($ranks[$c] == null) {
$ranks[$c] = 1;
}else {
$ranks[$c] ++;
}
}
这将创建一个数组 ($ranks),其中包含
此 I 中的 这些值 [2:2, 3:1, 4:1, 9:1]
将能够将 2,3 ad4 4 以下的值相乘并将它们乘以 3,所以我会得到 2*1*1 * 3
我现在试图弄清楚如何找到连续的值,并忽略那些不是的值't(如9)
How do I find number of all the series (combinations of an array that have at least 3 consecutive values, like [7,8,9]) and have the longest number of values?
from [3,4,1,2,2] it would be 2 - ([1,2,3,4] twice, but ignore [1,2,3]*2 and [2,3,4]*2)
from [9,6,7,5,8] it would be 1 - ([5,6,7,8,9])
from [1,2,3,1,2] it would be 4 ([1,2,3] * 3)
Thanks
edit
the point of this is to count runs in a crib hand. It doesn't matter what order the array has the series in, as long as the series counted don't overlap all cards.
edit 1
var $cards:Array = [9, 4, 3, 2, 2];
var $ranks:Array = [];
var $c:int;
for each ($c in $cards) {
if ($ranks[$c] == null) {
$ranks[$c] = 1;
}else {
$ranks[$c] ++;
}
}
this will create an array ($ranks) that will have these values [2:2, 3:1, 4:1, 9:1]
from this I will be able to multiply the values under 2,3 ad4 4 and multiply them by 3, so I would get 2*1*1 * 3
I'm trying to figure out now how to find the consecutive values, and ignore ones that aren't (like the 9)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要对值进行排序,并将所有重复项替换为它们自己的数组。 IE。
然后你会想要找到最大的连续序列并消除违规。
然后将所有数组的长度相乘即可得出分数。
一旦你清理了你的阵列,你就可以想到这样的公式。
弄清楚如何用代码编写它应该很有趣。
You want to order your values and replace all duplicates with an array of themselves. ie.
Then you will want to find the largest consecutive sequence and remove violations.
Then it will be a matter of multiplying the length of all of your arrays to find your score.
Once you have cleaned your array, you can think of the formula like this.
It should be pretty fun figuring out how to write this in code.
这有效:
它使用 casalib 来获取最小值/最大值,但还有其他方法可以解决它。不过,这个 onlu 找到了最大的连续数字集,因为它意味着计算最多有 5 张牌的婴儿床手牌,因此不可能同时存在两个系列(例如 2,3,4 和 9,10,11)
我通过 $seq 数组找到连续的数字,只要 $ranks[i] 有值就压入值,如果长度大于 3 且大于 $longest 数组,则将数组复制过来(使用 concat()!) ,如果没有值,则 $seq 被重置。
一旦你知道了,事情就这么简单......
编辑
我注意到代码中有一个拼写错误
if ($seq.length > $longest.length || $seq.length >= 2)
应该是
if ($seq.长度> $longest.length & $seq.length > 2)
this works:
it's using casalib for min/max, but there are other ways around it. this onlu finds the largest set of consecutive numbers though, as it is meant to count a crib hand which has a max of 5 cards, thus no two simultaneous series are possible (like 2,3,4 and 9,10,11)
I found the consecutive numbers through the $seq array, by pushing values whenever the $ranks[i] has a value, if the length is greater than 3 and grater than the $longest array, copy the array over (with concat()!), if there is no value, $seq gets reset.
it's so simple once you know...
edit
I noticed I had a typo in the code
if ($seq.length > $longest.length || $seq.length >= 2)
should have been
if ($seq.length > $longest.length && $seq.length > 2)