【问题标题】:How to write Szudzik's function in java如何在java中编写Szudzik的函数
【发布时间】:2019-05-17 01:52:28
【问题描述】:

如何用 Java 编写 Szudzik 的函数?

Szudzik 函数是将两个数字配对以给出唯一数字的函数。

Mapping two integers to one, in a unique and deterministic way

IMPLEMENTATION OF RHIZOMES

不是用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


    【解决方案1】:

    如果我理解正确,那么你需要这样的东西:

    public static int pair(int x, int y) {
        return x >= y ? x * x + x + y : y * y + x;
    }
    
    public static Pair<Integer, Integer> unpair(int z) {
        int b = (int) Math.sqrt(z);
        int a = z - b * b;
        return a < b ? new Pair<>(a, b) : new Pair<>(b, a - b);  
    }
    

    unpair 方法返回整数的Pair
    所以你需要这个:

    import javafx.util.Pair;
    

    所以这个:

    public static void main(String[] args) {
        int x = 5;
        int y = 12;
        int uniqueNumber = pair(x, y);
        System.out.println("For x = " + x + " and y = " + y + " the unique number is: " + uniqueNumber);
    
        Pair<Integer, Integer> p = unpair(uniqueNumber);
        x = p.getKey();
        y = p.getValue();
        System.out.println("For the unique number " + uniqueNumber + ": x = " + x + " and y = " + y);
    }
    

    将打印:

    For x = 5 and y = 12 the unique number is: 149
    For the unique number 149: x = 5 and y = 12
    

    编辑
    没有 Pair,有数组:

    public static int[] unpair(int z) {
        int b = (int) Math.sqrt(z);
        int a = z -b * b;
        return a < b ? new int[] {a, b} : new int[] {b, a - b};
    }
    
    public static void main(String[] args) {
        int x = 5;
        int y = 12;
        int uniqueNumber = pair(x, y);
        System.out.println("For x = " + x + " and y = " + y + " the unique number is: " + uniqueNumber);
    
        int[] p = unpair(uniqueNumber);
        x = p[0];
        y = p[1];
        System.out.println("For the unique number " + uniqueNumber + ": x = " + x + " and y = " + y);
    }
    

    来源:http://szudzik.com/ElegantPairing.pdf

    【讨论】:

    • 你能检查更新的问题吗,这就是我想要的 Szudzik 配对功能解决方案。
    • @KingAmada 检查如何使用我的方法是我编辑的答案。当然,您可以更改名称。
    • (p.getKey & p.getValue) 抛出错误“找不到符号”我正在使用 Netbeans 9. 和 Java 11
    • 你添加了这个:import javafx.util.Pair;
    • 我使用 (p.fst & p.snd) 而不是 (p.getKey & p.getValue)。但是当我使用非负整数时,它并没有带回相同的对。这意味着某处存在错误,因为 Szudzik 函数适用于正整数和负整数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2011-01-08
    • 1970-01-01
    • 2014-01-20
    • 2017-08-22
    • 2018-01-28
    相关资源
    最近更新 更多