Count Primes

原创 2015年07月07日 15:52:06

参考了官网hint

代码如下:

class Solution {
public:
    int countPrimes(int n) {
        bool *arr = new bool[n + 1];

        for(int i = 2; i < n; i++)
            arr[i] = true;

        for(int i = 2; i * i < n; i++) {
            for(int j = i * i; j < n; j = j + i) {
                arr[j] = false; 
            }
        }

        int counter = 0;
        for(int i = 2; i < n; i++) {
            if(arr[i]) 
                counter++;
        }

        return counter;
    }
};

HDU 5901 Count Primes (模板 + 数论知识)——2016 ACM/ICPC Asia Regional Shenyang Online

传送门 Count primesTime Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others...

【leetcode】【204】Count Primes

一、问题描述 Description: Count the number of prime numbers less than a non-negative number, n. ...

[LeetCode] Count Primes

Count Primes Description: Count the number of prime numbers less than a non-negative number, n ...

leetcode 204. Count Primes-素数统计

原题链接:204. Count Primes 按照题目“Show Hint”提示,如果定义一个函数,判断这个数是否是素数,那么效率会很低: public int countPrimes(int...

leetcode 204: Count Primes

问题描述:Description:Count the number of prime numbers less than a non-negative number, n.

LeetCode 204 - Count Primes

一、问题描述Description:Description:Count the number of prime numbers less than a non-negative number, nHi...

hdu 5901 Count primes

题意输出[1..n]的质数个数 (1

leetcode笔记:Count Primes

题目有很多tips,大意是算出2~n之间有多少个素数。 思路来自著名的埃拉托斯特尼筛法。...

[LeetCode] Count Primes - 素数系列问题

题目概述: Description: Count the number of prime numbers less than a non-negative number, n. 题意是给出n中所有素数...

LeetCode 204:Count Primes

Description: Count the number of prime numbers less than a non-negative number, n 分析: 题目要求计算小于N的所有...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Count Primes
举报原因:
原因补充:

(最多只允许输入30个字)