• I


    Give you a lot of positive integers, just to find out how many prime numbers there are.

    Input  There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.Output  For each case, print the number of prime numbers you have found out.Sample Input

    3
    2 3 4

    Sample Output

    2


    这是一道素数题。最关键得是我们要通过这道题学会筛选素数的算法。(这提别重要)

    毫无疑问这是一道打表题,别问我为什么?这是经验。其实这是有规律的
    我把实现筛选素数的算法给出来

    #include <stdio.h>
    #include <math.h>
    #define N 10000001
    int prime[N];
    int main()
    {
       int i, j;
       for(i=2; i<N; i++){
           if(i%2) prime[i]=1;
           else prime[i]=0;
       }

       for(i=3; i<=sqrt(N); i++)
       {   if(prime[i]==1)
           for(j=i+i; j<N; j+=i) prime[j]=0;
       }

       for(i=2; i<100; i++)//只输出2-100内的素数
        if( prime[i]==1 )printf("%d ",i);
       printf(" ");

       return 0;
    }




  • 相关阅读:
    洛谷P3157 [CQOI2011]动态逆序对
    CDQ分治
    快速数论变换(NTT)
    洛谷P3338 [ZJOI2014]力
    洛谷 P1919 A*B Problem升级版
    0-1分数规划
    洛谷P4593 [TJOI2018]教科书般的亵渎
    拉格朗日插值
    20180912-3 词频统计
    20190912-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/damaoranran/p/8748350.html
Copyright © 2020-2023  润新知