-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask4.java
67 lines (54 loc) · 1.61 KB
/
Task4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Kornilov Nikita, M3102, 04.10.2020 */
package Sem1.Lab2;
import java.io.*;
import java.util.*;
public class Task4 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
int k;
int array[];
public static void main(String[] args) {
new Task4().run("antiqs.in", "antiqs.out");
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens())
in = new StringTokenizer(br.readLine());
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public void solve() throws IOException {
int n = nextInt();
array = new int[n];
k = n;
half(0, n - 1);
array[n - 1] = 1;
for (int i = 0; i < array.length - 1; i++) {
out.print(array[i]);
out.print(" ");
}
out.print(array[array.length - 1]);
}
public void half(int leftIndex, int rightIndex) {
if (leftIndex != rightIndex) {
int mid = (leftIndex + rightIndex) / 2;
//System.out.printf("l:%d r:%d m:%d\n", leftIndex, rightIndex, mid);
array[mid] = k--;
half(leftIndex, mid);
half(mid + 1, rightIndex);
}
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}