您的位置:首页 > 其它

华为oj初级 查找组成一个偶数最接近的两个素数

2017-03-09 20:15 274 查看
描述

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

请实现如下接口

public static class PrimePair

{

public int primeMin;

public int primeMax;

}

public static PrimePair findPrimeNumber(int number)

{

/* 请实现 */

return null;
}


譬如:输入20 ,输出 7 13

约束

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

知识点 循环

运行时间限制 10M

内存限制 128

输入

输入一个偶数

输出

输出两个素数

样例输入 20

样例输出 7 13

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool is_pri(int n){
if (n < 3)return false;
for (int i = 2; i <= n / 2; i++){
if ((n%i) == 0) return false;
}
return true;
}
int main(){
int n;
cin >> n;
int a;
int b;
int min = n;
for (int i = 3; i < n / 2; i += 2){
if (is_pri(i) && is_pri(n - i)){
if ((n - 2 * i) < min){
a = i;
b = n - i;
}
}
}
cout << a <<endl<< b << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: