Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
33
Task/Perfect-shuffle/Java/perfect-shuffle.java
Normal file
33
Task/Perfect-shuffle/Java/perfect-shuffle.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class PerfectShuffle {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] sizes = {8, 24, 52, 100, 1020, 1024, 10_000};
|
||||
for (int size : sizes)
|
||||
System.out.printf("%5d : %5d%n", size, perfectShuffle(size));
|
||||
}
|
||||
|
||||
static int perfectShuffle(int size) {
|
||||
if (size % 2 != 0)
|
||||
throw new IllegalArgumentException("size must be even");
|
||||
|
||||
int half = size / 2;
|
||||
int[] a = IntStream.range(0, size).toArray();
|
||||
int[] original = a.clone();
|
||||
int[] aa = new int[size];
|
||||
|
||||
for (int count = 1; true; count++) {
|
||||
System.arraycopy(a, 0, aa, 0, size);
|
||||
|
||||
for (int i = 0; i < half; i++) {
|
||||
a[2 * i] = aa[i];
|
||||
a[2 * i + 1] = aa[i + half];
|
||||
}
|
||||
|
||||
if (Arrays.equals(a, original))
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue