呃,代码很长,用了快读,快输,还有快速的质数判断法:
#include <bits/stdc++.h>
using namespace std;
bool isnumber(char ch) {
if (ch >= '0' && ch <= '9')
return true;
return false;
}
int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isnumber(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isnumber(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void write(int x) {
char f[205];
int tmp = x > 0 ? x : -x;
if (x < 0)
putchar('-');
int cnt = 0;
while (tmp > 0) {
f[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) {
putchar(f[--cnt]);
}
}
bool isPrime(int num) {
if (num < 2)
return false;
if (num == 2 || num == 3)
return true;
if (num % 6 != 1 && num % 6 != 5)
return false;
int temp = sqrt(num);
for (int i = 5; i <= temp; i += 6)
if (num % i == 0 || num % (i + 2) == 0)
return false;
return true;
}
int main() {
int n = read(), a[100005];
for (int i = 0; i < n; i++) {
a[i] = read();
if (isPrime(a[i])) {
write(a[i]);
printf(" ");
}
}
return 0;
}
如果数据更庞大更可怕一点,我的代码就会体现出足够优势