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 ...
随机推荐
- AI自动寻路
1.首先把游戏场景中的物体设为静态 2.选中Window 中的Navigation ,点击Bake进行场景烘焙 3.在需要寻路的游戏对象上添加 NavMeshAgent组件.调整其AgentSize大 ...
- 在开启bin-log日志下Mysql报错
This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...
- find详解
一.简介 在目录结构中搜索文件,并执行指定的操作. 二.语法 find path -option [ -print ] [ -exec -ok command ] {} \;path: find命令所 ...
- SQLite-CONSTRAINTS(约束)
SQLite – CONSTRAINTS(约束) 约束是数据列在表上执行的规则.这些是用来限制的数据类型可以进入一个表.这样可以确保数据的准确性和可靠性在数据库中. 级或表级约束可以列.列级约束是仅适 ...
- POJ1664(简单动态规划)
#include<iostream> #include<string> #include<cstring> using namespace std; ][]; vo ...
- 案例:用JS实现放大镜特效
案例:用JS实现放大镜特效 案例:用JS实现放大镜特效
- android中创建模拟器的 SDCard
在eclipse中安装了android环境后,可以直接创建AVD和sdcard的,windows->Android Virtual Device Manager,创建一个AVD时,可以同时创建s ...
- java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
此方法为Timestamp的 转换方法. 这几天做到excel导入功能,其中里面有几个时间时段,所以用了这个类来将导入的字符串格式转换Timestamp格式. 不慎出现了 java.lang.Ille ...
- string字母排序,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- python高级编程(第12章:优化学习)3
#微观剖析 ''' 当找到速度很慢函数时,有时还需要做到测试某个部分函数剖析工作,这需要通过手动对一部分代码速度测试完成 ''' """ import tempfile, ...