728x90
소인수를 구하는 문제입니다.
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;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 2743번 단어 길이 재기 (0) | 2019.09.09 |
---|---|
[C++] 백준 1158번 조세퍼스 문제 (0) | 2019.09.09 |
[C++] 백준 2448번 별 찍기 - 11 (0) | 2019.09.09 |
[C++] 백준 2902번 KMP는 왜 KMP일까? (0) | 2019.09.09 |
[C++] 백준 11365번 !밀비 급일 (0) | 2019.09.09 |