在卡片应用程序中,我使用 0-51 来表示 5 张牌。

西装是card / 13排名是 card %只有 4 种可能的花色(黑桃、红心、俱乐部钻石)

如果所有五个花色都相同,则为同花顺。所有冲洗都具有相同的值(value)。黑桃与钻石相同。

我知道你会说预优化是邪恶的,但我正在运行一些模拟做这百万次,这是最昂贵的一步。卡片可能是字节,但使用 int 的计算似乎更快。我并没有真正检查范围,但我把它放进去,所以你会知道有一个范围。

有没有更有效的方法?

public bool IsFlush(int[] quick)
{
    HashSet<int> suit = new HashSet<int>();
    suit.Add(quick[0] / 13);
    int thisQuick;
    for (int i = 1; i < quick.Length; i ++)
    {
        thisQuick = quick[i];
        if (thisQuick < 0 || thisQuick > 51)
            throw new IndexOutOfRangeException();
        if (suit.Add(thisQuick / 13))
            return false;
    }
    return true;
}

最佳答案

消除 HashSet 应该会加快速度:

public static bool IsFlush(int[] hand)
{
    int firstSuit = hand[0] / 13;
    for (int i = 1; i < hand.Length; i++)
    {
        int card = hand[i];
        if (card < 0 || card > 51)
            throw new IndexOutOfRangeException();
        if (firstSuit != (card / 13))
            return false;
    }
    return true;
}

我的(诚然微不足道的)测试表明性能提高了大约 20%。

关于c# - 确定一张扑克牌同花手,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43242270/

10-13 03:11