0
点赞
收藏
分享

微信扫一扫

HDOJ 2098 分拆素数和

修炼之士 2022-05-14 阅读 63


Problem Description

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

Input

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

Output

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

Sample Input

30

26

0

Sample Output

3

2

素数打表法!

import java.util.Scanner;

public class Main {
static int[] a = new int[10001];

public static void main(String[] args) {
a[1]=1;
a[2]=0;
a[3]=0;
//打表,将是i是素数的a[i]标志为0;不是素数标志为1;
for(int i=4;i<10000;i++){
for(int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){
a[i]=1;
}
}
}


Scanner sc =new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
if(n==0){
return ;
}
int k=0;
for(int i=1;i<n/2;i++){
if(a[i]==0&&a[n-i]==0)
//n = i+(n-i);
k++;
}

System.out.println(k);
}

}

}



举报

相关推荐

0 条评论