에라토스테네스의 체의 방법을 사용하는데, 소수를 구하는게 목적이 아니고 K 번째 지워지는 숫자를 구하는게 목적
This file contains 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 <cstdio> | |
/** | |
* https://www.acmicpc.net/problem/2960 | |
* BOJ 백준온라인져지 2960 에라토스테네스의 체 풀이 | |
*/ | |
int main(){ | |
int N,K; | |
scanf("%d%d",&N,&K); | |
bool *isPrimeNumber = new bool[N+1]; | |
int count = 0, result = 0; | |
for(int i = 2; i <= N; i++){ | |
if(!isPrimeNumber[i]){ | |
for(int j = i; j <= N; j += i){ | |
if(!isPrimeNumber[j]){ | |
isPrimeNumber[j] = true, count++; | |
if(count == K) result = j; | |
} | |
} | |
} | |
} | |
printf("%d", result); | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 1016 제곱 ㄴㄴ 수 풀이 (0) | 2017.12.06 |
---|---|
BOJ 백준온라인져지 6588 골드바흐의 추측 풀이 (0) | 2017.12.06 |
BOJ 백준온라인져지 2490 윷놀이 풀이 (0) | 2017.12.05 |
BOJ 백준온라인져지 10872 팩토리얼 풀이 (0) | 2017.12.05 |
BOJ 백준온라인져지 1037 약수 풀이 (0) | 2017.12.05 |