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

Commit 15217cb

Browse files
committed
Lab32
1 parent 53a51e2 commit 15217cb

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

students/23K0565/23K0565-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0565</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0565-p32</artifactId>
12+
<description>32 практическая</description>
13+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.*;
4+
5+
public class PermJT {
6+
7+
static int n;
8+
static int[] a;
9+
static boolean[] dir;
10+
11+
static void swap(int i, int j) {
12+
int tmp = a[i];
13+
a[i] = a[j];
14+
a[j] = tmp;
15+
boolean tmpDir = dir[i];
16+
dir[i] = dir[j];
17+
dir[j] = tmpDir;
18+
}
19+
20+
static void show() {
21+
for (int i = 0; i < n; i++) {
22+
System.out.print(a[i] + " ");
23+
}
24+
System.out.println();
25+
}
26+
27+
static int largestMov() {
28+
int maxIdx = -1;
29+
for (int i = 0; i < n; i++) {
30+
int nextIdx = dir[i] ? i + 1 : i - 1;
31+
if (nextIdx >= 0 && nextIdx < n && a[i] > a[nextIdx]) {
32+
if (maxIdx == -1 || a[i] > a[maxIdx]) {
33+
maxIdx = i;
34+
}
35+
}
36+
}
37+
return maxIdx;
38+
}
39+
40+
static void generate() {
41+
show();
42+
int idx;
43+
while ((idx = largestMov()) != -1) {
44+
int nextIdx = dir[idx] ? idx + 1 : idx - 1;
45+
swap(idx, nextIdx);
46+
for (int i = 0; i < n; i++) {
47+
if (a[i] > a[nextIdx]) {
48+
dir[i] = !dir[i];
49+
}
50+
}
51+
show();
52+
}
53+
}
54+
55+
public static void main(String[] args) {
56+
Scanner sc = new Scanner(System.in);
57+
n = sc.nextInt();
58+
a = new int[n];
59+
dir = new boolean[n];
60+
for (int i = 0; i < n; i++) {
61+
a[i] = i + 1;
62+
dir[i] = false;
63+
}
64+
generate();
65+
sc.close();
66+
}
67+
}

0 commit comments

Comments
 (0)