HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

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

这个题目就是让你求一组的素数有多少个。
这个素数范围的数字有点大,所以不能用打表。
测试数据很水。。。直接判断就能过了。
不过判断的时候,有一个地方需要注意的,我在那个判断素数的方法注释了。

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //boolean db[] = new boolean[2147483647];
        //数组太大,不能打表!
        //dabiao(db);
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            long sum = 0;
            int m;
            for(int i=0;i<n;i++){
                m=sc.nextInt();
                if(prime(m)){
                    sum++;
                }
            }
            System.out.println(sum);
        }
    }

    //直接判断能过,说明数据比较水。
    private static boolean prime(int m) {
        for(int i=2;i<=Math.sqrt(m);i++){
         //***** 注意:i*i<=m  是会超时的,因为i*i每次都要计算
            if(m%i==0){
                return false;
            }
        }
        return true;
    }

    //素数筛选打表应该会超时
    private static void dabiao(boolean[] db) {
        Arrays.fill(db, true);
        for(int i=2;i<=Math.sqrt(db.length);i++){
            for(int j=i+i;j<db.length;j+=i){
                if(db[j]){
                    db[j]=!db[j];
                }
            }
        }
    }
}
时间: 2024-10-23 16:30:56

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)的相关文章

hdu 2138 How many prime numbers

http://acm.hdu.edu.cn/showproblem.php?pid=2138 #include <iostream> #include <cstdio> #include <cmath> using namespace std; bool isprime(int m) { if(m == 1) return 0; int n=sqrt((double)m); for(int i=2; i<=n; i++) if(m%i == 0) return 1

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be

HDOJ 1397 Goldbach&amp;#39;s Conjecture(快速筛选素数法)

Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether

HDOJ(HDU) 2161 Primes(素数打表)

Problem Description Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. Input Each i

poj 2739 Sum of Consecutive Prime Numbers【素数筛】

我觉得这题的数据应该有些刁钻,一共至少提交了五次,一直是WA,无语了......用一个result数组存素数硬是不对.后来就算了,改用直接判断(法二),继续WA,后来发现是MAXN至少应为10002(被数据坑了),终于A掉了...... 后来继续改法一多次,任然WA,一直不清楚原因. 继续思考发现有一个很隐蔽的问题就是我后来用到   if(result[i]>n) break;    而result数组中 10000以内 最后一个素数是 9997,后面全是0了.这样无法停止,所以必须加一个大数作

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ/HDU 1088 Write a simple HTML Browser(HTML字符串)

Problem Description If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed. Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC

HDOJ(HDU) 2106 decimal system(进制相互转换问题)

Problem Description As we know , we always use the decimal system in our common life, even using the computer. If we want to calculate the value that 3 plus 9, we just import 3 and 9.after calculation of computer, we will get the result of 12. But af