题意:

给出一个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就够了。

  1. struct Factor{
  2. int fac;//质因子
  3. int cnt;//质因子出现的次数
  4. }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()函数中直接硬编码了。

代码:

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct Factor{
  7. int fac;//质因子
  8. int cnt;//质因子出现的次数
  9. }factor[];
  10.  
  11. vector<int> prime;//素数表
  12.  
  13. //判断素数
  14. bool isPrime(int n)
  15. {
  16. ) return false;
  17. int sqr=(int)sqrt(n);
  18. ;i<=sqr;i++)
  19. ) return false;
  20. return true;
  21. }
  22.  
  23. //获取素数表,打表思想
  24. void getPrime()
  25. {
  26. ;i<;i++)
  27. if(isPrime(i)) prime.push_back(i);
  28. }
  29.  
  30. int main()
  31. {
  32. int val;
  33. scanf("%d",&val);
  34. getPrime();
  35. int temp=val;
  36. ;//factor数组的长度
  37. ;i<prime.size();i++){
  38. int p=prime[i];
  39. ){
  40. factor[len].fac=p;
  41. factor[len].cnt=;
  42. ){
  43. factor[len].cnt++;
  44. temp/=p;
  45. }
  46. len++;
  47. ) break;//表示除尽
  48. }
  49. }
  50. ){//考虑存在因子大于sqrt(n)的情况(有可能是素数,有可能不是素数,如9998=2*4999)
  51. factor[len].fac=temp;
  52. factor[len].cnt=;
  53. len++;
  54. }
  55. printf("%d=",val);
  56. ) printf(");
  57. ;i<len;i++){
  58. printf("%d",factor[i].fac);
  59. ) printf("^%d",factor[i].cnt);
  60. ) printf("*");
  61. }
  62. ;
  63. }

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. 报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]

    报这种错的原因基本上是applicationContext.xml文件中bean配置错误,错误如下: org.springframework.beans.factory.BeanCreationExc ...

  2. python学习笔记(pip下载安装)

    python有很多扩展模块需要安装 这个时候万能的pip就可以提供帮助 首页进入官网下载压缩包: https://pypi.python.org/pypi/pip#downloads 解压文件 cmd ...

  3. Confluence 6 测试电子邮件设置

    一个 Confluence 的管理员可以通过下面的步骤测试电子邮件服务器的配置: 按照上面的步骤中描述得方法设置一个电子邮件服务器. 单击 发送测试邮件(Send Test Email)来检查你设置的 ...

  4. Solr工具类

    import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.spr ...

  5. Shiro快速入门

    1.什么是Shiro Shiro是Java的一个安全框架, 完成权限控制的任务. 权限控制的基本功能: 认证(让系统知道你是谁); 授权(让系统知道你能做什么)权限控制常用的技术: 过滤器/拦截器, ...

  6. Eclipse Android 代码自动提示功能

    Eclipse Android 代码自动提示功能 Eclipse for android 实现代码自动提示智能提示功能,介绍 Eclipse for android 编辑器中实现两种主要文件 java ...

  7. 【辅助工具】Python实现微信跳一跳

    最近迷上了微信跳一跳小游戏,正好也看到知乎上有大神分享了技术贴,我也参考了好多资料,原理就是通过abd命令截取图片,python计算两个点距离,然后转化按压时间,让电脑来完成游戏.我花了很长时间才把程 ...

  8. Report: Disappearing Wetlands Put Planet Life at Risk

    A new report warns that wetlands are disappearing three times faster than the world’s forests, with ...

  9. react 学习文章

    生命周期 学习笔记 一些坑 项目完成后总结 理解Immutable 是否要同构如何同构 react组件最佳实践 redux集合所有的state props来源, 页面所有状态 数据的唯一来源 reac ...

  10. python中的赋值与拷贝(浅拷贝与深拷贝)

    1.赋值与拷贝 直接赋值(b=a)是传引用,b改动a也会改动. a = [1, 2, 3, 4] b = a b[1] = 999 print(a, b) #[1, 999, 3, 4] [1, 99 ...