您的位置:首页 > 其它

使用筛法找素数 HOJ 2098 分拆素数和

2013-04-11 15:10 197 查看
Problem Description

把一个偶数拆成两个不同素数的和,有几种拆法呢?

 

Input

输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。

 

Output

对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。

 

Sample Input

30
26
0

 

Sample Output

3
2

#include <iostream>
#include <string.h>
using namespace std;
int prim[10001];
int main() {
int max = 10001;
int n;
memset(prim, 1, sizeof(prim));
//先找到所有素数 101 = sqrt(10001)
for(int i=2; i<101; ++i){
if(prim[i]){
for(int j = i; i*j <max; j ++)
prim[j*i] = 0;
}
}
int cnt;
while( cin >> n && n){
cnt = 0;
int last = n/2-1;
//从3开始就行,2是偶数
for(int i=3; i <= last; i++){
if( prim[i] && prim[n-i])
cnt++;
}
cout << cnt << endl;
}
return 0;
}

Problem Description

把一个偶数拆成两个不同素数的和,有几种拆法呢?

 

Input

输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。

 

Output

对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。

 

Sample Input

30
26
0

 

Sample Output

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