/
Skob.m.a
/
WorkSpace
Обзор
Документация
Войти
/
Skob.m.a
/
WorkSpace
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Task_32
63 строки
2 KB
Skob.m.a
create Task_32
22 дек 2024, 16:13
22 дек 2024, 16:13
99099e7
Код
Авторство
О чём код?
import java.util.Arrays; public class JohnsonTrotter { private int[] permutation; private int[] direction; private int n; public JohnsonTrotter(int n) { this.n = n; this.permutation = new int[n]; this.direction = new int[n]; for (int i = 0; i < n; i++) { permutation[i] = i + 1; direction[i] = -1; } } public void generatePermutations() { System.out.println(Arrays.toString(permutation)); while (true) { int largestMobileIndex = -1; int largestMobileValue = -1; for (int i = 0; i < n; i++) { int nextIndex = i + direction[i]; if (nextIndex >= 0 && nextIndex < n) { if (permutation[i] > permutation[nextIndex]) { if (permutation[i] > largestMobileValue) { largestMobileValue = permutation[i]; largestMobileIndex = i; } } } } if (largestMobileIndex == -1) break; int moveIndex = largestMobileIndex + direction[largestMobileIndex]; swap(largestMobileIndex, moveIndex); for (int i = 0; i < n; i++) { if (permutation[i] > largestMobileValue) { direction[i] = -direction[i]; } } System.out.println(Arrays.toString(permutation)); } } private void swap(int i, int j) { int temp = permutation[i]; permutation[i] = permutation[j]; permutation[j] = temp; } public static void main(String[] args) { int n = 3; JohnsonTrotter jt = new JohnsonTrotter(n); jt.generatePermutations(); } }