끄적끄적 코딩
article thumbnail

소인수를 구하는 문제입니다.

n을 2~n까지 나눠봅니다.
나누어 떨어지면 안 나누어 떨어질 때까지 나눕니다.
그리고 나눈 횟수를 배열[나눈 수]에 저장합니다.


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[])
{
	int n;
	int temp;
	int ts;
	int arr[100010];

	cin >> ts;

	while (ts--) {
		memset(arr, 0, sizeof(arr));

		cin >> n;

		temp = n;

		for (int i = 2; i <= temp; ++i) {
			while (temp % i == 0) {
				temp /= i;
				++arr[i];
			}
		}

		for (int i = 2; i <= n; ++i) {
			if (arr[i]) {
				cout << i << " " << arr[i] << endl;
			}
		}
	}

	return 0;
}

검색 태그