HPU暑期第五次積分賽 F - 辭樹的質因數分解
阿新 • • 發佈:2018-11-01
題目
程式碼
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=1e6 + 100;
int ans[maxn],a[maxn];
int main()
{
int t,n,i,j;
cin>>t;
while(t--)
{
j=0;
memset(ans,0,sizeof(ans));
memset(a,0,sizeof(a));
cin>>n;
int k=n;
if(n==0 || n==1)
{
cout<<n<<endl;
continue;
}
for(i=2;i<=(k+1)/2;i++)
{
if(n%i==0)
{
a[j++]=i;
ans[i]=0;
while(n%i==0)
{
n/=i;
ans[i]++;
}
}
}
if (j==0)
{
cout<<n<<"=";
cout<<n<<"^"<<"1"<<endl;
}
else
{
cout<<k<<"=";
for(i=0;i<j;i++)
{
cout<<a[i]<<"^";
cout<<ans[a[i]];
if (i!=j-1) cout<<"*";
}
cout<<endl;
}
}
return 0;
}