如何继续在数组上提供方法,直到它变得等于其他数组
How to continue providing method on array until it's become equal to other array
我有一个 int1[k] 数组,其值等于它的索引 {0,1,2,3...}
。
我还有一个 method1,它采用该数组和 returns 另一个 int2[k],其值以这种方式洗牌:
初始套牌:0 1 2 3 4 5 6 7
洗牌:4 0 5 1 6 2 7 3
最后我有一个 method2,它接受任何 int[k] 数组并计算 method1 的洗牌次数,以使其恢复到原始状态:
Shuffles Deck Order
0 0, 1, 2, 3, 4, 5, 6, 7
1 4, 0, 5, 1, 6, 2, 7, 3
2 6, 4, 2, 0, 7, 5, 3, 1
3 7, 6, 5, 4, 3, 2, 1, 0
4 3, 7, 2, 6, 1, 5, 0, 4
5 1, 3, 5, 7, 0, 2, 4, 6
6 0, 1, 2, 3, 4, 5, 6, 7
这里有6次。
在最后的方法中,我想 运行 do{}while()
以条件 "until new array is not equal to original itself" 循环,尽管 System.out.printing 表明数组在每次迭代中都在变化(并变得等于原始状态)条件相等永远不会变为真。
public class PerfectShuffle {
private int[] deck;
public PerfectShuffle(int size) {
this.deck = new int[size];
for (int i = 0; i < size; i++) {
this.deck[i] = i;
}
}
public int[] method1(int[] input) {
int[] newDeck = new int[input.length];
int[] input1 = new int[input.length/2];
int[] input2 = new int[input.length/2];
System.arraycopy(input, 0, input1, 0, input.length/2);
System.arraycopy(input, input.length/2 - 1, input2, 0, input.length/2);
for (int i = 0; i < input.length/2; i++){
newDeck[i*2 + 1] = input1[i];
newDeck[i*2] = input2[i];
}
return newDeck;
}
public int method2() {
int[] tempDeck = this.deck;
int count = 0;
do {
tempDeck = this.method1(tempDeck);
count++;
System.out.println(Arrays.toString(tempDeck));
} while (!Arrays.equals(tempDeck, this.deck));
return count;
}
}
public class Main {
public static void main(String[] args) {
PerfectShuffle s = new PerfectShuffle(52);
System.out.println( s.method2() );
}
}
我期待一个数字,但只是 "thinks" 了很长时间。
这一行是错误的:System.arraycopy(input, input.length/2 - 1, input2, 0, input.length/2);
。它有一个 off-by-one 错误。
源数组起始索引应为input.length/2
。
我有一个 int1[k] 数组,其值等于它的索引 {0,1,2,3...}
。
我还有一个 method1,它采用该数组和 returns 另一个 int2[k],其值以这种方式洗牌:
初始套牌:0 1 2 3 4 5 6 7
洗牌:4 0 5 1 6 2 7 3
最后我有一个 method2,它接受任何 int[k] 数组并计算 method1 的洗牌次数,以使其恢复到原始状态:
Shuffles Deck Order
0 0, 1, 2, 3, 4, 5, 6, 7
1 4, 0, 5, 1, 6, 2, 7, 3
2 6, 4, 2, 0, 7, 5, 3, 1
3 7, 6, 5, 4, 3, 2, 1, 0
4 3, 7, 2, 6, 1, 5, 0, 4
5 1, 3, 5, 7, 0, 2, 4, 6
6 0, 1, 2, 3, 4, 5, 6, 7
这里有6次。
在最后的方法中,我想 运行 do{}while()
以条件 "until new array is not equal to original itself" 循环,尽管 System.out.printing 表明数组在每次迭代中都在变化(并变得等于原始状态)条件相等永远不会变为真。
public class PerfectShuffle {
private int[] deck;
public PerfectShuffle(int size) {
this.deck = new int[size];
for (int i = 0; i < size; i++) {
this.deck[i] = i;
}
}
public int[] method1(int[] input) {
int[] newDeck = new int[input.length];
int[] input1 = new int[input.length/2];
int[] input2 = new int[input.length/2];
System.arraycopy(input, 0, input1, 0, input.length/2);
System.arraycopy(input, input.length/2 - 1, input2, 0, input.length/2);
for (int i = 0; i < input.length/2; i++){
newDeck[i*2 + 1] = input1[i];
newDeck[i*2] = input2[i];
}
return newDeck;
}
public int method2() {
int[] tempDeck = this.deck;
int count = 0;
do {
tempDeck = this.method1(tempDeck);
count++;
System.out.println(Arrays.toString(tempDeck));
} while (!Arrays.equals(tempDeck, this.deck));
return count;
}
}
public class Main {
public static void main(String[] args) {
PerfectShuffle s = new PerfectShuffle(52);
System.out.println( s.method2() );
}
}
我期待一个数字,但只是 "thinks" 了很长时间。
这一行是错误的:System.arraycopy(input, input.length/2 - 1, input2, 0, input.length/2);
。它有一个 off-by-one 错误。
源数组起始索引应为input.length/2
。