끄적끄적 코딩
article thumbnail
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;
}

검색 태그