sicily 1259 Sum of Consecutive Primes

又水,素数只能是连续的,而且最多才10000!

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int a[10001];
 6 
 7 bool is_prime(int x)
 8 {
 9     if(x == 1)    return 0;
10     for(int i=2; i*i<=x; i++)
11         if(x % i == 0)
12             return 0;
13     return 1;
14 }
15 
16 int get_prime()
17 {
18     int count=0;
19     for(int i=2; i<10000; i++)
20     {
21         if(is_prime(i))
22             a[count++]=i;
23     }
24     return count;
25 }
26 
27 int main()
28 {
29     int num;
30     int count = get_prime();
31     while(scanf("%d", &num) != EOF, num)
32     {
33         
34         int ans=0;
35         for(int i=0; i<count; i++)
36         {
37             int sum=0;
38             int j=i;
39             while(sum < num)
40             {        
41                 sum+=a[j++];
42                 //cout << "sum: " <<sum << endl;
43                 if(sum == num)
44                 {
45                     ans++;
46                     break;
47                 }
48             }
49             
50         }
51         cout << ans << endl;
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/dominjune/p/4547502.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值