You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3 KiB

2 years ago
##[$AcWing$ $867$. 分解质因数](https://www.acwing.com/problem/content/description/869/)
### 一、题目描述
给定 $n$ 个正整数 $a_i$,将每个数分解质因数,并按照质因数从小到大的顺序输出每个质因数的底数和指数。
**输入格式**
第一行包含整数 $n$。
接下来 $n$ 行,每行包含一个正整数 $a_i$。
**输出格式**
对于每个正整数 $a_i$,按照从小到大的顺序输出其分解质因数后,每个质因数的底数和指数,每个底数和指数占一行。
每个正整数的质因数全部输出完毕后,输出一个空行。
**数据范围**
$1≤n≤100,2≤a_i≤2×10^9$
**输入样例:**
```cpp {.line-numbers}
2
6
8
```
**输出样例:**
```cpp {.line-numbers}
2 1
3 1
2 3
```
### 二、实现代码
```cpp {.line-numbers}
#include <bits/stdc++.h>
using namespace std;
void divide(int x) {
for (int i = 2; i <= x / i; i++)
if (x % i == 0) {
int s = 0;
while (x % i == 0) x /= i, s++;
cout << i << ' ' << s << endl;
}
if (x > 1) cout << x << ' ' << 1 << endl;
}
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
divide(x);
puts("");
}
return 0;
}
```