poj2909
素数筛法,然后判断有几个即可。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
#define maxn 32005
int prm[maxn];
bool is[maxn];
int getprm(int n)
{
int i, j, k = 0;
int s, e = (int) (sqrt(0.0 + n) + 1);
memset(is, 1, sizeof(is));
prm[k++] = 2;
is[0] = is[1] = 0;
for (i = 4; i < n; i += 2)
is[i] = 0;
for (i = 3; i < e; i += 2)
if (is[i])
{
prm[k++] = i;
for (s = i * 2, j = i * i; j < n; j += s)
is[j] = 0;
// 因为j是奇数,所以+奇数i后是偶数,不必处理!
}
for (; i < n; i += 2)
if (is[i])
prm[k++] = i;
return k; // 返回素数的个数
}
int main()
{
//freopen("D:\\t.txt", "r", stdin);
int n;
getprm(maxn - 4);
while (scanf("%d", &n) != EOF && n != 0)
{
int e = n / 2;
int ans = 0;
for (int i = 0; prm[i] <= e; i++)
if (is[n - prm[i]])
ans++;
printf("%d\n", ans);
}
return 0;
}