Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 = p?1???k?1????×p?2???k?2????×?×p?m???k?m????. Input Specification: Each input file cont

Given any positive IntegeN,you are supposed to find all of its prime factors,and write them in the format N = @H_262_37@p?1???k?1????×@H_262_37@p?2???k?2????×?×@H_262_37@p?@H_375_53@m???k?@H_375_53@m????.

Input Specification:

Each input file contains one test case which gives a positive IntegeN in the range of long int.

Output Specification:

Factor N in the format = p?1??^k?1??*p?2??^k?2??**p?@H_375_53@m??^k?@H_375_53@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^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

@H_801_884@

大佬总结

以上是大佬教程为你收集整理的1059 Prime Factors全部内容,希望文章能够帮你解决1059 Prime Factors所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:prime