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 ...
随机推荐
- HDU 5492(DP) Find a path
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...
- debian系统下安装ssh服务
它是什么?? SSH 为 Secure Shell 的缩写,简单地说,SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用 SSH 协议可 ...
- 给div中动态添加节点并设置样式
前端IOS今天需要动态的在图片前面添加一个按钮 主要是在使用 bt.setAttribute("class","aaa"); 可以对创建的节点使用setAttr ...
- 【iOS开发】添加子控件方式(懒加载,GCC)
// // ViewController.m // GCC // // Created by admin on 15/10/7. // Copyright © 2015年 admin. All rig ...
- Asset Catalog Creator Free 生成程序内图标的软件
Asset Catalog Creator Free 生成程序内图标的软件
- Effective Java实作equals() - 就是爱Java
equals()这个方法,定义在Object class中,这个是所有class的base class,因此所有的class都继承这个方法,默认是比较内存地址,不过Mix需要的是商业规则上的比较,所以 ...
- ListView中RadioButton实现单项选择
1:FragmentHack5.java public class FragmentHack5 extends Fragment { View view; ListView lvCountries; ...
- Morse Clock
Morse Clock "di-dah di-di-di-dit di-dah-dah di-dah-dah-dah dah-di-dit dah-di-di-dah", soun ...
- HDU4171--bfs+树
第一开始想成了DP.尼玛后来才发现只有N条边,那就简单了.. 从起点S遍历整棵树,从某点跳出来回到终点T,问最短路长度.然而从某点跳出时走过的路径是一个定值.... 长度为整棵树的边长和sum*2-d ...
- 【转】多核CPU运行模式
多核CPU运行模式主要有以下三种: •非对称多处理(Asymmetric multiprocessing,AMP)——每个CPU内核运行一个独立的操作系统或同一操作系统的独立实例(instantiat ...