Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p
1
k
1
×p
2
k
2
×⋯×p
m
k
m
.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N = p
1
^k
1
*p
2
^k
2
*…*p
m
^k
m
, where p
i
's are prime factors of N in increasing order, and the exponent k
i
is the number of p
i
– hence when there is only one p
i
, k
i
is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^211171011291
代码
#include<stdio.h>
struct fac
{
int x;
int count=0;
}f[10];
const int max=1000001;
int p[max]={0};
int k=0;
bool fa[max]={false};
void pri()
{
for(int i=2;i<max;i++)
{
if(fa[i]==false)
{
p[k++]=i;
for(int j=i+i;j<max;j=j+i)
{
fa[j]=true;
}
}
}
}
int main()
{
void pri();
pri();
int n;
int i=0;
int t=0;
scanf("%d",&n);printf("%d=",n);
if(n==1)
{
printf("1");
return 0;
}
while(n!=1)
{
if(n%p[i]==0)
{
f[t].x=p[i];
while(n%p[i]==0)
{
f[t].count++;
n=n/p[i];
}
t++;
}
else
i++;
}
for(int j=0;j<t;j++)
{
printf("%d",f[j].x);
if(f[j].count!=1)
printf("^%d",f[j].count);
if(j!=t-1)
printf("*");
}
}
本文介绍了一种质因数分解算法,该算法能够将任意正整数N分解为其所有质因数的乘积形式,即N=p1^k1*p2^k2*...*pm^km,并提供了详细的算法实现代码。
738

被折叠的 条评论
为什么被折叠?



