728x90
자연수 X를 소인수분해한 수들을 출력하는 문제입니다.
1. 1부터 X까지 반복
2. X % n == 0 이면 출력
3. X / = n
4. 1~3 반복 (X가 1이 될 때까지)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
while (1) {
if (n == 1) {
break;
}
for (int i = 2; i <= n; ++i) {
if (n % i == 0) {
cout << i << endl;
n /= i;
break;
}
}
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 2981번 검문 (0) | 2019.09.03 |
---|---|
[C++] 백준 2609번 최대공약수와 최소공배수 (0) | 2019.09.02 |
[C++] 백준 1037번 약수 (0) | 2019.09.02 |
[C++] 백준 5086번 배수와 약수 (0) | 2019.09.02 |
[C++] 백준 1541번 잃어버린 괄호 (0) | 2019.09.02 |