使用筛法找素数 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
把一个偶数拆成两个不同素数的和,有几种拆法呢?
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
相关文章推荐
- 埃氏筛法 —— 对素数的使用
- hdu 分拆素数和 2098
- HDU 2098 分拆素数和(筛法求素数+暴力枚举)
- HDU 2098 分拆素数和
- 分拆素数和 HDU - 2098
- 素数筛总结篇___Eratosthenes筛法和欧拉筛法(*【模板】使用 )
- 分拆素数和(http://acm.hdu.edu.cn/showproblem.php?pid=2098)
- Hdoj 2098 分拆素数和 之解题报告
- hdu2098 分拆素数和 筛法
- HDU 2098 分拆素数和
- hdu 2098 分拆素数和
- HDU2098——分拆素数和
- 杭电ACM 2098 分拆素数和
- hdu-2098-分拆素数和。。。
- 2098 分拆素数和
- hdoj 2098 分拆素数和 【素数打表】
- HDU 2098 - 分拆素数和
- 分拆素数和 【hdu-2098】【素数】
- HDUOJ_2098_分拆素数和
- hdu 2098 分拆素数和