题意:
任意取出一个偶数,来寻找两个值最相近的素数,其和等于该偶数
思路:
水题,判断素数
但还是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;
}
反思:
仔细一点
本文解析了HDU1262题目中的寻找素数对问题。通过给出的C++代码示例,介绍了如何找出两个值最接近的素数使得它们的和等于一个指定的偶数。特别注意的是,这两个素数可以相同。

1607

被折叠的 条评论
为什么被折叠?



