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
| import java.util.*;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt();
List<Integer> evenList = new ArrayList<>(); List<Integer> oddList = new ArrayList<>(); for(int i = 0; i < num; i++){ int j = in.nextInt(); if(isEven(j)){ evenList.add(j); }else{ oddList.add(j); } }
int size = evenList.size(); int count = 0;
int[] evensMatch = new int[size];
for(Integer odd : oddList){ int[] used = new int[size]; if(find(odd, evenList, used, evensMatch)){ count++; } } System.out.println(count); in.close(); }
public static boolean isEven(int n){ if(n % 2 == 0){ return true; } return false; }
public static boolean isPrime(int n){ if(n <= 1){ return false; }else{ for(int i = 2; i < n; i++){ if(n % i == 0){ return false; } } } return true; }
public static boolean find(int odd, List<Integer> evens, int[] used, int[] evenMatch){ for(int i = 0; i < evens.size(); i++){ if(isPrime(odd + evens.get(i)) && used[i] == 0){ used[i] = 1; if(evenMatch[i] == 0 || find(evenMatch[i], evens, used,evenMatch)){ evenMatch[i] = odd; return true; } } } return false; } }
|