-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversePrimeNumber.java
More file actions
48 lines (45 loc) · 1.23 KB
/
Copy pathReversePrimeNumber.java
File metadata and controls
48 lines (45 loc) · 1.23 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReversePrimeNumber {
public boolean isPrime(int num){
if (num == 1){
return false;
}
for (int i=2;i<num/2;i++){
if (num%i==0){
return false;
}
}
return true;
}
public List<Integer> solution(int n, int[] arr){
List<Integer> answer = new ArrayList<>();
for (int i=0;i<n;i++){
int res=0;
int tmp = arr[i];
while (tmp != 0 ){
int t = tmp % 10;
res = res*10+t;
tmp = tmp/10;
}
if (isPrime(res)){
answer.add(res);
}
}
return answer;
}
public static void main(String[] args) {
ReversePrimeNumber t = new ReversePrimeNumber();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i=0 ; i<n; i++){
arr[i] = scanner.nextInt();
}
List<Integer> solution = t.solution(n, arr);
for (Integer integer : solution) {
System.out.print(integer + " ");
}
}
}