1.解题思路:点击打开链接
2.解题思路:本题要求寻找连续个素数相加为n的个数。由于n的范围不大, 因此可以事先打表。计算好所有的连续和的个数,最后直接输出即可。
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<functional> using namespace std; #define N 10000+5 int vis[N]; int d[N]; vector<int>primes; void init() { int m = sqrt(N); memset(vis, 0, sizeof(vis)); memset(d, 0, sizeof(d)); for (int i = 2; i < m; i++)if (!vis[i]) for (int j = i*i; j < N; j += i) vis[j] = 1; for (int i = 2; i < N;i++) if (!vis[i]) primes.push_back(i); int len = primes.size(); for (int i = 0; i < len; i++) { int sum = primes[i]; int j = i + 1; while (j < len&&sum < N) { d[sum]++; sum += primes[j]; j++; } } } int main() { //freopen("t.txt", "r", stdin); init(); int n; while (~scanf("%d", &n) && n) cout << d[n] << endl; return 0; }
原文地址:http://blog.csdn.net/u014800748/article/details/45169241