HDU 2138 How many prime numbers

http://acm.hdu.edu.cn/showproblem.php?pid=2138

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

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N;
int num[maxn];

long long prime(long long x) {
    if(x == 1 || x == 2) return 1;
    else {
        for(long long i = 2; i * i <= x; i ++)
            if(x % i == 0)
                return 0;
    }
    return 1;
}

int main() {
    while(~scanf("%d", &N)) {
        int cnt = 0;
        for(int i = 1; i <= N; i ++) {
            scanf("%d", &num[i]);
            if(prime(num[i])) cnt++;
        }
        printf("%d\n", cnt);
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/9642922.html