How many prime numbers(求素数个数)
2015-12-20 15:31
459 查看
How many prime numbers
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14684 Accepted Submission(s): 5091[align=left]Problem Description[/align]
Give you a lot of positive integers, just to find out how many prime numbers there are.
[align=left]Input[/align]
There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.
[align=left]Output[/align]
For each case, print the number of prime numbers you have found out.
[align=left]Sample Input[/align]
3
2 3 4
[align=left]Sample Output[/align]
2
题解:
水暴力,打表不行,由于数据量太大,运行到sqrt(x)就可以;
代码:
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; bool js(int x){ if(x==2)return true; if(x==1||x==0)return false; if(x%2==0)return false; for(int i=3;i<=sqrt(x);i+=2){ if(x%i==0)return false; } return true; } int main(){ int N,a; while(~scanf("%d",&N)){ int cnt=0; for(int i=0;i<N;i++){ scanf("%d",&a); if(js(a))cnt++; } printf("%d\n",cnt); } return 0; }
相关文章推荐
- 插件推荐:RTImageAssets 自动生成 AppIcon 和 @2x @1x 比例图片
- struct结构在声明时对其中的某一项进行初始化
- 阿里感悟(十四)-如何带新员工(转)
- HTML5拖放API(代码展示)
- WordPress菜单“显示选项”无法显示的解决办法
- 我在数据库方面踩过的"坑"
- 二维码生成
- LeetCode 021
- PHP GD库常用功能封装
- Java NIO系列教程(三) Buffer(转)
- shell脚本错误:syntax error near unexpected token '$'\r''
- python核心编程-继承
- C#基础之——C#总观
- WebStorm,Chrome,Nodejs实现Angularjs所见即所得
- 自定义LinkedList集合
- leetcode || Spiral Matrix
- HTML5拖放API(相关实例)
- MySQL 基础常用命令
- MySQL好用的数学函数
- Jquery技巧:使用ajax技术提交表单数据