zoukankan      html  css  js  c++  java
  • Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)

    Primes Problem


    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 12    Accepted Submission(s): 11


    Problem Description
    Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
     
    Input
    Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n10000).
     
    Output
    For each test case, print the number of ways.
     
    Sample Input
    3 9
     
    Sample Output
    0 2
     
    Accepted 的代码:
     
    #include <string>
    #include <iostream>
    #include <cstdio>
    #include <math.h>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    int f[10001];
    
    void sushu()
    {
        int i, j;
        memset(f, 0, sizeof(f));
        f[1]=1;
        i=2;
        while(i<=200)
        {
            for(j=i*2; j<=10000; j+=i)
            {
                f[j]=1;
            }
            i++;
            while(f[i]==1)
            {
                i++;
            }
        }
    }
    
    int s[10000], e;
    
    int main()
    {
        int n;
        int i, j, k;
        int cnt;
        sushu();
         e=0;
        for(i=2; i<=10000; i++)
        {
            if(f[i]==0)
            {
                s[e++]=i;
            }
        }
        while(scanf("%d", &n)!=EOF)
        {
            if(n<6)
            {
                cout<<'0'<<endl;
                continue;
            }
            cnt=0;
            int flag=0;
            for(i=0; i<=n; i++)
            {
                if(s[i]>=n)
                  break;
                for(j=i; j<=n; j++)
                {
                    if( (s[i]+s[j])>=n )
                    {
                        flag=1;
                        break;
                    }
                    else
                    {
                        int dd=n-s[i]-s[j];
                        if(f[dd]==0 && dd>=s[i] && dd>=s[j] )
                        {
                            cnt++;
                        }
    
                    }
                }
            }
            cout<<cnt<<endl;
        }
        return 0;
    }
    
     
    这个代码是超时的(3层循环太 耗时):
    #include <string>
    #include <iostream>
    #include <cstdio>
    #include <math.h>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    int f[10001];
    
    void sushu()
    {
        int i, j;
        memset(f, 0, sizeof(f));
        f[1]=1;
        i=2;
        while(i<=200)
        {
            for(j=i*2; j<=10000; j+=i)
            {
                f[j]=1;
            }
            i++;
            while(f[i]==1)
            {
                i++;
            }
        }
    }
    
    int s[10000], e;
    
    int main()
    {
        int n;
        int i, j, k;
        int cnt;
        sushu();
         e=0;
        for(i=2; i<=10000; i++)
        {
            if(f[i]==0)
            {
                s[e++]=i;
            }
        }
        while(scanf("%d", &n)!=EOF)
        {
            if(n<6)
            {
                cout<<'0'<<endl;
                continue;
            }
            cnt=0;
            for(i=0; i<=n; i++)
            {
    
                for(j=i; j<=n; j++)
                {
                    for(k=j; k<=n; k++)
                    {
                        if((s[i]+s[j]+s[k])==n)
                        {
                            cnt++;
                        }
                    }
                }
            }
            cout<<cnt<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    查看数据库中指定用户下每个表占的实际空间大小
    数据库中查询列数据是否有重复
    oracle查看数据库的字符集
    【转】oracle数据库中varchar2陷阱
    cursor详解
    vs报算术运算溢出的错误
    count(1)比count(*)效率高
    基于NPOI的Execl导入导出例子
    day4-2数组及方法
    day4-1深入理解对象之创建对象
  • 原文地址:https://www.cnblogs.com/yspworld/p/4100215.html
Copyright © 2011-2022 走看看