传送门:http://poj.org/problem?id=2739、
直接用筛选法打表,然后双重循环判断多个情况,符合就cnt++。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int maxn = 10000; int s[maxn]; int prime[maxn]; int l; //素数打表 void set() { int i,j; memset(prime,0,sizeof(prime)); for(i = 2; i<10000; i++) { if(prime[i]) continue;//筛选 for(j = i+i; j<10000; j+=i)//经过筛选实际上超过不了maxn=10000 prime[j] = 1;//标记 s[l++] = i;//存下素数 } } int main() { set(); int ans; while (~scanf("%d",&ans)) { if (ans == 0) { return 0; } int cnt = 0; for (int i = 0; s[i] <= ans; i++) { int sum = 0; for (int j = i; sum <= ans; j++) { sum += s[j]; if (sum == ans) { cnt++; break; } } } cout<<cnt<<endl; } }