AC

许诺  •  1天前


#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {

long long N;
cin >> N;

vector<pair<long long, int>> factors; // 存储质因数及其指数

// 处理质因数 2
if (N % 2 == 0) {
    int count = 0;
    while (N % 2 == 0) {
        count++;
        N /= 2;
    }
    factors.push_back({2, count});
}

// 处理奇数质因数
for (long long i = 3; i * i <= N; i += 2) {
    if (N % i == 0) {
        int count = 0;
        while (N % i == 0) {
            count++;
            N /= i;
        }
        factors.push_back({i, count});
    }
}

// 处理最后剩下的质数
if (N > 1) {
    factors.push_back({N, 1});
}

// 输出结果
bool first = true;
for (auto &factor : factors) {
    if (!first) {
        cout << " * ";
    }
    first = false;
    
    if (factor.second == 1) {
        cout << factor.first;
    } else {
        cout << factor.first << "^" << factor.second;
    }
}
cout << endl;

return 0;

}


评论:

请先登录,才能进行评论