我試圖整數整數和整數整數,到目前爲止我的成功很少。整數整數與整數的整數
我具有從0 18個整數對於欲洗牌
int[] chunks = {2, 3, 10, 3};
整數的陣列開始像0整數的每個組塊的陣列,以17
array = new int[18];
for(int i = 0; i < 18; i++){
array[i] = i;
}
和陣列, 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
對於第二塊數組的元素,它的值爲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,1 2,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);
}
任何幫助將不勝感激,如果你需要我澄清任何事情讓我知道。
謝謝
如果您可以與我們分享您迄今嘗試過的代碼,可能會有幫助嗎? –
即使是你的for循環中的語法錯誤,也不會編譯,因爲你想把一個'Array'分配給一個'int'的最後一行('array [']'是一個'int' ) –
是的你是對的,這是我的錯。我的意思是放入第一個而不是數組 – user221