63 lines
1.3 KiB
C++
63 lines
1.3 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
|
||
//判断是不是质数
|
||
bool isPrime(int n) { //最快的方法
|
||
if (n < 2) return false;
|
||
if (n == 2 || n == 3) return true;
|
||
if (n % 6 != 1 && n % 6 != 5) return false;
|
||
for (int i = 5; i <= floor(sqrt(n)); i += 6)
|
||
if (n % i == 0 || n % (i + 2) == 0)
|
||
return false;
|
||
return true;
|
||
}
|
||
|
||
int main() {
|
||
int n;
|
||
cin >> n;
|
||
cout << n << "=";
|
||
int a[100] = {0};
|
||
int b[100] = {0};
|
||
|
||
int k = 0;
|
||
for (int i = 2; i <= n / 2; ++i) {
|
||
if (isPrime(i)) {
|
||
a[k] = i;
|
||
k++;
|
||
}
|
||
}
|
||
|
||
|
||
for (int i = 0; i < k;) {
|
||
if (n % a[i] == 0) {
|
||
n = n / a[i];
|
||
b[i]++;
|
||
} else {
|
||
i++;
|
||
}
|
||
}
|
||
|
||
|
||
//最后一个不是0的,就是停止位
|
||
int lastIndex=0;
|
||
for (int i = k-1; i >=0 ; i--) {
|
||
if(b[i]>0) {
|
||
lastIndex=i;
|
||
break;
|
||
}
|
||
}
|
||
|
||
//输出
|
||
for (int i = 0; i <= lastIndex; i++) {
|
||
if (b[i] > 1) {
|
||
cout << a[i] << "^" << b[i];
|
||
if (i < lastIndex) cout << "*";
|
||
} else if (b[i] == 1) {
|
||
cout << a[i];
|
||
if (i < lastIndex) cout << "*";
|
||
}
|
||
}
|
||
return 0;
|
||
}
|