Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Hilbert Curve
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { System.out.println(xy2d(1, 1, 2)); // expected: 2 System.out.println(xy2d(5, 6, 3)); // expected: 39 System.out.println(xy2d(1, 5, 3)); // expected: 18 System.out.println(Arrays.toString(d2xy(2, 2))); // expected: 2 System.out.println(Arrays.toString(d2xy(39, 3))); // expected: 1 System.out.println(Arrays.toString(d2xy(18, 3))); // expected: 18 } private static int xy2d(int x, int y, int n) { if (n == 0) return 0; int count = 1 << (2 * (n - 1)); // steps to go through a quater square int len = 1 << (n - 1); // size of quater square if (x >= len && y >= len) return 2*count + xy2d(x - len, y - len, n - 1); else if (x < len && y >= len) return count + xy2d(x, y - len, n - 1); else if (x < len && y < len) return xy2d(y, x, n - 1); else return 3*count + xy2d(len - 1 - y, 2*len - 1 - x, n - 1); } private static int[] d2xy(int d, int n) { if (n == 0) return new int[] {0, 0}; int count = 1 << (2 * (n - 1)); // steps to go through a quater square int len = 1 << (n - 1); // size of quater square if (d < count) { int[] res = d2xy(d, n - 1); return new int[] {res[1], res[0]}; } else if (d < 2*count) { int[] res = d2xy(d - count, n - 1); return new int[] {res[0], res[1] + len}; } else if (d < 3*count) { int[] res = d2xy(d - 2*count, n - 1); return new int[] {res[0] + len, res[1] + len}; } else { int[] res = d2xy(d - 3*count, n - 1); return new int[] {-res[1] + 2*len - 1, -res[0] + len - 1}; } } }
run
|
edit
|
history
|
help
0
Sorted Array
exp5
Airbnb-QueueLinkedList-2/3
ElaineBrown**
kjhkj
상속1
Bubble Sort Java 2
jb12.0 threads.enums
Oh no
Arredondamento de datas para dias 10, 20 e 30