• POJ3978


    /*
    Description


    A pretty straight forward task, calculate the number of primes between 2 integers. 


    Given 2 integers A ≤ B < 105 what’s the number of primes in range from A to B inclusive. 


    Note: A prime number is a positive integer greater than 1 and is divisible by 1 and itself only. For N to be prime it is enough to test the divisibility of numbers less than or equal to square root of N.
    Input


    As many as 1000 lines, each line contains 2 integers A and B separated by a space. Input is terminated when A = B = -1 (Do not process this line).
    Output


    For every line in input – except for the last line where A = B = -1 - print the number of prime numbers between A and B inclusive.
    Sample Input


    0 9999
    1 5
    -1 -1
    Sample Output


    1229
    3
    Source


    Seventh ACM Egyptian National Programming Contest
    */
    #include<iostream>
    using namespace std;
    bool isPrime(int n)//判断是否是素数
    {
        if(n==1||n==0) return false;//1不是素数
        if(n!=2&&n%2==0) return false;//2是素数,清除2的倍数
        for(int i=3; i*i<=n; i=i+2)
        {
            if(n%i==0) return false;
        }
        return true;
    }
    int main()
    {
        int a,b;
        int count;
        while(cin>>a>>b)
        {
            count=0;
            if(a==-1&&b==-1)
                break;
            for(int i=a; i<=b; i++)
            {
                if(isPrime(i)) count++;
            }
            cout<<count<<endl;
        }
        return 0;
    }

  • 相关阅读:
    投票通过,PHP 8 确认引入 Union Types 2.0
    Laravel 菜鸟的晋级之路
    给公司写的composer包开发的规范
    Swoft 源码剖析
    听说PHP的生成器yield处理大量数据杠杠的
    读懂JWT的使用,你就会用PHP如何实现了
    python标准库及其它应用
    python常用算法题
    python迭代器实例
    python生成器实例
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2978261.html
Copyright © 2020-2023  润新知