Repository navigation
Expand file tree
/
Copy pathChoosingNumbers.java
More file actions
79 lines (73 loc) · 1.77 KB
/
Copy pathChoosingNumbers.java
File metadata and controls
79 lines (73 loc) · 1.77 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class ChoosingNumbers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, Boolean> factors ;
while(in.hasNextLine()){
factors = new HashMap<Integer, Boolean>();
List<Integer> list = readInts(in);
boolean good;
for(int i=0; i<list.size(); i++){
int n = list.get(i);
if( primeFactors(n, factors)){
list.remove(list.indexOf(n));
i--;
}
}
for(int i=0; i<list.size(); i++){
int n = list.get(i);
if(checkPrimeFactors(n, factors)){
list.remove(list.indexOf(n));
i--;
}
}
System.out.println( (Collections.max(list)) );
}
}
public static ArrayList<Integer> readInts( Scanner in) {
String line = in.nextLine();
String[] result = line.split("\\s+");
int size = Integer.parseInt(result[0]);
ArrayList<Integer> a = new ArrayList<Integer>(size);
for (int i=1; i<result.length; i++) {
a.add(i-1, Integer.parseInt(result[i]));
}
return a;
}
public static boolean primeFactors(int n, HashMap<Integer, Boolean> factors) {
boolean res=true;
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
if(factors.containsKey(i)){
factors.put(i, true);
res = false;
}else{
factors.put(i, false);
}
n /= i;
}
}
if (n > 1) {
factors.put(n, false);
}
return res;
}
public static boolean checkPrimeFactors(int n, HashMap<Integer, Boolean> factors){
boolean res=true;
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
res = factors.get(i);
n /= i;
}
}
return res;
}
}