• PAT 1059 Prime Factors[难]


    1059 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 = 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

     题目大意:给出一个数n,在long int内,给出其素数分解,如果某个素数有多个是其分数,那么用指数形式表示;如果n本身就是素数,那么指数1不输出。

    //猛一看,自己就想到了是建立大数素数表,但是具体操作我似乎不大会。以前看过,但是不知道具体怎么写了。

    //自己写了一点点就蔫了。
    #include <iostream>
    #include <algorithm>
    #include<cstdio>
    #include<stdio.h>
    #include <map>
    #include<cmath>
    using namespace std;
    //需要先求出long int内所有的素数。
    //但是有一个问题,如果是long int,那么我定义多大的数组呢?要么用向量?
    #define maxn 1e7
    int a[maxn];
    vector<int> vt;
    bool isPrime(int n){
        int m=sqrt(n);
        for(int i=2;i<=n;i++){
            if(n%i==0)return false;
        }
        return true;
    }
    
    void getPrime(int n){
        for(int i=2;i<=n;i++){
            if(a[i]==0){
                if(isPrime(i)){
                    a[i]=1;
                    for(int j=i*i;j<=n;j=j*i){//是按照立方去标记吗?
                        
                    }
                }
            }
        }
    }
    int main()
    {
        long int n;
        cin>>n;
        getPrime(n);
        return 0;
    }

     下面是柳神的代码:

    1.原来求大数内素数还可以这么求,学习了。

    2.并且在判断时可以边判断边输出。

  • 相关阅读:
    C对字符串的部分操作
    <string> <string.h>
    最常见的HTTP错误
    python面试题
    玩转type类型(牛逼克拉斯 )
    django路由系统之反向生成url
    django事物回滚
    django中admin路由系统工作原理
    django中的django admin插件
    ajax跨域资源共享
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9938935.html
Copyright © 2020-2023  润新知