直接上程序,注意要把N为1的情况排除掉.
#include <iostream>
#include <cmath>
#include <map>
using namespace std;
bool isPrime(long long N){
auto a= (long long)sqrt(N);
for(long long i=2;i<=a;i++){
if(N%i==0){
return false;
}
}
return true;
}
int main() {
int N;
cin>>N;
if(N==1){
cout<<"1=1"<<endl;
return 0;
}
long long a=N;
map<long long ,int> map1;
for (long long i = 2; i <=N ; ++i) {
if(isPrime(i)&&N%i==0){
int count=1;
N=N/i;
while (N%i==0){
count++;
N=N/i;
}
map1[i]=count;
}
}
cout<<a<<"=";
bool first=true;
for (auto pair:map1){
if(first) {
if (pair.second == 1) {
cout<<pair.first;
}else{
cout<<pair.first<<"^"<<pair.second;
}
first= false;
} else{
cout<<"*";
if (pair.second == 1) {
cout<<pair.first;
}else{
cout<<pair.first<<"^"<<pair.second;
}
}
}
return 0;
}