Files
python/TangDou/AcWing/Prime/867.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

61 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##[$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;
}
```