本文主要是介绍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;
}
这篇关于POJ 2739 Sum of Consecutive Prime Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!