여기이 스레드에서 논의 된 내용을 구현하려고 시도했습니다. Algorithm to apply permutation in constant memory space. 그러나 문제의 해결책을 올바르게 이해할 수 없거나 코드에 감지 할 수없는 버그가 있습니다. 앤트류의 도움을 주시면 감사하겠습니다.작동하도록 배열의 순열을 구현할 수 없습니다.
public class ArrayPermute{
public static void main(String[] args){
char[] arr = {'a','b','c','d'};
int[] p = {2,0,1,3};
for(int i=0; i<arr.length; i++){
int tmp = i;
while(p[tmp] >= 0){
char t = arr[p[tmp]];//d
arr[p[tmp]] = arr[tmp];
arr[tmp]=t;
int _tmp = p[tmp];
p[tmp] = -1;
tmp = _tmp;
print(arr);
print(p);
}
}
for(char i: arr){
System.out.print(i + " ");
}
}
public static void print(char[] arr){
for(char c: arr){
System.out.print(c + " ");
}
System.out.println();
}
public static void print(int[] arr){
for(int c: arr){
System.out.print(c + " ");
}
System.out.println();
}
}
문제가 정확히 무엇입니까? 잘못된 결과가 나옵니까? 그렇다면 무엇을 얻고 무엇을 기대합니까? – kraskevich
예, 출력이 잘못되었습니다. – wayfare