PAT-1059 Prime Factors (25分)【素数】

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 pm^km.

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 = p1^k1 * p2^k2 pm^km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.

Sample Input:
97532468

Sample Output:
97532468=2^211171011291

先用素数晒法把求出素数然后再进行枚举

#include<bits/stdc++.h>
using namespace std;

#define  ll long long
int a[100005];
int  prime[100005];
int tot;

void make_prime()
{
    memset(a,1,sizeof(a));
    a[0]=0,a[1]=0;
    tot=0;
    for(int i=2; i<=100005; i++)
    {
        if(a[i])
        {
            prime[tot++]=i;
            for(int j=i+i; j<=100005; j+=i)
            {
                a[j]=0;
            }
        }
    }
}
int main()
{
    make_prime();
    ll n;
    scanf("%lld",&n);
    if(n==1)
    {
        printf("1=1");
        return 0;
    }
    printf("%lld=",n);
    int cnt=0;
    for(int i=0; i<tot&&n>=2; i++)
    {
        cnt=0;

        while(n%prime[i]==0)
        {
            n/=prime[i];
            cnt++;
        }
        if(cnt==0) continue;
        if(cnt==1)
        {
            printf("%d",prime[i]);
        }
        else
        {
            printf("%d^%d",prime[i],cnt);
        }
        if(n>1)
        {
            printf("*");
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 又是一场秋雨 下得竟这般淅淅漓漓 天空默不作声的哭泣 打破了这座千年古城的静寂 似喜非喜的面容 若同黛玉般弱柳拂风...
    晴空阳树阅读 422评论 1 2
  • 《娃近况》 宝贝,宝贝,吃喝拉撒睡。 想哭就哭,眼角刚含泪。 流行小脸,左右躺不累。 婴儿专用,给你独特位。 家人...
    昔青散文阅读 222评论 0 0
  • 为了应合本文的主旨“分期付款”,我刚查了一下自己的花呗分期,这个月是还清了,下个月还有1948元的消费未入账,这钱...
    化浊阅读 336评论 0 0