利用试除法:
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;//输入该数字
int tp = n;
int *cnt = new int[n+1];
memset(cnt, 0, (n+1)*sizeof(int));//清空数组
for (int i = 2; i<=n; i++) {
while (tp%i==0) {//即该数字可以被2整除
tp = tp/I;//尽可能除尽2
cnt[i]++;//除的次数增加
}
}
//输出答案
cout<<"=";
bool isFirst = true;
for (int i = 2; i<=n; i++) {
if (cnt[i]!=0) {
if (isFirst) {
isFirst = false;
}else{
cout<<"+";
}
cout<<i<<"^"<<cnt[i];
}
}
cout<<endl;
return 0;
}