您的位置:首页 > 其它

[水题]hdu 1262 寻找素数对

2017-03-22 20:06 253 查看
hdu 1262 寻找素数对

题意:

任意取出一个偶数,来寻找两个值最相近的素数,其和等于该偶数

思路:

水题,判断素数

但还是wa了一次,这两个素数可以是一样的……比如 6 = 3 + 3;

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

bool judge(int x){
if(x < 2) return 0;
else if(x == 2) return 0;
else{
int tmp = sqrt(x);
for(int i=2; i<=tmp; i++)
if(x % i == 0) return 0;
}
return 1;
}
int main(){
int n, mid;
while(~scanf("%d", &n)){
mid = n/2;
for(int i=0; i<mid; i++){
if(judge(mid-i) && judge(mid+i)){
printf("%d %d\n", mid-i, mid+i);
break;
}
}
}
return 0;
}


反思:

仔细一点
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: