PAT 1059. Prime Factors (25) 质因子分解
题目链接 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:
- 97532468
Sample Output:
- 97532468=2^2*11*17*101*1291
---------------------------------------------------华丽的分割线---------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cmath>
- using namespace std;
- const int maxn=;
- int prime[maxn],pnum=;
- bool p[maxn]={};
- void Find_Prime(){
- p[]=p[]=true;
- for(int i=;i<maxn;i++){
- if(p[i]==false){
- prime[pnum++]=i;
- for(int j=i+i;j<maxn;j+=i)
- p[j]=true;
- }
- }
- }
- struct factor{
- int x,cnt;
- }fac[];
- int num;
- void PrimeFactor(int n){
- int sqr=(int)sqrt(n);
- num=;
- for(int i=;i<maxn && prime[i]<=sqr;i++){
- if(n%prime[i]==){
- fac[num].x=prime[i];
- fac[num].cnt=;
- while(n%prime[i]==){
- fac[num].cnt++;
- n/=prime[i];
- }
- num++;
- }
- if(n==) break;
- }
- if(n!=){
- fac[num].x=n;
- fac[num++].cnt=;
- }
- }
- void Print_fac(int n){
- printf("%d=",n);
- for(int i=;i<num;i++){
- if(i>)
- printf("*");
- if(fac[i].cnt>)
- printf("%d^%d",fac[i].x,fac[i].cnt);
- else
- printf("%d",fac[i].x);
- }
- printf("\n");
- }
- int main()
- {
- Find_Prime();
- int n;
- while(scanf("%d",&n)!=EOF){
- if(n==)
- printf("1=1\n");
- else{
- PrimeFactor(n);
- Print_fac(n);
- }
- /*
- int a=(1<<31)-1;
- cout<<a<<endl;
- cout<<"1"<<endl;
- PrimeFactor(a);
- cout<<"2"<<endl;
- Print_fac(a);
- cout<<"3"<<endl;
- cout<<num<<endl;
- */
- }
- return ;
- }
- //97532468=2^2*11*17*101*1291
PAT 1059. Prime Factors (25) 质因子分解的更多相关文章
- 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 ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- 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 ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- PAT (Advanced Level) 1059. Prime Factors (25)
素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
- PAT 1059. Prime Factors
反正知道了就是知道,不知道也想不到,很快 #include <cstdio> #include <cstdlib> #include <vector> using ...
随机推荐
- 在JasperReport中填充JavaBean(4)
使用Parameters参数对象传递字符串的示例,本节将演示打印List接口中Userinfo.java实体类的示例,打印的数据源不是来自于Parameters对象,而是JRBeanCollectio ...
- 【Android】使用Gson和Post请求和服务器通信
一.需求文档如下: URL:http://108.188.129.56:8080/example/cal 请求格式: {"para1":10,"para2":2 ...
- 树莓派 wheezy安装与远程登录配置
一.准备工作 1:wheezy系统镜像 2:Win32DiskImager-0.9.5-install 3:SDFormatter 4:SD卡 我用的是8G 二.安装系统 流程:SDFormatter ...
- 《Programming WPF》翻译 第7章 5.可视化层编程
原文:<Programming WPF>翻译 第7章 5.可视化层编程 形状元素能提供一种便利的方式与图形一起工作,在一些情形中,添加表示绘图的元素到UI树中,可能是比它的价值更加麻烦.你 ...
- kibana 统计每天注册数
- 【转】ubuntu设置PATH----不错
原文网址:http://no001.blog.51cto.com/1142339/554927 试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用 ...
- bzoj2049-洞穴勘测(动态树lct模板题)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- 游标的使用实例(Sqlserver版本)
游标,如果是之前给我说这个概念,我的脑子有二个想法:1.你牛:2.我不会 不会不是理由,更不是借口,于是便要学习,本人属性喜欢看代码,不喜欢看书的人,所以嘛,文字对我没有吸引力:闲话少说啊,给大家提供 ...
- HDU 4287 (13.08.17)
Problem Description We all use cell phone today. And we must be familiar with the intelligent Englis ...
- 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)
我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...