Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 7256c25

Browse files
committed
Лабораторная работа №32
1 parent 49ea3b4 commit 7256c25

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

students/23K0815/23K0815-p32/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p32</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.Arrays;
4+
5+
public class JohnsonTrotter {
6+
private int[] permutation; // Текущая перестановка
7+
private int[] directions; // Направления элементов
8+
private int n; // Количество элементов
9+
10+
public JohnsonTrotter(int n) {
11+
this.n = n;
12+
this.permutation = new int[n];
13+
this.directions = new int[n];
14+
for (int i = 0; i < n; i++) {
15+
permutation[i] = i + 1; // Заполнение перестановки числами от 1 до n
16+
directions[i] = -1; // Изначально все элементы направлены влево
17+
}
18+
}
19+
20+
public void generatePermutations() {
21+
boolean morePermutations = true;
22+
23+
while (morePermutations) {
24+
// Печать текущей перестановки
25+
System.out.println(Arrays.toString(permutation));
26+
27+
// Найти наибольший элемент, который может двигаться
28+
int largestMobileIndex = -1;
29+
int largestMobileValue = -1;
30+
31+
for (int i = 0; i < n; i++) {
32+
if (isMobile(i)) {
33+
if (permutation[i] > largestMobileValue) {
34+
largestMobileValue = permutation[i];
35+
largestMobileIndex = i;
36+
}
37+
}
38+
}
39+
40+
// Если не осталось мобильных элементов, завершить
41+
if (largestMobileIndex == -1) {
42+
morePermutations = false;
43+
} else {
44+
// Перемещение наибольшего мобильного элемента
45+
move(largestMobileIndex);
46+
47+
// Изменение направления перемещенного элемента
48+
int movedValue = permutation[largestMobileIndex];
49+
for (int i = 0; i < n; i++) {
50+
if (permutation[i] > movedValue) {
51+
directions[i] = -directions[i]; // Изменяем направление
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
private boolean isMobile(int index) {
59+
int direction = directions[index];
60+
int newIndex = index + direction;
61+
62+
// Проверка, находится ли новый индекс в пределах массива
63+
return newIndex >= 0 && newIndex < n && permutation[newIndex] < permutation[index];
64+
}
65+
66+
private void move(int index) {
67+
int direction = directions[index];
68+
int newIndex = index + direction;
69+
70+
// Меняем местами элементы
71+
int temp = permutation[index];
72+
permutation[index] = permutation[newIndex];
73+
permutation[newIndex] = temp;
74+
}
75+
76+
public static void main(String[] args) {
77+
int n = 3; // Задайте количество элементов
78+
JohnsonTrotter jt = new JohnsonTrotter(n);
79+
jt.generatePermutations();
80+
}
81+
}

students/23K0815/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
<module>23K0815-p29</module>
4040
<module>23K0815-p30</module>
4141
<module>23K0815-p31</module>
42+
<module>23K0815-p32</module>
4243
</modules>
4344
</project>

0 commit comments

Comments
 (0)