选择不同对的算法

huangapple go评论55阅读模式
英文:

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 with U larger than all y and L smaller than all y;
  • Of course you could do it the other way around instead, with K = max(x) - min(x) + 1, and F(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 with U larger than all y and L smaller than all y;
  • Of course you could do it the other way around instead, with K = max(x) - min(x) + 1, and F(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.

huangapple
  • 本文由 发表于 2023年2月18日 16:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定