zoukankan      html  css  js  c++  java
  • 求n到m之间素数的个数

    Description

    求n到m之间素数的个数

    Input

    多组测试数据,每组先输入一个整数t,表示组数,然后每组输入2个正整数n和m,(1 <= n <= m <= 10000)

    Output

    每组一行,内容为一个整数,输出n到m之间素数的个数

    Sample Input

    1
    2 3

    Sample Output

    2


    #include<stdio.h> #include<math.h> int main() { int t; int i,j,k; int n,m; int q; int count; while(scanf("%d",&t)!=EOF) { for(i=0;i<t;i++) { count=0; scanf("%d%d",&n,&m); if(n==1) { for(j=2;j<=m;j++) { q=sqrt(j); for(k=2;k<=q;k++) { if(j%k==0) break; } if(k>q) count++; } printf("%d ",count); } if(n>1) { for(j=n;j<=m;j++) { q=sqrt(j); for(k=2;k<=q;k++) { if(j%k==0) break; } if(k>q) count++; } printf("%d ",count); } } } }
  • 相关阅读:
    Arbitrage
    Big Event in HDU
    敌兵布阵
    Eddy's picture
    A Walk Through the Forest 最短路径+深搜
    Holding Bin-Laden Captive! 母函数
    Moving Tables 贪心
    Fire Net
    Number Sequence
    Find your present! map
  • 原文地址:https://www.cnblogs.com/zhouweibaba/p/10224873.html
Copyright © 2011-2022 走看看