반응형
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#define MAX 1000000 | |
using namespace std; | |
bool isPrime[MAX + 1]; | |
int num, t, T; | |
void Eratosthenes(int n) { | |
memset(isPrime, 1, sizeof(isPrime)); //init | |
for (int i = 2; i * i <= n; i++) { | |
if (!isPrime[i]) continue; | |
for (int j = i + i; j <= n; j += i) { | |
isPrime[j] = 0; | |
} | |
} | |
} | |
int main() { | |
Eratosthenes(MAX); | |
cin >> t; | |
while (t--) { | |
T++; | |
cin >> num; | |
cout << "Case #" << T << "\n"; | |
if (isPrime[num]) { | |
cout << "YES" << "\n"; | |
} else { | |
cout << "NO" << "\n"; | |
} | |
} | |
return 0; | |
} |
반응형
'알고리즘' 카테고리의 다른 글
[알고리즘] cpp 파이프 옮기기 (0) | 2020.09.20 |
---|---|
[알고리즘] 아기상어 (0) | 2020.09.20 |
[알고리즘] 스타트와 링크 (0) | 2020.09.20 |
[알고리즘] 시험 감독 (0) | 2020.09.20 |
[알고리즘] pair (0) | 2020.03.21 |
댓글