zoukankan      html  css  js  c++  java
  • 筛素数——HDU 3792 Twin Prime Conjecture

    Twin Prime Conjecture

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


    Problem Description
    If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
    Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
     
    Input
    Your program must read test cases from standard input.
    The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
     
    Output
    For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
     
    Sample Input
    1 5 20 -2
     
    Sample Output
    0 1 4
     
    题意:输入一个数N(N<105),问这个数以内有多少对孪生素数(两个素数相差2)。
    题解:筛出所需的素数,然后用一个数组来存到第i个素数时有多少对孪生素数。最后找到不大于N的最大素数,只要输出到这个素数有多少对孪生素数。
    /*HDU 3792*/
    /*筛素数*/
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int maxn=1e5+100;
    bool isprime[maxn];
    int prime[maxn];
    int cnt=0;
    int a[maxn];
    void getprime()
    {
        memset(isprime,1,sizeof(isprime));
        memset(prime,0,sizeof(prime));
        isprime[0]=isprime[1]=0;
        for(int i=2;i<maxn;i++)
        {
            if(isprime[i])
                for(int j=2*i;j<maxn;j+=i)
                    isprime[j]=false;
        }
    
        for(int i=0;i<maxn;i++)
        {
            if(isprime[i])
                prime[cnt++]=i;
        }
        memset(a,0,sizeof(a));
        int t=0;
        for(int i=1;i<cnt;i++)
        {
            if((prime[i]-prime[i-1])==2)
            {
                a[i]=++t;/*当找到一对孪生素数时,在后一个+1*/
                //printf("%d %d
    ",prime[i],prime[i-1]);/**/
            }
            else
                a[i]=t;
        }
    }
    
    int find(int n,int l,int r)/*开始想用二分查找,可是不知道错哪了*/
    {
    
        int mid=(l+r)/2;
        if(prime[mid]>n)
            return find(n,l,mid);
        else if(prime[mid]==n)
            return a[mid];
        else 
            return find(n,mid+1,r);
    }
    int main()
    {
        int n;
        getprime();
        while(scanf("%d",&n)!=EOF)
        {
            if(n<0)    break;    
            int ans=0;
            int i;
            for(i=0;i<cnt-1;i++)
                if(prime[i]<=n&&prime[i+1]>n)
                    break;
            ans=a[i];
            if(n==1)    ans=0;
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    c++中重载、重写、覆盖的区别
    c++值传递,指针传递,引用传递以及指针与引用的区别
    c/c++中static与extern关键字介绍
    Typedef和#define之间的区别
    c++中sizeof()的用法介绍
    c++笔试题:不使用第三个变量来交换俩个变量的数值
    c++位运算符介绍
    3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建
    2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离
    1.高并发教程-基础篇-之nginx负载均衡的搭建
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5365707.html
Copyright © 2011-2022 走看看