zoukankan      html  css  js  c++  java
  • 1059 Prime Factors

    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 = 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^2*11*17*101*1291


    /*
        Name:
        Copyright:
        Author:  流照君
        Date: 2019/8/6 11:09:58
        Description:
    */
    #include <iostream>
    #include<string>
    #include <algorithm>
    #include <vector>
    #include<cmath>
    #define inf 0x3f3f3f
    using namespace std;
    typedef long long ll;
    ll prime[inf],a[inf];
    ll num=1;
    void sieve(int n)
    {
        for(int i=2;i<=n;i++)
        {
            if(a[i]==0)
            prime[num++]=i;
            for(int j=i*2;j<=n;j=j+i)
            {
                a[j]=1;
            }
        }
    }
    int main(int argc, char** argv)
    {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        fill(a,a+inf,0);
        ll n,flag=0;
        cin>>n;
        sieve(500000);
        //for(int i=1;i<=num;i++)
        //cout<<prime[i]<<" ";
        //cout<<endl;
        if(n==1)
        {
            cout<<n<<"="<<"1";
            return 0;
        }
        cout<<n<<"=";
        for(int i=1;i<num;i++)
        {
            int sum=0;
            while(n%prime[i]==0)
            {
                n=n/prime[i];
                sum++;
            }
            if(flag&&sum)
            {
                if(sum==1)
                    cout<<"*"<<prime[i];
                if(sum>=2)
                {
                    cout<<"*"<<prime[i]<<"^"<<sum;
                }    
            }
            if(flag==0&&sum)
            {
                if(sum==1)
                    cout<<prime[i];
                if(sum>=2)
                {
                    cout<<prime[i]<<"^"<<sum;
                }
                flag=1;
            }
            if(n==1)
            return 0;
        }
        return 0;
    }

    别忘了考虑特例 1

  • 相关阅读:
    Haproxy图解
    Keeplived 配制图解
    日志文件 的管理 logrotate 配置
    Haproxy+MYSQL 负载均衡 原创
    MySQL内存----使用说明全局缓存+线程缓存) 转
    MYSQL内存--------启动mysql缓存机制,实现命中率100% 转
    MYSQL SQL 审核工具 (inception安装步骤)
    MHA手动切换 原创4 (非交互式切换)
    MHA手动切换 原创2 (主参与复制)
    MHA手动在线切换主 原创3(主不参与复制)
  • 原文地址:https://www.cnblogs.com/liuzhaojun/p/11308578.html
Copyright © 2011-2022 走看看