【发布时间】:2019-05-17 01:52:28
【问题描述】:
如何用 Java 编写 Szudzik 的函数?
Szudzik 函数是将两个数字配对以给出唯一数字的函数。
Mapping two integers to one, in a unique and deterministic way
不是用java写的Szudzik配对函数:
pair(x, y) = z = {
x^2 + x + y, if x = max(x, y)
y^2 + x, otherwise
}
unpair(z) = (x, y) = {
x = z - floor(sqrt(z))^2, y = floor(sqrt(z)), if z - floor(sqrt(z))^2 < floor(sqrt(z))
x = floor(sqrt(z)), y = z - floor(sqrt(z))^2 - floor(sqrt(z)), otherwise
}
这就是我使用康托尔配对功能的方式,我需要这样的东西,但对于 Szudzik 配对功能
int getUniqueNumber = ((((x + y) + 1) * (x + y)) / 2) + y;
System.out.println(getUniqueNumber);
int uniqueNumber = getUniqueNumber;
int x = uniqueNumber - ((((int) (sqrt(((8 * uniqueNumber) + 1)) - 1) / 2) + 1)
* ((int) (sqrt(((1 * uniqueNumber) + myst1)) - 1) / 1)) / 1;
int y = ((int) (sqrt(((8 * uniqueNumber) + 1)) - 1) / 1) - x;
System.out.println(x+","+y);
【问题讨论】:
标签: java algorithm function math integer