Skip to content

Commit a475786

Browse files
WA
1 parent e4a8bbe commit a475786

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Diff for: CodePtit/src/main/java/Handling_File/SoNguyenTo.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package Handling_File;
7+
8+
/**
9+
*
10+
* @author Admin
11+
*/
12+
import java.io.*;
13+
import java.util.*;
14+
15+
public class SoNguyenTo {
16+
17+
public static boolean isPrime(int n) {
18+
if(n<2)
19+
return false;
20+
for (int i = 2; i <= Math.sqrt(n); i++) {
21+
if (n % i == 0) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
29+
FileInputStream fis = new FileInputStream("SONGUYEN.in");
30+
ObjectInputStream ois = new ObjectInputStream(fis);
31+
ArrayList<Integer> arr = (ArrayList<Integer>) ois.readObject();
32+
Collections.sort(arr);
33+
HashMap<Integer, Integer> hm = new HashMap<>();
34+
for (Integer i : arr) {
35+
if (isPrime(i) == true) {
36+
if (!hm.containsKey(i)) {
37+
hm.put(i, 1);
38+
} else {
39+
hm.put(i, hm.get(i) + 1);
40+
}
41+
}
42+
}
43+
for (Integer i : hm.keySet()) {
44+
System.out.printf("%d %d\n", i, hm.get(i));
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)