首页 > 代码库 > [PAT]素因子分解(20)

[PAT]素因子分解(20)

#include "stdio.h"#include "math.h"long Prime(long);long PrimeCount(long,long);int main(){    int a,curPrime,cfCount;    int v;    int tPrime=0;    scanf("%d",&a);    v=Prime(a);    curPrime=a;    printf("%d=",a);    if(v==1||v==a)    {        printf("%d",v);    }    else    {        tPrime=v;        while(tPrime!=curPrime)        {            cfCount=PrimeCount(curPrime,tPrime);                        if(cfCount>1)            {                printf("%d^%d",tPrime,cfCount);                curPrime=curPrime / pow((double)tPrime,cfCount);            }            else            {                printf("%d",tPrime);                curPrime=curPrime / pow((double)tPrime,cfCount);            }            tPrime=Prime(curPrime);            if(tPrime!=curPrime) printf("*");        }        if(tPrime!=1&&tPrime==curPrime)        {            printf("*%d",tPrime);        }    }    getchar();    getchar();    return 0;}long Prime(long num){    long i=0;    if(num<2) return 1;    for(i=2;i<num;i++)    {        if(num%i==0) return i;    }    return num;}long PrimeCount(long num,long cellnum){    long c=0;    for(;num%cellnum==0;)    {        c++;        num/=cellnum;    }    return c;}

 

[PAT]素因子分解(20)