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 ...
随机推荐
- 64位Ubuntu14.04搭建ADT开发环境
本来放假是要出去玩的,结果出游计划拖到了周一,今天先来无事看看ubuntu最近发展得怎么样了,顺便把ADT也搭建好,方便以后研究android框架.想想在windows下,ADT直接down下来解压就 ...
- 类XX是公共的,应在名为XX.java的文件中声明public class XX
找了一个程序粘贴到txt文档中,随便起个名为abc,然后改后缀为.java,接下来在dos中运行时出现以上错误 打开abc.java看了看,声明public class了,但是名字是粘贴过来的类名Wo ...
- nginx日志管理与限速
1.日志简介nginx日志主要有两种:访问日志和错误日志.访问日志主要记录客户端访问nginx的每一个请求,格式可以自定义:错误日志主要记录客户端访问nginx出错时的日志,格式不支持自定义.两种日志 ...
- SSH自动部署
我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上.写了个脚本能够自动上传文件和执行部署任务.完成这个任务需要的条件包括SSH配置和一个执行脚本. 准备 1. ...
- C++函数后面加const修饰
声明一个成员函数的时候用const关键字是用来说明这个函数是 "只读(read-only)"函数,也就是说明这个函数不会修改任何数据成员(object). 为了声明一个const成 ...
- facl笔记
文件系统访问列表:tom: tom, tom基本组jerry: other:r-- chown FACL:Filesystem Access Control List利用文件扩展保存额外的访问控 ...
- Session,有没有必要使用它?
阅读目录 开始 Session的来龙去脉 Session对并发访问的影响 Session的缺点总结 不使用Session的替代方法 Asp.net MVC 中的Session 现有的代码怎么办? 今天 ...
- C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...
- UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
一.对UITabBar背景和icon图标的一些设置 (1)因为直接给UITabBar设置的背景颜色显示的不纯,半透明的感觉,所以,有时候我们可以直接利用纯色的图片作为背景达到想要的效果: (2)给ic ...
- 无法登陆mysql服务器
解决 .#2002 无法登录 MySQL 服务器 将config.sample.inc.php复制成config.inc.php 出现这个错误,表示没有连接到数据库.修改config.inc.php文 ...