题目地址:http://www.patest.cn/contests/pat-a-practise/1059
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <stack>
using namespace std;
bool isPrime(long int n)
{
if(n == 1)
return false;
if(n == 2)
return true;
long int i ;
for( i = 2 ;i*i <= n ; i++)
{
if( n % i == 0)
return false;
}
return true;
}
long int primes[3000];
struct data{
long int factor ;
int fcount ;
};
vector<data> vans ;
int main()
{
//freopen("in.txt", "r", stdin);
long int tmp = 3;
primes[0] = 2 ;
int num = 1 ;
while(tmp < 5000)
{
if(isPrime(tmp))
primes[num++] = tmp;
tmp += 2 ;
}
while(scanf("%d",&tmp) != EOF)
{
if(isPrime(tmp) || tmp == 1)
printf("%ld=%ld\n",tmp,tmp);
else{
printf("%ld=",tmp);
vans.clear();
num = 0;
while( !isPrime(tmp) )
{
while( tmp % primes[num] != 0)
{
num ++ ;
}
data dt ;
dt.factor = primes[num];
int count = 0;
while(tmp % dt.factor == 0)
{
count ++ ;
tmp /= dt.factor;
}
dt.fcount = count ;
vans.push_back(dt);
num = num + 1 ;
if( isPrime(tmp)){
dt.factor = tmp ;
dt.fcount = 1;
vans.push_back(dt);
break;
}
if(tmp == 1)
break;
}
printf("%d",vans[0].factor);
if(vans[0].fcount != 1)
{
printf("^%d",vans[0].fcount);
}
int len = vans.size();
for(int j = 1 ; j < len ; j++)
{
printf("*%d",vans[j].factor);
if(vans[j].fcount != 1)
{
printf("^%d",vans[j].fcount);
}
}
printf("\n");
}//else
} // while
return 0;
}