AcWing 867. 分解质因数

题目传送门

#include <bits/stdc++.h>

using namespace std;
/**
 * 功能:分解质因数
 * @param x 待分解的质数因数
 */
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;
    cout << endl;
}


int main() {
    //读入优化
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        divide(x);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15341316.html