问题
Task is to find the permutations of numbers {1,2,3...N}. N <=16. The problem is in conditions: each number must be used only one time and permutation should be of N-size . And the each permutation should be array, because it will be tested.
As input is given one permutation. The main task is to find all the permutations, that have the same set of difference as the first one has. For example, is given
12 3 6 1 9 11 8 5 2 4 7 10
Difference is -9,3,-5,8,2,-3,-3,-3,2,3,10
So, there are 12 permutations, that have the same permutation of difference.
3 11 8 5 2 4 6 9 12 7 10 1
3 6 9 11 8 5 2 4 12 7 10 1
4 12 7 10 1 3 6 9 11 8 5 2
4 7 10 12 3 6 1 9 11 8 5 2
4 6 9 12 7 10 1 3 11 8 5 2
6 9 12 7 10 1 3 11 8 5 2 4
9 11 8 5 2 10 12 3 6 1 4 7
11 8 5 2 10 12 3 6 1 4 7 9
11 8 5 2 4 12 7 10 1 3 6 9
11 8 5 2 4 7 10 12 3 6 1 9
12 3 6 1 9 11 8 5 2 4 7 10
12 3 6 1 4 7 9 11 8 5 2 10
I have wrote the checking of each permutation, but now I can`t correctly find all the permutations(with no repeating numbers) to check them.
Here it is
in
- permutation of N, de
- permutation of difference
boolean Check(int[] in, int[] de){
for(int i=0; i<de.length;i++){
if(in[0]+de[i]==in[1]){
int[] in2=new int[in.length-1];
if(in2.length==1){
return true; }
for(int j=1; j<in.length;j++){
in2[j-1]=in[j];
}
int[] de2=new int[de.length-1];
for(int j=0; j<de.length;j++){
if(de[j]!=de[i]){
de2[j]=de[j];}}
Check(in2,de2);
return false;
}
return false;
}
return false;
}
来源:https://stackoverflow.com/questions/22388138/creating-combination-of-numbers