首页 > 代码库 > poj2739-Sum of Consecutive Prime Numbers

poj2739-Sum of Consecutive Prime Numbers

题目链接 http://vjudge.net/problem/POJ-2739

 

解题思路

  先用筛法筛出素数,然后枚举就行了。

 

代码

#include<stdio.h>#include<string.h>#include<math.h>#define MAX_SIZE 10005int primeNumber[MAX_SIZE]; bool judgePrime[MAX_SIZE];int tot; int search(int number){    int count = 0;    for(int i=0; i<tot; i++) {        int now = 0; int j = i;        while(now <= number && j < tot) {            now += primeNumber[j];            if(now == number) { count++; break; }            j++;        }    }    return count;}int main(){    int n;    memset(judgePrime, true, sizeof(judgePrime));    int m = sqrt(MAX_SIZE) + 0.5;    for(int i=2; i<m; i++) {        if(judgePrime[i]) {            for(int j=i*i; j<MAX_SIZE; j+=i)                judgePrime[j] = false;        }    }    for(int i=2; i<MAX_SIZE; i++) if(judgePrime[i])        primeNumber[tot++] = i;    scanf("%d", &n);    while(n != 0) {        printf("%d\n", search(n));        scanf("%d", &n);    }    return 0;}

 

poj2739-Sum of Consecutive Prime Numbers