素数筛了。
代码:
#include <cstdio>
int prime[1000000];
void init()
{
for(int i = 2;i <= 65535;++i)
prime[i] = 1;
for(int i = 2;i <= 65535;++i)
{
if(prime[i])
{
for(int j = 2 * i;j <= 65535;j += i)
prime[j] = 0;
}
}
}
int nextPrime(int n)
{
for(int i = n + 1;;++i)
if(prime[i])
return i;
}
int main()
{
init();
int n;
while(~scanf("%d",&n))
{
if(prime[n])
{
printf("%d\n",n);
}
else
{
int cnt = 0;
int t = 2;
while(n!= 1)
{
if(n % t == 0)
{
if(!cnt)
printf("%d",t);
else
printf("*%d",t);
n /= t;
//printf("%d ",n);
cnt++;
}
else t = nextPrime(t);
}
printf("\n");
}
}
}