英文:
Algorithm for selecting the differents pair
问题
可以有人帮我吗?
所以,我有一组成对的值 x0-y0, x1-y1,等等。
并且始终 x[i] < y[i]。因此,我需要一个函数(或算法)用于每一对,
使得 F(x[i],y[i]) = result[i],每个特定对的结果必须是一个整数唯一值。
英文:
May be somebody can help me with it.
So, I have a set of pairs x0-y0, x1-y1, etc.
And always x[i]<y[i]. Thus I need a function (or algorithm) for every pair, so
F(x[i],y[i]) = result[i], and each result for a particular pair must be an integer unique value.
答案1
得分: 2
Let M = max(y) - min(y) + 1
, then use the formula:
F(x, y) = x * M + y
Remarks:
- You don't have to use the exact maximum and minimum, you can use an upperbound and a lowerbound,
M = U - L + 1
withU
larger than all y andL
smaller than ally
; - Of course you could do it the other way around instead, with
K = max(x) - min(x) + 1
, andF(x, y) = y * K + x
; - When using a finite integer type, be careful with overflow, for instance if x and y both have values larger than 46340, then F(x, y) won't fit in a 32-bit signed integer.
英文:
Let M = max(y) - min(y) + 1
, then use the formula:
F(x, y) = x * M + y
Remarks:
- You don't have to use the exact maximum and minimum, you can use an upperbound and a lowerbound,
M = U - L + 1
withU
larger than all y andL
smaller than ally
; - Of course you could do it the other way around instead, with
K = max(x) - min(x) + 1
, andF(x, y) = y * K + x
; - When using a finite integer type, be careful with overflow, for instance if x and y both have values larger than 46340, then F(x, y) won't fit in a 32-bit signed integer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论