문제를 딱 보고.
아 이건! 미리 구해놓고 하나씩 출력하면 될듯~
에라토스테네스의 체를 복습했다.
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/4948 | |
* BOJ 백준온라인져지 4948 베르트랑 공준 풀이 | |
*/ | |
int main(){ | |
int N = 1; | |
bool isPrime[123456*2+1] = {}; | |
for(int i=2; i<=123456*2; i++) isPrime[i]=true; | |
for(int i=2; (i*i)<=123456*2; i++){ | |
if(isPrime[i]){ | |
for(int j=i*i; j<=123456*2; j+=i) isPrime[j]=false; | |
} | |
} | |
while(1){ | |
scanf("%d",&N); | |
if(N == 0) break; | |
int result = 0; | |
for(int i = N+1,length=2*N; i<=length; i++){ | |
if(isPrime[i]) result++; | |
} | |
printf("%d\n",result); | |
} | |
return 0; | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 1874 스택 수열 풀이 (0) | 2017.11.27 |
---|---|
BOJ 백준온라인져지 9020 골드바흐의 추측 풀이 (0) | 2017.11.25 |
BOJ 백준온라인져지 1181 단어 정렬 풀이 (0) | 2017.11.23 |
BOJ 백준온라인져지 1427 소트인사이드 풀이 (0) | 2017.11.22 |
BOJ 백준온라인져지 9426 중앙값 측정 풀이 (0) | 2017.11.21 |