算法-将n C 4整数映射到[1,n]内的唯一值

时间:2021-12-09 12:05:07

So to demonstrate what I'm hoping to achieve I'll use a deck of cards.

为了演示我希望达到的效果,我将使用一副牌。

Let's say there are three people, each with their own randomly shuffled deck. The cards in the deck simply have values 1 through 13, and there are four of each card.

假设有三个人,每个人都有自己的随机洗牌。牌中的牌只值1到13,每张牌有4张。

When it comes time to draw a card, everyone takes their top card and shows its value to the other two players.

当需要抽牌的时候,每个人都拿着他们的最高卡,并向另外两名球员展示它的价值。

What I want now, is some way to map the values of each of these top cards to a single integer from 1 - 13. The goal being that this algorithm would generate something unique for each operation, and only allow for 4 of the same value (when the exact same inputs are calculated each of 4 times it can happen).

我现在想要的是,从1到13,将这些*牌的值映射到一个整数。这个算法的目标是为每个操作生成一个独特的东西,并且只允许4个相同的值(当相同的输入被计算出4倍的值时)。

I know I can use Cantor Pairing Function to generate a unique value, but again I want it to be in the range 1-13.

我知道我可以使用Cantor配对函数来生成一个唯一的值,但我还是希望它在1-13的范围内。

1 个解决方案

#1


0  

The Cantor pairing function is only necessary if you want to map all positive integers. What's wrong with simply the following, with n = 13 and N = n^4?

如果你想映射所有正整数,那么康托配对函数是唯一必要的。只是下面的,有什么问题与n = 13 n = n ^ 4 ?

(val1 - 1) * n^3 + (val2 - 1) * n^2 + (val3 - 1) * n + (val4 - 1) + 1

The ordering can be given by the suit of the card, or the order in which they are drawn.

订购可以由卡片的套装,或他们所绘制的顺序。

#1


0  

The Cantor pairing function is only necessary if you want to map all positive integers. What's wrong with simply the following, with n = 13 and N = n^4?

如果你想映射所有正整数,那么康托配对函数是唯一必要的。只是下面的,有什么问题与n = 13 n = n ^ 4 ?

(val1 - 1) * n^3 + (val2 - 1) * n^2 + (val3 - 1) * n + (val4 - 1) + 1

The ordering can be given by the suit of the card, or the order in which they are drawn.

订购可以由卡片的套装,或他们所绘制的顺序。