国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode 204. Count Primes

2019-11-14 12:21:42
字體:
來源:轉載
供稿:網友

解題思路:維基百科中 https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 

 Input: an integer n > 1.  Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.  for i = 2, 3, 4, ..., not exceeding √n:   if A[i] is true:     for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:       A[j] := false.  Output: all i such that A[i] is true.

class Solution {public:        int countPRimes(int n) {            if (2 >= n) return 0;            vector<bool>primes(n,true);            int sqr = sqrt(n - 1);            for (int i = 2; i <= sqr; ++i){                if (primes[i]){                    for (int j = i * i; j < n; j += i)                    primes[j] = false;            }            }            int sum = 0;            for (int i = 2; i < n; ++i)                sum += (primes[i]) ? 1 : 0;            return sum;    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延吉市| 都昌县| 密云县| 色达县| 宁明县| 隆德县| 印江| 兰溪市| 小金县| 台南县| 大宁县| 静安区| 淮北市| 永川市| 炉霍县| 荣昌县| 台州市| 剑河县| 保康县| 江都市| 沙洋县| 宁武县| 高要市| 盐城市| 东阿县| 宝山区| 高淳县| 兰考县| 石屏县| 黑水县| 枣庄市| 赣榆县| 泰和县| 平潭县| 萝北县| 思茅市| 泊头市| 集安市| 新乐市| 集贤县| 永康市|