1978번: 소수 찾기
소수는 1과 자기 자신으로만 나눠지는 수이다. 예를 들어 2, 3, 5, 7 등이 있다.
여기서는 안 쓰이는 방법 같은데 N(자연수)까지의 소수의 개수를 구하는 많은 양을 작업할 때는 에라토스테네스의 체라는 것을 사용한다고 한다.
하지만 여기서 사용하진 않았다.
코드
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 <stdio.h> | |
int main() | |
{ | |
int n, cnt = 0; | |
scanf("%d", &n); | |
for (int i = 0; i < n; i++) { | |
int loopget; | |
scanf("%d", &loopget); | |
if (loopget >= 2) | |
{ | |
int flag = 1; | |
for (int j = 2; j * j <= loopget; j++) { | |
if (loopget % j == 0) | |
flag = 0; | |
} | |
cnt += flag; | |
} | |
} | |
printf("%d", cnt); | |
return 0; | |
} |
'온라인저지' 카테고리의 다른 글
[BOJ]1920번: 수 찾기 (0) | 2018.02.05 |
---|---|
[BOJ]2606번: 바이러스 (0) | 2018.02.05 |
[BOJ] 1932번: 숫자삼각형 (0) | 2018.02.03 |
[BOJ]2193번: 이친수 (0) | 2018.02.01 |
[BOJ]2669번: 직사각형 네개의 합집합의 면적 구하기 (0) | 2018.01.25 |
[BOJ]2667번: 단지번호붙이기 (0) | 2018.01.25 |