查找数组中的所有系列

发布于 2024-10-10 07:30:17 字数 746 浏览 0 评论 0原文

如何找到所有系列的编号(至少具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-10-17 07:30:17

您想要对值进行排序,并将所有重复项替换为它们自己的数组。 IE。

//Order values and group matches
[3,4,1,2,2] = [1,[2,2],3,4]
[9,6,7,5,8] = [5,6,7,8,9]
[1,2,3,1,2] = [[1,1],[2,2],3]

然后你会想要找到最大的连续序列并消除违规。

//remove violations (6,7,8)... which I guess you also want to count separately.
[1,[2,2],3,4,6,7,8] = [1,[2,2],3,4]

然后将所有数组的长度相乘即可得出分数。

一旦你清理了你的阵列,你就可以想到这样的公式。

array2 = [2,2];
array1 = [1, array2, 3, 4];
score = array1.length * array2.length = 8;

array3 = [3,3,3];
array2 = [2,2];
array1 = [1, array2, array3, 4];
score = array1.length * array2.length * array3.length = 24;

弄清楚如何用代码编写它应该很有趣。

You want to order your values and replace all duplicates with an array of themselves. ie.

//Order values and group matches
[3,4,1,2,2] = [1,[2,2],3,4]
[9,6,7,5,8] = [5,6,7,8,9]
[1,2,3,1,2] = [[1,1],[2,2],3]

Then you will want to find the largest consecutive sequence and remove violations.

//remove violations (6,7,8)... which I guess you also want to count separately.
[1,[2,2],3,4,6,7,8] = [1,[2,2],3,4]

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.

array2 = [2,2];
array1 = [1, array2, 3, 4];
score = array1.length * array2.length = 8;

array3 = [3,3,3];
array2 = [2,2];
array1 = [1, array2, array3, 4];
score = array1.length * array2.length * array3.length = 24;

It should be pretty fun figuring out how to write this in code.

弥繁 2024-10-17 07:30:17

这有效:
它使用 casalib 来获取最小值/最大值,但还有其他方法可以解决它。不过,这个 onlu 找到了最大的连续数字集,因为它意味着计算最多有 5 张牌的婴儿床手牌,因此不可能同时存在两个系列(例如 2,3,4 和 9,10,11)

private function countRuns($cards:Array):int {
    var $ranks:Array = [];
    var $c:int;

    for each ($c in $cards) {
        if ($ranks[$c] == null) {
            $ranks[$c] = 1;
        }else {
            $ranks[$c] ++;
        }
    }

    var $highest:int = ArrayUtil.getHighestValue($cards);
    var $lowest:int = ArrayUtil.getLowestValue($cards);
    var $seq:Array = [];
    var $longest:Array = [];
    for (var i:int = $lowest; i <= $highest; i++) {
        if ($ranks[i] != null) {
            $seq.push($ranks[i]);
            if ($seq.length > $longest.length && $seq.length > 2) {
                $longest = $seq.concat();
            }
        }else {
            $seq = [];
        }
    }

    var $total:int = $longest.length;
    for each ($c in $longest) {
        $total *= $c;
    }
    trace($total, $cards);
    return $total;
}

我通过 $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)

private function countRuns($cards:Array):int {
    var $ranks:Array = [];
    var $c:int;

    for each ($c in $cards) {
        if ($ranks[$c] == null) {
            $ranks[$c] = 1;
        }else {
            $ranks[$c] ++;
        }
    }

    var $highest:int = ArrayUtil.getHighestValue($cards);
    var $lowest:int = ArrayUtil.getLowestValue($cards);
    var $seq:Array = [];
    var $longest:Array = [];
    for (var i:int = $lowest; i <= $highest; i++) {
        if ($ranks[i] != null) {
            $seq.push($ranks[i]);
            if ($seq.length > $longest.length && $seq.length > 2) {
                $longest = $seq.concat();
            }
        }else {
            $seq = [];
        }
    }

    var $total:int = $longest.length;
    for each ($c in $longest) {
        $total *= $c;
    }
    trace($total, $cards);
    return $total;
}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文