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 ...
随机推荐
- adb调试实用命令
获取设备IMEI: adb shell dumpsys iphonesubinfo 文件在设备和PC端的操作:adb push [PC端源文件路径] [设备的目的文件路径] 例如:adb push C ...
- discuz@功能的代码
//转载 $atlist = $atlist_tmp = $ateduids = array(); preg_match_all("/@([^\r\n]*?)\s/i", $mes ...
- inux关于readlink函数获取运行路径的小程序
inux关于readlink函数获取运行路径的小程序 相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int re ...
- ASCII码、base64编码 为什么有的代码要用 base64 进行编码?
百度百科 ASCII码:http://baike.baidu.com/link?url=bNtzytBhlSUt_l3pwpfICxCxqgAfqsBMaeWX6QF7gH46Tg4pQtKM2aAV ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
- Linux下如何选择文件系统:EXT4、Btrfs 和 XFS
老实说,人们最不曾思考的问题之一是他们的个人电脑中使用了什么文件系统.Windows 和 Mac OS X 用户更没有理由去考虑,因为对于他们的操作系统,只有一种选择,那就是 NTFS 和 HFS+. ...
- MySql 中的常见问题解决方法
一.关键字做字段 ,mssql的做法是[关键字],mysql的做法是 `关键字` ;ps:(` 是 tab键上面的一个按键) 二.mssql数据导入到mysql: 1.mssql导出insert语句( ...
- MyWidget【简单自制控件】
#coding=gbk from PyQt4 import QtGui,QtCore import random class MyWidget(QtGui.QWidget): def __init__ ...
- Java客户端Jedis的八种调用方式
redis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分 ...