题目链接 http://www.patest.cn/contests/pat-a-practise/1059

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 N = 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.(坑爹,这最后一句不是说满足情况不输出,可答案是要输出的,害我吓考虑2个case没过)

Sample Input:

  1. 97532468

Sample Output:

  1. 97532468=2^2*11*17*101*1291
    ---------------------------------------------------华丽的分割线---------------------------------------------------------------------------------------------
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. const int maxn=;
  5. int prime[maxn],pnum=;
  6. bool p[maxn]={};
  7. void Find_Prime(){
  8. p[]=p[]=true;
  9. for(int i=;i<maxn;i++){
  10. if(p[i]==false){
  11. prime[pnum++]=i;
  12. for(int j=i+i;j<maxn;j+=i)
  13. p[j]=true;
  14. }
  15. }
  16. }
  17. struct factor{
  18. int x,cnt;
  19. }fac[];
  20. int num;
  21. void PrimeFactor(int n){
  22. int sqr=(int)sqrt(n);
  23. num=;
  24. for(int i=;i<maxn && prime[i]<=sqr;i++){
  25. if(n%prime[i]==){
  26. fac[num].x=prime[i];
  27. fac[num].cnt=;
  28. while(n%prime[i]==){
  29. fac[num].cnt++;
  30. n/=prime[i];
  31. }
  32. num++;
  33. }
  34. if(n==) break;
  35. }
  36. if(n!=){
  37. fac[num].x=n;
  38. fac[num++].cnt=;
  39. }
  40. }
  41. void Print_fac(int n){
  42. printf("%d=",n);
  43. for(int i=;i<num;i++){
  44. if(i>)
  45. printf("*");
  46. if(fac[i].cnt>)
  47. printf("%d^%d",fac[i].x,fac[i].cnt);
  48. else
  49. printf("%d",fac[i].x);
  50. }
  51. printf("\n");
  52. }
  53. int main()
  54. {
  55. Find_Prime();
  56. int n;
  57. while(scanf("%d",&n)!=EOF){
  58. if(n==)
  59. printf("1=1\n");
  60. else{
  61. PrimeFactor(n);
  62. Print_fac(n);
  63. }
  64.  
  65. /*
  66. int a=(1<<31)-1;
  67. cout<<a<<endl;
  68. cout<<"1"<<endl;
  69. PrimeFactor(a);
  70. cout<<"2"<<endl;
  71. Print_fac(a);
  72. cout<<"3"<<endl;
  73. cout<<num<<endl;
  74. */
  75. }
  76. return ;
  77. }
  78. //97532468=2^2*11*17*101*1291

PAT 1059. Prime Factors (25) 质因子分解的更多相关文章

  1. 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 ...

  2. 1059 Prime Factors (25分)

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

  3. PAT 1059 Prime Factors[难]

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

  4. 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 ...

  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 (25)-素数筛选法

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

  7. PAT (Advanced Level) 1059. Prime Factors (25)

    素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  8. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

  9. PAT 1059. Prime Factors

    反正知道了就是知道,不知道也想不到,很快 #include <cstdio> #include <cstdlib> #include <vector> using ...

随机推荐

  1. 在JasperReport中填充JavaBean(4)

    使用Parameters参数对象传递字符串的示例,本节将演示打印List接口中Userinfo.java实体类的示例,打印的数据源不是来自于Parameters对象,而是JRBeanCollectio ...

  2. 【Android】使用Gson和Post请求和服务器通信

    一.需求文档如下: URL:http://108.188.129.56:8080/example/cal 请求格式: {"para1":10,"para2":2 ...

  3. 树莓派 wheezy安装与远程登录配置

    一.准备工作 1:wheezy系统镜像 2:Win32DiskImager-0.9.5-install 3:SDFormatter 4:SD卡 我用的是8G 二.安装系统 流程:SDFormatter ...

  4. 《Programming WPF》翻译 第7章 5.可视化层编程

    原文:<Programming WPF>翻译 第7章 5.可视化层编程 形状元素能提供一种便利的方式与图形一起工作,在一些情形中,添加表示绘图的元素到UI树中,可能是比它的价值更加麻烦.你 ...

  5. kibana 统计每天注册数

  6. 【转】ubuntu设置PATH----不错

    原文网址:http://no001.blog.51cto.com/1142339/554927 试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用 ...

  7. bzoj2049-洞穴勘测(动态树lct模板题)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  8. 游标的使用实例(Sqlserver版本)

    游标,如果是之前给我说这个概念,我的脑子有二个想法:1.你牛:2.我不会 不会不是理由,更不是借口,于是便要学习,本人属性喜欢看代码,不喜欢看书的人,所以嘛,文字对我没有吸引力:闲话少说啊,给大家提供 ...

  9. HDU 4287 (13.08.17)

    Problem Description We all use cell phone today. And we must be familiar with the intelligent Englis ...

  10. 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)

    我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...