多組測試,每組給 N 個整數(≥2,≤ 2³¹-1),統計質數個數。
#include <cstdio>
#include <cmath>
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
int limit = (int)sqrt(n);
for (int i = 3; i <= limit; i += 2)
if (n % i == 0) return false;
return true;
}
int main() {
int n;
while (scanf("%d", &n) == 1) {
int cnt = 0;
for (int i = 0, x; i < n; ++i) {
scanf("%d", &x);
if (isPrime(x)) ++cnt;
}
printf("%d\n", cnt);
}
return 0;
}