204 Count Primes

class Solution {
public:
    int countPrimes(int n) {
        int count=0;
        vector<bool>map(n+1,true);
        for(int i=2;i<n;i++){
            if(map[i]){
                count++;
                for(int j=i*2;j<n;j+=i)
                map[j]=false;
            }
        }
        return count;
    }
};

输出小于N质数的个数。

百度了一下埃拉托斯特尼筛法。。听说居然是小学奥数的东西。。

posted on 2015-05-28 04:38  88123  阅读(168)  评论(0编辑  收藏  举报