解法一:筛法求素数
筛法求素数,然后从小到大判断。
#include <bits/stdc++.h>using namespace std;const int MAX_N = 100000;vector<int> primes;void getPrimes() {bool flag[MAX_N];fill(flag, flag + MAX_N, true);for (int i = 2; i <= MAX_N; ++i) {if (flag[i]) {for (int j = i << 1; j <= MAX_N; j += i) {flag[j] = false;}primes.emplace_back(i);}}}int main() {ios::sync_with_stdio(false);cin.tie(0);getPrimes();int N;cin >> N;if (N == 1) {cout << "1=1\n";return 0;}cout << N << "=";for (auto prime:primes) {if (N == 1) {break;}int cnt = 0;while (N % prime == 0) {cnt++;N /= prime;}if (cnt > 0) {if (cnt == 1) {if (N == 1) {cout << prime << "\n";return 0;} else {cout << prime << "*";}} else {if (N == 1) {cout << prime << "^" << cnt << "\n";return 0;} else {cout << prime << "^" << cnt << "*";}}}}}
