-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
65 lines (55 loc) · 1.37 KB
/
Main.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
package basicLevel1005;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k = in.nextInt();
int[] n = new int[k];
@SuppressWarnings("rawtypes")
TreeSet[] treeSets = new TreeSet[k];
for (int i = 0; i < k; i++) {
n[i] = in.nextInt();
treeSets[i] = new TreeSet<Integer>(getNumber(n[i]));
}
in.close();
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < k; i++) {
if (isMajor(n[i], treeSets, i)) {
list.add(n[i]);
}
}
int[] array = new int[list.size()];
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}
Arrays.sort(array);
for (int i = array.length - 1; i > 0; i--) {
System.out.print(array[i] + " ");
}
System.out.print(array[0]);
}
public static boolean isMajor(int n, @SuppressWarnings("rawtypes") TreeSet[] treeSets, int index) {
for (int i = 0; i < treeSets.length; i++) {
if (i != index && treeSets[i].contains(n)) {
return false;
}
}
return true;
}
public static TreeSet<Integer> getNumber(int n) {
TreeSet<Integer> treeSet = new TreeSet<Integer>();
treeSet.add(n);
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = (3 * n + 1) / 2;
}
treeSet.add(n);
}
return treeSet;
}
}