hdu-2138- How many prime numbers
2013-11-28 23:23
375 查看
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
3
2 3 4
Sample Output
2
意思就是说给了多组数字,每组都有很多数字,让你判断每组里的素数有多少个,方法很简单,不过不管是用c还是java,貌似都是用sqrt函数比较快些,大概就是这样,具体代码如下
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
3
2 3 4
Sample Output
2
意思就是说给了多组数字,每组都有很多数字,让你判断每组里的素数有多少个,方法很简单,不过不管是用c还是java,貌似都是用sqrt函数比较快些,大概就是这样,具体代码如下
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(new BufferedInputStream(System.in)); int num,sum,n; boolean isprime; while(cin.hasNext()) { num=cin.nextInt(); sum=0; for(int i=1;i<=num;++i) { n=cin.nextInt(); if(n==1) continue; if(n<4) { ++sum; continue; } isprime=true; double tem=Math.sqrt(n); for(int j=2;j<=tem;++j) { if(n%j==0) { isprime=false; break; } } if(isprime) ++sum; } System.out.println(sum); } } }
相关文章推荐
- hdu 2138 How many prime numbers(求素数)
- hdu 2138——How many prime numbers
- HDU 2138 How many prime numbers
- HDU 2138 How many prime numbers【素数判断,卡时间!!】
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
- 【HDU】2138 How many prime numbers
- HDU - 2138 How many prime numbers
- HDU 2138:How many prime numbers 【水】
- HDU 2138 How many prime numbers(米勒拉宾素数测试算法)
- HDU 2138 How many prime numbers
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
- HDU 2138 How many prime numbers
- HDU 2138 How many prime numbers
- HDU 2138 How many prime numbers(参考博客)
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
- HDU 2138 How many prime numbers
- HDU 题目2138 How many prime numbers
- HDU-2138 How many prime numbers
- HDu 2138 How many prime numbers 高效Miller素数测试
- hdu 2138 How many prime numbers