Number of prime pairs
Title Description
If the number of two positive integers is a prime number, then these two positive integers are called "prime mates", such as 2 and 5, 6, and 13, which can be used for communication encryption. Now the password learn to ask you to design a program, from the existing n (n is even) a number of positive integers selected to form a "prime companion", the selection of a variety of options, for example, there are 4 positive integers: 2,5,6,13, if the 5 and 6 in a group can only get a group of "prime Companion", and 2 and 5, The 6 and 13 groups will get two sets of "prime Companion", the program that can form "prime companion" is called "Best plan", of course, the Password society wants you to find out "best plan".
Input:
There is a positive even N (n≤100) that represents the number of natural numbers to select. The following gives a specific number, ranging from [2,30000].
Output:
Output an integer k that represents the logarithm of the "best plan" that you are seeking to make up the "prime companion".
Input Description:
Enter a description
1 Enter a positive even n
2 Enter n integers
Output Description:
The "best scheme" is the logarithm of the "prime Companion".
Input Example:
42 5 6 13
Output Example:
2
Solving
Not understanding
ImportJava.util.*; Public classmain{ Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); while(Sc.hasnext ()) {intLength =Sc.nextint (); int[] In_num =New int[length]; for(intI =0;i<length;i++) {In_num[i]=Sc.nextint (); } intMax_count =Getnumofprime (In_num); System.out.println (Max_count); } sc.close (); } Private Static BooleanIsPrime (intnum) { for(inti = 2;i<=math.sqrt (num); i++){ if(num%i = = 0){ return false; } } return true; } Private Static intGetnumofprime (int[] in_num) { intLength =in_num.length; int[] DP =New int[Length+1]; for(inti = 0;i<dp.length;i++) {Dp[i]= 0; } intCount = 0; for(intj = length-2;j>=0;j--){ for(intK = length-1;k>j;k--){ if(IsPrime (in_num[j]+In_num[k])) {Count= Dp[j+1]-dp[k-1]+dp[k+1]+1; } Else{Count= Dp[j+1]; } if(count>Dp[j]) {Dp[j]=count; } } } returnDp[0]; } }
Huawei OJ: Number of prime pairs