水题练手POJ2739

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.判定是素数后继续。

转载于:https://www.cnblogs.com/leftcopy/archive/2012/03/06/2382549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值