HDOJ 2098 分拆素数和
【摘要】 Problem Description 把一个偶数拆成两个不同素数的和,有几种拆法呢?
Input 输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
Output 对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
Sample Input 30 26 0
Sample Output 3 2
素数打表法!...
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); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。
原文链接:chenhx.blog.csdn.net/article/details/50699210
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)