用整数块混洗整数数组
Shuffling array of ints with chunks of ints
我正在尝试用块对整数数组进行洗牌,但到目前为止我收效甚微。
我有一个从 0 到 17 的 18 个整数的数组
array = new int[18];
for(int i = 0; i < 18; i++){
array[i] = i;
}
以及我要打乱的每个整数块的数组
int[] chunks = {2, 3, 10, 3};
整数数组以 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 开头
选择 2 的块数组的第一个元素时,将选择数组的前 2 个元素 0 和 1。 1 被移动到数组的末尾,0 紧随其后。
现在顺序是2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,0,1
对于 chunks 数组的第二个元素,它的值为 3,因此选择 2、3、4 并将其放在 0 和 1 的顶部。
现在顺序是5,6,7,8,9,10,11,12,13,14,15,16,17,2,3,4,0,1
这一直持续到整数数组与所有块完全混洗
完全打乱块的数组是15,16,17,5,6,7,8,9,10,11,12,13,14,2,3,4,0,1
研究
到目前为止我尝试编程的方法是
for(int i = 0; i < chunks[0] i++) {
int first = array[0];
System.arraycopy(array, 1, array, 0, array.length-1);
array[array.length - 1] = first;
}
更新
这是我现在使用的代码,我得到了以下结果
10、11、12、13、14、15、3、4、5、6、7、8、9、1、1、1、1、1,而不是
15,16,17,5,6,7,8,9,10,11,12,13,14,2,3,4,0,1
int[] array2 = array.clone();
int temp = array2[0];
int chunkIndex = 0;
int count = 0;
int chunkCount = 0;
//Loops 4 times
for(int i = 0; i < chunks.length; i++) {
//Loops as many times for the chunk element
while(chunks[i] > count) {
for(int k = 1; k < array2.length; k++) {
//Move all the elements back
array2[k-1] = array2[k];
}
chunkIndex = array2.length-1 - chunkCount;
array2[chunkIndex] = temp;
temp = array2[0];
count++;
}
//Increment the limit
chunkCount += chunks[i];
//System.out.println(chunkCount);
}
任何帮助将不胜感激,如果您需要我澄清任何事情,请告诉我。
谢谢
我不会给你代码,因为这是一个很好的练习。
但首先,不要一次又一次地复制数组。
一个基本的解决方案是:
1 - Read the chunk length
2 - Save the first value in a temp variable
3 - Shift every value on the left
4 - Put the temp variable at the end
这是一种在数组中移动值的简单方法。
现在,对于每个块,你需要将值放在最后一个之前,所以你只需要记住这个块的 index
在哪里。这只是 lastIndex
- chunk length
。这将是移动的限制以及放置临时值的位置。
有了这个,您可以轻松编写出在最佳状态下运行的解决方案。
如果块读取很多值(如果你想在 12 个单元格的数组中有 13 个项目。
编辑:
1 - 你应该在移动数组之前保存 temp
,你先覆盖值然后尝试获取它。
2 - 你总是在移动整个数组,所以你要在移动完第一个块后移动它。如果您需要一个 index
将递减
int endIndex = array2.length; //### 2 ###
while(chunks[i] > count) {
temp = array2[0]; //### 1 ###
for(int k = 1; k < endIndex; k++) { //### 2 ###
//Move all the elements back
array2[k-1] = array2[k];
}
...
}
endIndex -= chunks[i];
应该会好一点,可惜我没时间测试
我正在尝试用块对整数数组进行洗牌,但到目前为止我收效甚微。
我有一个从 0 到 17 的 18 个整数的数组
array = new int[18];
for(int i = 0; i < 18; i++){
array[i] = i;
}
以及我要打乱的每个整数块的数组
int[] chunks = {2, 3, 10, 3};
整数数组以 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 开头 选择 2 的块数组的第一个元素时,将选择数组的前 2 个元素 0 和 1。 1 被移动到数组的末尾,0 紧随其后。
现在顺序是2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,0,1
对于 chunks 数组的第二个元素,它的值为 3,因此选择 2、3、4 并将其放在 0 和 1 的顶部。
现在顺序是5,6,7,8,9,10,11,12,13,14,15,16,17,2,3,4,0,1
这一直持续到整数数组与所有块完全混洗
完全打乱块的数组是15,16,17,5,6,7,8,9,10,11,12,13,14,2,3,4,0,1
研究
到目前为止我尝试编程的方法是
for(int i = 0; i < chunks[0] i++) {
int first = array[0];
System.arraycopy(array, 1, array, 0, array.length-1);
array[array.length - 1] = first;
}
更新
这是我现在使用的代码,我得到了以下结果 10、11、12、13、14、15、3、4、5、6、7、8、9、1、1、1、1、1,而不是 15,16,17,5,6,7,8,9,10,11,12,13,14,2,3,4,0,1
int[] array2 = array.clone();
int temp = array2[0];
int chunkIndex = 0;
int count = 0;
int chunkCount = 0;
//Loops 4 times
for(int i = 0; i < chunks.length; i++) {
//Loops as many times for the chunk element
while(chunks[i] > count) {
for(int k = 1; k < array2.length; k++) {
//Move all the elements back
array2[k-1] = array2[k];
}
chunkIndex = array2.length-1 - chunkCount;
array2[chunkIndex] = temp;
temp = array2[0];
count++;
}
//Increment the limit
chunkCount += chunks[i];
//System.out.println(chunkCount);
}
任何帮助将不胜感激,如果您需要我澄清任何事情,请告诉我。
谢谢
我不会给你代码,因为这是一个很好的练习。
但首先,不要一次又一次地复制数组。
一个基本的解决方案是:
1 - Read the chunk length
2 - Save the first value in a temp variable
3 - Shift every value on the left
4 - Put the temp variable at the end
这是一种在数组中移动值的简单方法。
现在,对于每个块,你需要将值放在最后一个之前,所以你只需要记住这个块的 index
在哪里。这只是 lastIndex
- chunk length
。这将是移动的限制以及放置临时值的位置。
有了这个,您可以轻松编写出在最佳状态下运行的解决方案。
如果块读取很多值(如果你想在 12 个单元格的数组中有 13 个项目。
编辑:
1 - 你应该在移动数组之前保存 temp
,你先覆盖值然后尝试获取它。
2 - 你总是在移动整个数组,所以你要在移动完第一个块后移动它。如果您需要一个 index
将递减
int endIndex = array2.length; //### 2 ###
while(chunks[i] > count) {
temp = array2[0]; //### 1 ###
for(int k = 1; k < endIndex; k++) { //### 2 ###
//Move all the elements back
array2[k-1] = array2[k];
}
...
}
endIndex -= chunks[i];
应该会好一点,可惜我没时间测试