题目

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.

Sample Input:

97532468

Sample Output:

97532468=2^211171011291

题目分析

输入一个整数N,将其分解为质因数的乘法式

解题思路

  1. 创建质数表
  2. 依次判断质数表中质数i,若N%i==0,则打印质数(并内循环统计i整除N的次数tm,每次内循环完成更新N=N/i,若N%i!=0,退出内循环);
  3. 打印当前质数的指数tm

Code

Code 01

#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if(n<=1)return false;
int sqr=(int)sqrt(1.0*n);
for(int i=2; i<=sqr; i++) {
if(n%i==0)return false;
}
return true;
}
int prime_table[100010];
bool prime_bool[100010];
void create_pt() {
int index = 0;
for(int i=2; i<100010; i++) {
if(prime_bool[i]==false) {
prime_table[index++]=i;
for(int j=i+i; j<100010; j+=i) {
prime_bool[j]=true;
}
}
}
}
int main(int argc,char *argv[]) {
// 1.创建素数表
create_pt();
// 2.接收输入
long long n;
scanf("%lld", &n);
// 3. 打印
printf("%lld=",n);
if(n==1)printf("1");
int index=0;
bool wf = false; //false为打印的第一个数字,true为打印的第一个数字后的数字
while(n>1) {
int prime = prime_table[index];
bool flag = false;
int tm=0;
while(n%prime==0) {
n/=prime;
tm++;
}
if(tm>0) {
if(wf)printf("*");
printf("%d",prime);
wf = true;
}
if(tm>1)printf("^%d",tm);
index++;
}
return 0;
}

Code 02(素数表生成简洁但不高效)

#include <cstdio>
#include <vector>
using namespace std;
vector<int> prime(500000, 1);
int main() {
for(int i = 2; i * i < 500000; i++)
for(int j = 2; j * i < 500000; j++)
prime[j * i] = 0;
long int a;
scanf("%ld", &a);
printf("%ld=", a);
if(a == 1) printf("1");
bool state = false;
for(int i = 2; a >= 2; i++) {
int cnt = 0, flag = 0;
while(prime[i] == 1 && a % i == 0) {
cnt++;
a = a / i;
flag = 1;
}
if(flag) {
if(state) printf("*");
printf("%d", i);
state = true;
}
if(cnt >= 2)
printf("^%d", cnt);
}
return 0;
}

PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]的更多相关文章

  1. 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 ...

  2. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  3. 1059 Prime Factors (25分)

    1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...

  4. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  5. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  6. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  7. PAT (Advanced Level) 1059. Prime Factors (25)

    素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  8. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

  9. pat1059. Prime Factors (25)

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

随机推荐

  1. 067-PHP使用匿名函数

    <?php $func=function ($x,$y){ //匿名函数与变量绑定 return $x+$y; }; echo '5+6='.$func(5,6); //使用匿名函数 echo ...

  2. zoj 1483 划分类DP

    还是看了little_w大神写的才知道怎么写,看完发现自己题意也理解错了,里面有个neighboring,意思就是你指定任务的时候指定的是原序列中连续的一段 然后就是怎么DP了,新学了个很好的dp模型 ...

  3. 干货分享:想要写好Proposal,这四个问题必须解决

    当大家确定了毕业论文选题之后,下一步就是着手写开题报告,也就是proposal.开题报告或者说是研究计划要回答四个问题:为什么这个问题重要,为什么这个问题很难解决,为什么现在要考虑解决这个问题,为什么 ...

  4. (五)selenuim和phantonJs处理网页动态加载数据的爬取

    selenuim和phantonJs处理网页动态加载数据的爬取 一 图片懒加载 自己理解------就是在打开一个页面的时候,图片数量特别多,图片加载会增加服务器的压力,所以我们在这个时候,就会用到- ...

  5. [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error [Errno 10061] Connection refused>

    [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error ...

  6. POJ 3096:Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6258   Accepted: 407 ...

  7. MFC OCX 控件事件的添加和处理

    1.控件的事件一般都是由对外的接口引发到,这里定一个接口先: 该接口有一个字符串参数,表示调用者将传入一个字符串,传进来后,我们将取得字符串的长度. 2.添加事件: 事件应该是属于窗口的,所以在Ctr ...

  8. 当chart图遇上bootstrap的TAB切换 无宽高问题?

    .tab-content > .tab-pane, .pill-content > .pill-pane {    display: block; /* undo display:none ...

  9. 三十一、CI框架之使用验证码

    一.CI的验证码功能用着很是舒服,需要在根目录下新建一个captcha的验证码文件夹用于存放生产的图片,代码如下: 二.浏览器效果如下: 总结:关于验证码生产函数,有很多参数可以设置,包括字体,验证码 ...

  10. 电脑连接了HDMI线,电脑没有声音了,原因和解决办法

    我们经常会使用笔记本电脑通过HDMI线外接显示器或者投影仪设备,会遇到笔记本电脑没有声音或者声音很小的问题. 没有声音说明电脑的播放设备(扬声器)设置问题,可以通过查看扬声器情况解决. 如图所示: 需 ...