poj: 2739

挺简单,生成素数表之后建个全素数的vector,然后。。随便玩咯

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <string>

 4 #include <stack>

 5 #include <map>

 6 #include <vector>

 7 #include <algorithm>

 8 using namespace std;

 9 

10 bool prime[10010];

11 

12 void primes() {

13     memset(prime, true, sizeof(prime));

14     for (int i = 2; i < 10001; i++)

15         if (prime[i])

16             for (int j = 2; j * i < 10001; ++j) prime[i*j] = false;

17 }

18 

19 int main()

20 {

21     primes();

22     int n;

23     vector<int> S;

24     for (int i = 2; i < 10000; ++i)

25         if (prime[i]) S.push_back(i);

26     while (cin >> n && n) {

27         int ans = 0;

28         for (int i = 0; S[i] <= n; ++i) {

29             int sum = 0;

30             int cur = i;

31             while (sum <= n) {

32                 if (sum == n) {

33                     ans++;

34                     break;

35                 }

36                 else {

37                     sum += S[cur];

38                     cur++;

39                 }

40             }

41         }

42         cout << ans << endl;

43     }

44 

45     return 0;

46 }

 

你可能感兴趣的:(poj)