"Huawei OJ" "074-Prime Companion"

Source: Internet
Author: User

"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "074-Prime Companion" "Project Download" topic description
    若两个正整数的和为素数,则这两个正整数称之为“素数伴侣”,如2和5、6和13,它们能应用于通信加密。现在密码学会请你设计一个程序,从已有的N(N为偶数)个正整数中挑选出若干对组成“素数伴侣”,挑选方案多种多样,例如有4个正整数:2,5,6,13,如果将5和6分为一组中只能得到一组“素数伴侣”,而将2和5、6和13编组将得到两组“素数伴侣”,能组成“素数伴侣”最多的方案称为“最佳方案”,当然密码学会希望你寻找出“最佳方案”。输入:    有一个正偶数N(N≤100),表示待挑选的自然数的个数。后面给出具体的数字,范围为[2,30000]。输出:    输出一个整数K,表示你求得的“最佳方案”组成“素数伴侣”的对数。
Enter a description
输入说明1 输入一个正偶数n2 输入n个整数
Output description
求得的“最佳方案”组成“素数伴侣”的对数。
Input example
42 5 6 13
Output example
2
Algorithm implementation
ImportJava.util.Arrays;ImportJava.util.Scanner;/** * Author: Wang Junshu * date:2016-01-06 10:43 * declaration:all rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnext ()) {intn = scanner.nextint ();int[] arr =New int[n]; for(inti =0; I < n;            i++) {Arr[i] = Scanner.nextint ();        } System.out.println (Countprimepairs (arr));    } scanner.close (); }Private Static Boolean IsPrime(intN) {if(N <2) {return false; }intsqrt = (int) math.sqrt (n); for(inti =2; I <= sqrt; i++) {if(n% i = =0) {return false; }        }return true; }/** * from behind * using dynamic programming to solve problems, dp[i] means (n-1) ~i The maximum number of companions; * When V[i]+v[j] is prime.     DP[I]+DP[J+1] = dp[i+1]+dp[j+1]+1;     * Due to the number of couples in pairs appear, must only in I-1 and j-1 on the basis of a pair. * When V[i]+v[j] is not a prime number. DP[I]=DP[I+1] * * @param v * @return  */    Private Static int Countprimepairs(int[] v) {int[] DP =New int[V.length +1]; for(inti = v.length-2; I >-1; i--) { for(intj = v.length-1; J > i; j--) {intCNT = IsPrime (V[i] + v[j])? (Dp[i +1]-Dp[j-1] + dp[j +1] +1): Dp[i +1]; Dp[i] = (cnt > dp[i])?            Cnt:dp[i]; }        }//System.out.println (arrays.tostring (v));//SYSTEM.OUT.PRINTLN (Arrays.tostring (DP));        returndp[0]; }}

"Huawei OJ" "074-Prime Companion"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.