반응형
1978번: 소수 찾기
첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.
www.acmicpc.net
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int n, x, cnt = 0; bool not_prime[1001]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); // 에라토스테네스의 체 not_prime[1] = 1; for (int i = 2; i <= 1000; i++) { if (not_prime[i]) continue; for (int j = 2; i * j <= 1000; j++) not_prime[i * j] = 1; } cin >> n; while (n--) { cin >> x; if (!not_prime[x]) cnt++; } cout << cnt; } | cs |
반응형