examination questions

Description:

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

Please use the following function to solve the problem:

int countPrimes(int n){

}


解题代码

int countPrimes(int n) {
if (n == || n == || n == ){
return ;
}
if (n == ){
return ;
} int temp = ;
bool flag = false;
int arr[] = { '\0' };
int k = ;
arr[] = ; for (int i = ; i < n; i++){
for (int j = ; j <= k; j++){
if (i%arr[j] == ){
flag = true;
break;
}
} if (flag == false){
if (k < ){
k++;
arr[k] = i;
          i++;
}
temp++;
}
else{
flag = false;
}
} return temp+;
}

基本算法思想

判断一个数是否是质数, 仅需判断这个数是否能被比这个数小的质数整除, 若不能, 就是质数.

代码注释分析

int countPrimes(int n) {
//题目要求输入的测试值是非负数,所以必须包含0,1的特殊情况
//由于2的结果也是0,所以也包含了进去
if (n == || n == || n == ){
return ;
}
//为了方便后面的算法设计,要单独把3拿出来
if (n == ){
return ;
} int temp = ;//定义一个计数器,用来记有多少个质数
bool flag = false;//标记,用来判断该数是否是质数
int arr[] = { '\0' };//最为基数的质数的量的最大值设置为200,可以测试10的6次方量级
int k = ;//用来计数质数的个数的,也就是arr的下标
arr[] = ;//第一个质数赋值为2 for (int i = ; i < n; i++){ //这个循环符合n>=4的情况,遍历所有小于n的数,一一进行检测
for (int j = ; j <= k; j++){ //如果这个数能被arr[0]~arr[k]整除,说明它不是质数,flag变为true
if (i%arr[j] == ){
flag = true;
break;
}
} if (flag == false){ //如果flag没有变成true,那么说明它是质数
if (k < ){ //我们要求arr质数仅需要200个,超出的不计入!
k++;
arr[k] = i; //把这个质数也加入arr数组中
i++;//一个质数被判断为质数后,它的后面一个数字不可能是质数(除了2和3之外),所以用i++来减少对不不要数的检测
}
temp++; //质数量+1
}
else{
flag = false; //把flag 再初始化为false,回到最初状态,用于判断下一个数值
}
} return temp + ;//+1是因为要加上 2 这个质数,上面的temp中不包括2
}

此外, 有以下解题方法供参考(由 stevenczp 提供):

int countPrimes(int n) {
bool* map = (bool*)malloc(n * sizeof(bool));
memset(map, , n * sizeof(bool)); for (int i = ; i <= sqrt(n); i++)
{
if (map[i])
continue;
int t = * i;
while (t < n)
{
map[t] = true;
t += i;
}
} int result = ;
for (int i = ; i < n; i++)
{
if (!map[i])
result++;
}
return result;
}

关于本题的详细解题过程, 请点击这里:

解决一道leetcode算法题的曲折过程及引发的思考

Count Primes - LeetCode的更多相关文章

  1. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  2. Count Primes ——LeetCode

    Description: Count the number of prime numbers less than a non-negative number, n. 题目大意:给一个int,返回小于它 ...

  3. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  4. [leetcode] Count Primes

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

  5. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  6. 【刷题-LeetCode】204. Count Primes

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

  7. LeetCode_204. Count Primes

    204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...

  8. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  9. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. BizTalk开发系列(二十二) 开发自定义Map Functoid

    尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作,但仍可能会遇到需要其他方法的情况.<BizTalk开发系列 Map扩展开发>介绍了通过使用自定义 XSL ...

  2. java.lang.UnsupportedClassVersionError: Bad version number in .class file异常

    java.lang.UnsupportedClassVersionError: Bad version number in .class file异常 部署工程时也出现过因为版本不同引起的问题,那时我 ...

  3. setAlpha方法 设置透明度

    public void setAlpha (int x) 其中,参数x为透明度,取值范围为0~255,数值越小越透明.

  4. UIBezierPath类 笔记

    使用UIBezierPath类可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状.     ...

  5. App 打包并跳过 AppStore 的发布下载

    一.App 打包 (编译 -> 链接 -> 打包) 1) 下载发布版的证书并安装. 2)Target -> Build Setting,改为发布版本的 profile 3) Targ ...

  6. [转]倍数提高工作效率的 Android Studio 奇技

    转自:http://android.jobbole.com/81687/ 倍数提高工作效率的 Android Studio 奇技 2015/10/08 · 技术分享 · 4 评论· Android S ...

  7. 动画 CABasicAnimation animationWithKeyPath 一些规定的值

    CABasicAnimation animationWithKeyPath Types When using the ‘CABasicAnimation’ from the QuartzCore Fr ...

  8. redis集群配置

    客户端分片 程序端实现 代理proxy,访问proxy,proxy指定redis保存位置. Twemproxy Redis cluster ,会造成一部分数据丢失,无中心化1.将数据自动切分(spli ...

  9. 解析Json需要设置Mime

    IIS6.0 1.打开IIS添加Mime项 关联扩展名:*.json内容类型(MIME):application/x-javascript      2.添加映射: 位置在IIS对应站点右键属性:”主 ...

  10. PL/SQL不支持64位Oracle Client 解决办法

    解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed 说明PLSQL Developer并不支持Or ...