poj2739-Sum of Consecutive Prime Numbers
Posted 啊嘞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2739-Sum of Consecutive Prime Numbers相关的知识,希望对你有一定的参考价值。
题目链接 http://vjudge.net/problem/POJ-2739
解题思路
先用筛法筛出素数,然后枚举就行了。
代码
#include<stdio.h> #include<string.h> #include<math.h> #define MAX_SIZE 10005 int 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的主要内容,如果未能解决你的问题,请参考以下文章
尺取法 || POJ 2739 Sum of Consecutive Prime Numbers
(数学+尺取法)2739 - Sum of Consecutive Prime Numbers