题意:

给出一个int型正整数N,要求把N分解成若干个质因子,如N=97532468,则把N分解成:97532468=2^2*11*17*101*1291。质因子按增序输出,如果某个质因子的幂是1,则1不输出。

思路:质因子分解的基础题。

首先,定义如下因子的结构体,用于存放最终的结果。因为N是一个int范围的正整数,由于2*3*5*7*11*13*17*19*23*29>INT_MAX,也就是说,任意一个int型整数,可分解出来的不同的质因子的个数不会超过10个,因此,数组只要开到10就够了。

struct Factor{
    int fac;//质因子
    int cnt;//质因子出现的次数
}factor[];

在对正整数N进行分解之前,首先要获取素数表,令其存在数组prime中,这样一来,则逐个判断这个prime数组中的素数,若整除,则一直用N除以当前这个素数,记录这个素数出现的次数,除到不能整除为止,再进入下一个素数的判断。

但是,如果我们要求N=2,147,483,647(INT_MAX)的质因子呢?难道需要求出所有整型的素数吗?这样做当然没有错,但却会超时(判断素数的时间复杂度是O(sqrt(N)),枚举获取1~N的全部素数的时间复杂度是O(N),因此总的时间复杂度是O(N*sqrt(N)),若N>10^5基本就超时了),事实上,对于质因子分解,需要明白这样一个事实——

对于任何一个整数,如果它是素数,则不可被分解,因子只有1和其本身;如果它是合数,则除了1和本身之外,它的因子必然是在sqrt(n)两侧成对出现的,此时,这些质因子要么全部小于等于sqrt(n);要么只存在一个质因子大于sqrt(n),而其他质因子全部小于等于sqrt(n)。基于此,我们考虑,int型的最大值是2,147,483,647,而sqrt(2,147,483,647)≈46,341,根据刚才所说的结论,也就是说一个int型整数若不能被46341以内的素数整除的话,说明因子就是其本身了。因此,我在获取素数表的getPrime()函数中直接硬编码了。

代码:

#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;

struct Factor{
    int fac;//质因子
    int cnt;//质因子出现的次数
}factor[];

vector<int> prime;//素数表

//判断素数
bool isPrime(int n)
{
    ) return false;
    int sqr=(int)sqrt(n);
    ;i<=sqr;i++)
        ) return false;
    return true;
}

//获取素数表,打表思想
void getPrime()
{
    ;i<;i++)
        if(isPrime(i)) prime.push_back(i);
}

int main()
{
    int val;
    scanf("%d",&val);
    getPrime();
    int temp=val;
    ;//factor数组的长度
    ;i<prime.size();i++){
        int p=prime[i];
        ){
            factor[len].fac=p;
            factor[len].cnt=;
            ){
                factor[len].cnt++;
                temp/=p;
            }
            len++;
            ) break;//表示除尽
        }
    }
    ){//考虑存在因子大于sqrt(n)的情况(有可能是素数,有可能不是素数,如9998=2*4999)
        factor[len].fac=temp;
        factor[len].cnt=;
        len++;
    }
    printf("%d=",val);
    ) printf(");
    ;i<len;i++){
        printf("%d",factor[i].fac);
        ) printf("^%d",factor[i].cnt);
        ) printf("*");
    }
    ;
}

1059 Prime Factors的更多相关文章

  1. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

  2. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  3. 1059 Prime Factors (25分)

    1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...

  4. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  5. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  6. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  7. 1059 Prime Factors(25 分)

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  8. PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]

    题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...

  9. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

随机推荐

  1. TextView两种显示link的方法

    TextView两种显示link的方法 一.简介 也是TextView显示文本控件两种方法 也是显示丰富的文本 二.方法 TextView两种显示link的方法  1)通过TextView里面的类ht ...

  2. h5 audio播放音频文件

    h5 audio播放音频文件 注:下面html中样式及不相关的内容去掉了 第一个例子 播放没有防盗链的外网音频文件是可以的 <!doctype html> <html> < ...

  3. java环信服务端注册IM代码

    下载环信api代码 https://github.com/easemob/emchat-server-examples 里面包含各种语言版本,我只下载了java版emchat-server-java ...

  4. 【Hive】执行脚本

    1.linux下执行hive sql脚本 (1)hive -e “sql语句” (2)hive -e “sql语句” >> xxx 将sql查出来的语句重定向到xxx文件中,会显示Ok和数 ...

  5. 006——VUE中的内容与属性中使用javascript表达式的方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 【pat-1074】宇宙无敌加法器(高精度)

    链接:https://www.patest.cn/contests/pat-b-practise/1074 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在PAT星人开挂的世界里,每 ...

  7. 2017.11.4 Datasheet 查找替代料

  8. asp.net mvc获得请求体所有内容

    代码如下 Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new St ...

  9. [分享]Google 全球 IP 地址库[Google Global Cache IPs]

    Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr,共计4351个. Bulgaria 93.123.23.1 93.123.23.2 93.123.23 ...

  10. 伪元素:target

    :target 伪类选择器 :target 是 CSS3 新增的一个伪类,可用于选取当前活动的目标元素.当 URL 末尾带有锚名称 #,就可以指向文档内某个具体的元素.这个被链接的元素就是目标元素(t ...