POJ 2739 Sum of Consecutive Prime Numbers

简单的素数打表题, 不过我用的是筛子做的

#include <stdio.h>
#include <math.h>
#include <string.h>
int prime[10000], max;
void init()
{
	int i, j;
	memset(prime, 0, sizeof(prime));
	prime[0] = prime[1] = 1;
	for (i = 2; i <= 100; i++)
		if (prime[i] == 0)
		for (j = 2 * i; j <= 10000; j += i)
			prime[j] = 1;
	for (j = 0, i = 2; i <= 10000; i++)
		if (prime[i] == 0)
			prime[j++] = i;
	max = j;
}
int main()
{
	int i, j, n, num, sum, k;
	init();
	while (scanf("%d", &n) && n)
	{
		num = 0;
		for (i = 0; i <= max; i++)
		{
			sum = 0;
			for (j = i; j <= max; j++)
			{
				sum += prime[j];
				if (sum < n)	continue;
				if (sum == n)	num++;
				else			break;
			}
		}
		printf("%d\n", num);
	}
	return 0;
}


你可能感兴趣的:(Numbers)