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 = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

具体代码

#include <cstdio>
#include <cmath>bool isprime(int a){if(a<=1)return false;int sqr=(int)sqrt(a);for(int i=2;i<=sqr;i++){if(a%i==0)false;}return true;}int prime[100020],pnum=0;//pnum为100020以内质数的个数void findprime(){for(int i=0;i<100020;i++){if(isprime(i)==true)prime[pnum++]=i;}}//0到n内的所有质数,并传入数组prime[]struct factor{int x,cnt;//x为质因子,cnt为质因子的个数
}fac[15];int main(){findprime();int n,num=0;//num为不同的质因子的个数scanf("%d",&n);if(n==1)printf("1=1");else{printf("%d=",n);int sqr=sqrt(n);//因为n的因子只可能在根号n以内for(int i=0;i<pnum&&prime[i]<sqr;i++){if(n%prime[i]==0){fac[num].x=prime[i];fac[num].cnt=0;while(n%prime[i]==0){fac[num].cnt++;n/=prime[i];}num++;}if(n==1)break;}if(n!=1){fac[num].x=n;fac[num++].cnt=1;}for(int i=0;i<num;i++){if(i>0)printf("*");            printf("%d",fac[i].x);if(fac[i].cnt>1)printf("^%d",fac[i].cnt);}}return 0;}

1059 Prime Factors (25 分)质因子 易错题相关推荐

  1. 【题目分析】1059 Prime Factors (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...

  2. 1059 Prime Factors (25 分)【难度: 一般 / 知识点: 分解质因子 】

    注意: 判断1000000007这种只有一个质数特别大的情况 #include<bits/stdc++.h> using namespace std; map<int,int> ...

  3. 1059. Prime Factors (25)

    题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...

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

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

  5. 1059. Prime Factors (25)--taste

    点击打开链接 注意: 1.对于输入为1的情况要特判 2.factor在int范围内开到10就足够 3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double int sqr=sqrt( ...

  6. 用计算机计算一定比笔算简便,四年级数学(下册)期末知识要点汇总,易错题解析...

    当我第一遍读一本好书的时候,我仿佛觉得找到了一个朋友;当我再一次读这本书的时候,仿佛又和老朋友重逢.我们要把读书当作一种乐趣,并自觉把读书和学习结合起来,做到博览.精思.熟读,更好地指导自己的学习,让 ...

  7. PAT甲级1059 Prime Factors :[C++题解]分解质因子

    文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...

  8. 1059 Prime Factors(25 分)(cj)

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

  9. 1059 Prime Factors

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

  10. PAT 1059 Prime Factors[难]

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

最新文章

  1. 异步备份和还原数据库:.NET发现之旅(六)
  2. 《梦断代码》读后笔记_2
  3. URAL 1009. K-based Numbers
  4. 大山深处,有一所希望学校
  5. vs找不到dll_零基础学习Python_绝对VS相对
  6. 转载--【笔记】Asp.Net构架(Http请求处理流程)
  7. 145_Power BI Report Server自定义Form登录
  8. 零基础入门深度学习(3) - 神经网络和反向传播算法
  9. 跨域问题时的Filter无效
  10. 100款违法违规APP下架整改:微店、更美等在列
  11. 11gR2 Grid Infrastructure Installation prerequisites On Windows
  12. Android显示shp文件,Arcgis for Android移动平台能直接打开本地的shp文件吗
  13. Python编程学习:中兴LTE网管MML命令脚本生成器
  14. SQL语句练习-入门篇
  15. Angular导入导出Excel控件简介
  16. 国内外银行核心系统之间的差异
  17. 苹果系统如何添加服务器的打印机,哪位清楚macbookair如何添加打印机
  18. (一)移动端前端开发-移动端基础
  19. php 汉语转换拼音
  20. python invoking c++

热门文章

  1. 前言:设计模式六大原则
  2. android studio文件读写,Android 中的文件读写操作
  3. arcgis构建金字塔失败什么原因_新西兰创业移民转永居失败!原因是什么?
  4. 人机身份验证开发资料
  5. python struct 45s_python32版本,struct.error: argument for 's' must be a bytes object - 小众知识...
  6. python中的三种排序方法,使用冒泡对列表排序,使用自带sort方法进行排序
  7. r语言 rgl 强制过程中_R语言中的方差分析方法汇总
  8. java p39课后答案_面向对象程序设计(JAVA)答案
  9. 【转】nodejs更新mysql数据库信息
  10. java B2B2C springmvc mybatis电子商城系统-分布式配置中心(Spring Cloud Config)