Algorithm/Problem Solving
[BOJ/11653] 소인수분해
DevMoomin
2020. 5. 16. 14:59
(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)
문제 링크
- https://www.acmicpc.net/problem/11653
사용 알고리즘
-
풀이
- 2부터 루트N까지 루프를 돌며 나눠지지 않을 때 까지 계속 나누며 출력
- N 자체가 소수일 수도 있으므로 해당 부분 처리
소스 코드
- https://github.com/moomini/algorithm/tree/master/boj
#include <cstdio>
int main(void) {
int n; scanf("%d", &n);
for (int i = 2; i * i <= n; ++i) {
while (n % i == 0) {
printf("%d\n", i);
n /= i;
}
}
if (n > 1) printf("%d\n", n);
return 0;
}
|
cs |