RFC

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

POJ2739

 1 import java.util.Scanner;
2
3 public class _2739 {
4 public static final int MAX = 10001;
5 private static boolean[] com = new boolean[MAX];
6
7 public static void findPrime() {
8 for (int i = 2; i * i <= MAX; i++) {
9 if (!com[i]) {
10 for (int j = 2 * i; j < MAX; j += i) {
11 com[j] = true;
12 }
13 }
14 }
15 }
16
17 public static void main(String[] args) {
18 findPrime();
19 Scanner sc = new Scanner(System.in);
20 int num, sum = 0, count = 0;
21 while ((num = sc.nextInt()) != 0) {
22 for (int i = num; i > 1; i--) {
23 if(!com[i]){
24 for (int j = i; j > 1 && sum < num; j--) {
25 if (!com[j])
26 sum += j;
27 }
28 if (sum == num)
29 count++;
30 sum = 0;
31 }
32 }
33 System.out.println(count);
34 count = 0;
35 }
36 }
37 }

小结:

1.下界如果从1开始的话,注意不能越界,数组长度为MAX+1;

2.判定是素数后继续。

posted on 2012-03-06 20:22  hongxuchen  阅读(204)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量