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

Commit a4af6c9

Browse files
committed
Лабораторная №32
1 parent 50be76d commit a4af6c9

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

students/23K0120/23K0120-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>23K0120</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0120-p32</artifactId>
12+
<description>Combinatorics</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0120;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать девятая практическая работа!");
11+
}
12+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Combinatorics {
7+
private static List<Integer> undirectIntegers(List<DirectedInteger> directedIntegers) {
8+
List<Integer> integers = new ArrayList<>();
9+
for (DirectedInteger directedInteger : directedIntegers) {
10+
integers.add(directedInteger.getValue());
11+
}
12+
return integers;
13+
}
14+
15+
private int maxInd(List<DirectedInteger> directedIntegers) {
16+
int maxInd = 0;
17+
for (int i = 0; i < directedIntegers.size(); i++) {
18+
if (directedIntegers.get(i).getValue() > directedIntegers.get(maxInd).getValue()) {
19+
maxInd = i;
20+
}
21+
}
22+
return maxInd;
23+
}
24+
25+
private int maxMobileInd(List<DirectedInteger> directedIntegers) {
26+
int maxInd = -1;
27+
int size = directedIntegers.size();
28+
int maxUndirInd = maxInd(directedIntegers);
29+
if (maxUndirInd != 0 && maxUndirInd != size - 1
30+
|| maxUndirInd == 0 && directedIntegers.get(maxUndirInd).getDirection() == Direction.RIGHT
31+
|| maxUndirInd == size - 1 && directedIntegers.get(maxUndirInd).getDirection() == Direction.LEFT) {
32+
return maxUndirInd;
33+
}
34+
for (int i = 0; i < size; i++) {
35+
DirectedInteger dirInt = directedIntegers.get(i);
36+
if (i != 0 && i != size - 1) {
37+
int indDirIntFacing = dirInt.getDirection() == Direction.LEFT ? i - 1 : i + 1;
38+
DirectedInteger dirIntFacing = directedIntegers.get(indDirIntFacing);
39+
if (dirInt.getValue() > dirIntFacing.getValue()) {
40+
if (maxInd == -1) {
41+
maxInd = i;
42+
} else {
43+
DirectedInteger maxDirInt = directedIntegers.get(maxInd);
44+
if (dirInt.getValue() > maxDirInt.getValue()) {
45+
maxInd = i;
46+
}
47+
}
48+
}
49+
} else if (i == size - 1 && dirInt.getDirection() == Direction.LEFT && directedIntegers.get(i - 1).getValue() < dirInt.getValue()) {
50+
maxInd = i;
51+
} else if (i == 0 && dirInt.getDirection() == Direction.RIGHT && directedIntegers.get(1).getValue() < dirInt.getValue()) {
52+
maxInd = i;
53+
}
54+
}
55+
return maxInd;
56+
}
57+
58+
public List<List<Integer>> permutations(int n) {
59+
List<DirectedInteger> directedIntegers = new ArrayList<>();
60+
for (int i = 0; i < n; i++) {
61+
directedIntegers.add(new DirectedInteger(i + 1, Direction.LEFT));
62+
}
63+
List<List<Integer>> permutations = new ArrayList<>();
64+
permutations.add(undirectIntegers(directedIntegers));
65+
while (true) {
66+
int ind = maxMobileInd(directedIntegers);
67+
if (ind == -1) {
68+
break;
69+
}
70+
DirectedInteger maxDirInt = directedIntegers.get(ind);
71+
int tempInd = maxDirInt.getDirection() == Direction.LEFT ? ind - 1 : ind + 1;
72+
DirectedInteger tempDirInt = directedIntegers.get(tempInd);
73+
directedIntegers.set(ind, tempDirInt);
74+
directedIntegers.set(tempInd, maxDirInt);
75+
for (DirectedInteger directedInteger : directedIntegers) {
76+
if (directedInteger.getValue() > maxDirInt.getValue()) {
77+
directedInteger.switchDirection();
78+
}
79+
}
80+
permutations.add(undirectIntegers(directedIntegers));
81+
}
82+
return permutations;
83+
}
84+
85+
86+
enum Direction { LEFT, RIGHT }
87+
88+
private class DirectedInteger {
89+
private final Integer value;
90+
private Direction direction;
91+
92+
public DirectedInteger(Integer value, Direction direction) {
93+
this.value = value;
94+
this.direction = direction;
95+
}
96+
97+
public Integer getValue() {
98+
return value;
99+
}
100+
101+
public Direction getDirection() {
102+
return direction;
103+
}
104+
105+
public void setDirection(Direction direction) {
106+
this.direction = direction;
107+
}
108+
109+
public void switchDirection() {
110+
this.direction = this.direction == Direction.LEFT ? Direction.RIGHT : Direction.LEFT;
111+
}
112+
113+
@Override
114+
public String toString() {
115+
return direction + " " + value;
116+
}
117+
}
118+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
import java.util.List;
4+
5+
public abstract class Main {
6+
public static String combine(List<Integer> list) {
7+
StringBuilder s = new StringBuilder();
8+
for (Integer i : list) {
9+
s.append(i);
10+
}
11+
return s.toString();
12+
}
13+
14+
public static void main(String[] args) {
15+
Combinatorics comb = new Combinatorics();
16+
List<List<Integer>> perms = comb.permutations(4);
17+
System.out.println(perms);
18+
System.out.println(perms.size());
19+
}
20+
}

students/23K0120/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
<module>23K0120-p29</module>
4646
<module>23K0120-p30</module>
4747
<module>23K0120-p30-2</module>
48+
<module>23K0120-p32</module>
4849
</modules>
4950
</project>

0 commit comments

Comments
 (0)