查找组成一个偶数最接近的两个素数

来源:互联网 发布:会计事务所审计软件 编辑:程序博客网 时间:2024/04/30 19:30

  • 任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对 

请实现如下接口

    public static class PrimePair

    {

       public int primeMin;

       public int primeMax;

    }

    public static PrimePair findPrimeNumber(int number)

    {

        /* 请实现 */

 

 

       return null;

    }

 

譬如:输入20 ,输出 7 13

约束

  1. number为输入的偶数,5 < inum <= 10000


知识点循环运行时间限制10M内存限制128输入

输入一个偶数

输出

输出两个素数

样例输入20样例输出7 13

#include <iostream>

#include <string>
using namespace std;
int JudgIsPrime(const int a)  
{  
    for(int i = 2;i <= a/2; i++)  
    {  
        if(a % i == 0)  
            return 0;  
    }  
    return 1;  
}  
int main()
{
   int b;  
    cin >> b;  
  
    //非偶数异常输入  
    while(b % 2 != 0)  
        return 0;  
  
    int j,distance,t = 0;  
    int min = b; 
 for(j = 2; j <= b/2; j++)  
    {  
        if (JudgIsPrime(j) && JudgIsPrime(b - j))  
        {  
            distance = b - j -j;  
            if ( min > distance)  
            {  
                min = distance;  
                t = j;  
            }  
        }  
    }  

cout<<t<<endl;
cout<<b-t<<endl;
 return 0;


}
0 0
原创粉丝点击