zoukankan      html  css  js  c++  java
  • Sum of Consecutive Prime Numbers POJ

        Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
    numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
    Your mission is to write a program that reports the number of representations for the given positive integer.Input

    The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

    Output

    The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

    Sample Input

    2
    3
    17
    41
    20
    666
    12
    53
    0

    Sample Output

    1
    1
    2
    3
    0
    0
    1
    2
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<string>
     5 #include<cstring>
     6 #define MAX 10005
     7 using namespace std;
     8 
     9 int prime[MAX],b[MAX];
    10 int k;
    11 
    12 void get_p()
    13 {   memset(prime,true,sizeof(prime));
    14     prime[1]=false,prime[2]=true;
    15     k=1; 
    16     for(int i=2;i<=MAX;i++){
    17        if(prime[i]){
    18               b[k++]=i;
    19            int j=i+i;
    20            while(j<=MAX)
    21            {  prime[j]=false;
    22               j=j+i;
    23            }  
    24        }
    25     }
    26 }
    27 int main()
    28 {  int n;
    29    while(~scanf("%d",&n)&&n){
    30           get_p();
    31          // for(int i=1;i<k;i++) printf("%d ",b[i]);
    32           int l=1,r=1;
    33           int ans=0,sum=0,t=0;
    34           while(r<=k){
    35                 if(b[r]>n) break;
    36                 sum+=b[r];
    37                 r++;
    38                 while(sum>n){
    39                      sum-=b[l];
    40                      l++;
    41                 }
    42                 if(sum==n) t++;
    43           }
    44           printf("%d
    ",t);
    45    }
    46 } 
  • 相关阅读:
    看了一些Tab标签的效果,正好在学习前端,自己动手也写个把~
    js时间的操作,为了让cookie在当天24点过期~
    《转载》CSS换行问题
    【PHP项目】form表单的enctype属性
    php中处理字符串的常见函数
    PHP加密函数
    PHP判断时关于null,0,true,flase的值
    获取PHP页面的当前文件名(包括后缀名)
    php中关于empty()函数是否为真的判断
    【PHP】判断变量是否为控
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6754169.html
Copyright © 2011-2022 走看看