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

    Sum of Consecutive Prime Numbers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20050   Accepted: 10989

    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

    Source

     

    简单打表水题,往下写之前先看看 10000 内的素数有多少个,然后就可以开数组给OJ生成了,或者自己复制粘贴好数组交上去。
    得到数组就可以对数组进行游标法遍历了,不难。甚至试除法都可以过= =
     
     1 #include <stdio.h>
     2 #include <math.h>
     3 int pr[1229];
     4 int ispr(int n)
     5 {
     6     int p=sqrt(n);
     7     for(int i=2;i<=p;i++)
     8         if(n%i==0) return 0;
     9     return 1;
    10 }
    11 int main()
    12 {
    13     int n,cnt=0;
    14     int i,j,c;
    15     long sum;
    16 
    17     for(i=2;i<=10000;i++)
    18         if(ispr(i))
    19             pr[cnt++]=i;
    20 
    21     while(~scanf("%d",&n)&&n)
    22     {
    23         c=0;
    24         for(i=0;i<cnt&&pr[i]<=n;i++)
    25         {
    26             sum=0;
    27             for(j=i;j<cnt;j++)
    28             {
    29                 sum+=pr[j];
    30                 if(sum>=n) break;
    31             }
    32             if(sum==n) c++;
    33         }
    34         printf("%d
    ",c);
    35     }
    36     return 0;
    37 }





  • 相关阅读:
    Codeforces Round #499 (Div. 2) C.FLY 数学推导_逆推
    Codeforces div2 #499 B. Planning The Expedition 大水题
    Lost Cows POJ
    洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 状压动归
    2018.9.30 ~ 2018.11.1 做题记录
    推荐一款强大的轻量级模块化WEB前端快速开发框架--UIkit
    jQuery Validate多实例讲解
    关于Css的垂直居中的一些方法
    关于浮动与清除浮动,你应该知道的
    使用 Vuex + Vue.js 构建单页应用
  • 原文地址:https://www.cnblogs.com/BlackStorm/p/4268686.html
Copyright © 2011-2022 走看看