zoukankan      html  css  js  c++  java
  • PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488

    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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    long int P;
    int prime[500010];
    
    int main() {
        memset(prime, 1, sizeof(prime));
        for(int i = 2; i * i <= 500010; i ++)
            for(int j = 2; j * i < 500010; j ++)
            prime[j * i] = 0;
        scanf("%ld", &P);
    
        printf("%ld=", P);
        if(P == 1) printf("1");
    
        bool First = false;
        for(int i = 2; P >= 2; i ++) {
            int cnt = 0, flag = 0;
            while(prime[i] && P % i == 0) {
                cnt ++;
                P /= i;
                flag = 1;
            }
            if(flag) {
                if(First)
                    printf("*");
                printf("%d", i);
                First = true;
            }
            if(cnt > 1)
                printf("^%d", cnt);
        }
        return 0;
    }
    

      素数表! Get

  • 相关阅读:
    Arduino
    DTU
    现代信号处理与应用
    matlab学习记录
    列车准点节能操纵
    泊松过程
    序号生成算法odoo
    操作系统特性
    c语言中的变量
    xml中的四则运算与时间爱格式
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10185932.html
Copyright © 2011-2022 走看看