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

    Description

    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、对每个输入n,计算出小于n的素数,然后从连续的1个数、2个数、。。。、n/2个数,穷举,超时;
       2、由于最大为10000,先建立素数表,然后从连续的1个数、2个数、。。。、n/2个数,穷举,超时;
       3、建立素数表,然后穷举结束条件为和大于等于数n,AC。

    方法一:超时

    #include <iostream>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    int prime[1229];
    
    int main()
    {
        int n, res;
        int i, j, length, end, start, flag;
        res=0;
        //memset(prime,0,sizeof(prime));
    
        for(i=2; i<=10000; i++)
        {
            int sq=sqrt(i);
            flag=0;
            for(j=2; j<=sq; j++)
            {
                if(i%j==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                prime[res]=i;
                res++;
            }
        }
    
        while(cin>>n&&n!=0)
        {
            int k;
            for(k=0;k<1229;k++){
                if(prime[k]>=n)
                    break;
            }
            int sum=0, max=k;
            res=0;
            for(length=0; length<=k/2; length++)
            {
                start=0;
                end= max-length;
    
                for(start=0; start<=end; start++)
                {
    
                    sum=0;
                    for(i=start; i<=start+length; i++)
                    {
    
                        sum=sum+prime[i];
                    }
                    if(sum==n)
                    {
                        res++;
                        sum=0;
                        break;
                    }
                }
            }
            cout<<res<<endl;
        }
        return 0;
    }


    方法二:AC

    #include <iostream>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    int prime[1229];
    
    int main()
    {
        int n, res;
        int i, j, k, flag, sum;
        res=0;
        //memset(prime,0,sizeof(prime));
    
        for(i=2; i<=10000; i++)
        {
            int sq=sqrt(i);
            flag=0;
            for(j=2; j<=sq; j++)
            {
                if(i%j==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                prime[res]=i;
                res++;
            }
        }
    
        while(cin>>n&&n!=0)
        {
            k=0;
            for(i=0; i<res&&prime[i]<=n; i++)
            {
                sum = prime[i];
                j=i+1;
                while(sum<n&&j<res)
                {
                    sum=sum+prime[j++];
                }
                if(sum==n)
                {
                    k++;
                }
                //cout<<" i="<<i<<endl;
            }
            cout<<k<<endl;
        }
        return 0;
    }



  • 相关阅读:
    Ubuntu16.04更新源后apt-get update报错的解决方法
    安装Ubuntu16.04 64bit系统时出错的解决方案
    采用Python-Qt5制作置顶透明桌面提醒词/座右铭/便签
    如何手动解析Keras等框架保存的HDF5格式的权重文件
    读取yml配置文件中的值
    添加20位随机数,不重复,可以用来作为发票申请流水等功能
    java 从json串中取出某个字段的值
    Spring事务的两种方式
    (附表设计)超级全面的权限系统设计方案
    nfs 测试
  • 原文地址:https://www.cnblogs.com/panderen/p/2438833.html
Copyright © 2011-2022 走看看