当前位置:网站首页>Pat 1059 prime factors (25 points) prime table

Pat 1059 prime factors (25 points) prime table

2022-07-04 12:07:00 Python ml

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>prime(50000,1);
int main() {
       
    for(int i=2;i*i<50000;i++)
        for(int j=2;j*i<50000;j++)
            prime[j*i]=0;             //50000 Any inside can be decomposed into j*i None of the numbers are prime numbers , And one of the primes i It must be less than sqrt(50000)
    long int a;   
    scanf("%ld",&a);
    printf("%ld=",a);
    if(a==1)printf("1");
    bool state=false;
    for(int i=2;i<50000&&a>2;i++){
    
        int cnt=0,flag=0;
        while(prime[i]==1&&a%i==0){
             // Factors can be decomposed from small to large i,i There may be more than one , Always cycle division i
            cnt++;
            a=a/i;
            flag=1;
        }
        if(flag){
    
            if(state)printf("*");     // The second factor begins with *
            printf("%d",i);
            state=true;
        }
        if(cnt>=2)printf("^%d",cnt);
    }
    if(a>1)printf("%s%ld",state?"*":"",a);  //state Indicates that it can be decomposed ,a Is the last prime factor , Otherwise print directly n
    system("pause");
    return 0;
}
原网站

版权声明
本文为[Python ml]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141330373752.html