PTA 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 = 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^2*11*17*101*1291
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include <numeric>
#include <climits>//INT_MAX
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
//#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int  maxn=100010;
bool is_prime(int n)
{
    if(n==1)
        return false;
    int sqr=(int)sqrt(n * 1.0);
    for (int i=2; i<=sqr;i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}

int prime[maxn], pct=0;
void fd_prime()
{
    for (int i=1; i<maxn;i++)
    {
        if(is_prime(i)==true)
            prime[pct++]=i;
    }
}
struct node{
    int x;
    int ct;
} P[10];
int main()
{
    fd_prime();
    int n,num=0;
    cin>>n;
    if(n==1)
        printf("1=1");
    else{
        printf("%d=", n);
        int sqr=(int)sqrt(n*1.0);
        for (int i=0; i<pct&&prime[i]<=sqr;i++)
        {
            if(n%prime[i]==0)
            {
                P[num].x=prime[i];
                P[num].ct=0;
                 while(n%prime[i]==0)
                {
                    P[num].ct++;
                    n/=prime[i];
                }
                num++;
            }
            if(n==1)
                break;
        }
        if(n!=1){
            P[num].x=n;
            P[num++].ct=1;
        }
        for (int i=0; i<num;i++)
        {
            if(i>0)
                cout<<'*';
            cout<<P[i].x;
            if(P[i].ct>1)
                cout<<'^'<<P[i].ct;
        }
    }
    return 0;
}

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页