HDU ACM 2138 How many prime numbers
2015-11-29 01:06
274 查看
原题描述
Problem Description
Give you a lot of positive integers, just to find out how many prime numbers there are.Input
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.Output
For each case, print the number of prime numbers you have found out.Sample Input
32 3 4
Sample Output
2解题思路
本题求一组数中素数的个数。求一个数是否是素数,把这个数一次除以2到他的平方根的整数,如果某个结果能整除,则不是素数。
参考代码
#include <iostream> #include <cmath> using namespace std; bool isPrime(int x) // 调用函数比直接在main中判断更快 { if (x == 1)return false; for (int i = 2; i < sqrt(x); i++) // 只要到sqrt(x)即可 if (x%i == 0) return 0; return 1; } int main() { int n, c, s; while (~scanf("%d",&n)) { s = 0; for (int i = 0; i < n;i++) { scanf("%d", &c); if (isPrime(c)) s++; } printf("%d\n", s); } }
相关文章推荐
- c语言:求多项式1-1/2+1/3-1/4+...+1/99-1/100的值,3种循环实现
- Android手机出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
- Codeforces Round #333 (Div. 1)
- Hadoop家族学习路线图
- 【FSFA 读书笔记】Ch4 Volume Analysis & Cr 5 PC-based Partitions
- 10008---XML 配置里的 Bean 自动装配
- c语言:计算存款利息
- c语言:根据华氏温度求摄氏温度,并分析错误
- 阿里云服务器ECS部署应用教程
- linux app应用如何检测USB设备热插拔
- 编程心得02
- 【UI进阶】关于IB的理解,不知道这样是否可以
- opencv(1)
- 大数据和Hadoop生态圈
- Cobbler实现开机自动安装CentOS 7.1 X86_64
- [置顶] 关于web性能的思考与分享[06]——【原创】fis3构建工具使用教程(01)
- 1017. Queueing at Bank (25)——PAT (Advanced Level) Practise
- ls 命令详解
- 黑马程序员_java用java进行复制文件(考虑使用多线程),能系统自带快吗??
- jQuery关于通知栏自己滚动效果