码迷,mamicode.com
首页 > 其他好文 > 详细

UVA1210(连续素数和)。

时间:2017-01-13 22:47:03      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:ring   algorithm   int   水题   ons   方法   max   using   递归   

继续寻找水题。。。

把所有的素数存进一个数组,然后用递归的方法做比较简单。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cstdlib>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 const int maxn = 100000;
10 int a[maxn];
11 int c[maxn];
12 int cnt = 0;
13 void prime()
14 {
15     int i,j,k;
16     for(i=0;i<maxn;i++)
17         a[i]=i;
18     a[0]=0;
19     a[1]=0;
20     for(i=2;i<maxn;i++)
21     {
22         for(j=2*i;j<maxn;j+=i)
23         {
24             a[j]=0;
25         }
26     }
27     j=0;
28     for(i=0;i<maxn;i++)
29     {
30         if(a[i])
31             c[j++]=a[i];
32     }
33 }
34 void Calculate(int *c,int n,int k)
35 {
36     int i,j;
37     int t=n;
38     for(i=k;c[i]<=n;i++)
39     {
40         t-=c[i];
41         if(t==0)
42         {
43             cnt++;
44             k++;
45             Calculate(c,n,k);
46         }
47         if(t<0&&t+c[i]>0)
48         {
49             k++;
50             Calculate(c,n,k);
51         }
52     }
53 }
54 int main()
55 {
56     prime();
57     int i,j,k,n;
58     while(scanf("%d",&n)&&n)
59     {
60         Calculate(c,n,0);
61         printf("%d\n",cnt);
62         cnt = 0;
63     }
64     return 0;
65 }

 

UVA1210(连续素数和)。

标签:ring   algorithm   int   水题   ons   方法   max   using   递归   

原文地址:http://www.cnblogs.com/HsiaoYeekwan/p/6284172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!