您的位置:首页 > 其它

leetcode 204. Count Primes

2016-10-19 16:22 381 查看
/*
**leetcode 204. Count Primes**
Description:

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

题目大意:求小于n个素数的个数
解题思路:打表,素数筛选法求素数,然后统计。
*/

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
int countPrimes(int n)
{
//打表
vector<int> isPrime(n, true);
for (int i = 2; i * i <= n; ++i)
{
if (isPrime[i])
{
//2...i,把这些数的倍数不是素数
for (int j = i *i; j < n; j += i)
isPrime[j] = false;
}
}

int ret = 0;
for (int i = 2; i < n; ++i)
if (isPrime[i])
++ret;

return ret;
}

};

void test()
{
Solution sol;
cout << sol.countPrimes(10) << endl;
}

int main()
{
test();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: