题目

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. 029-PHP取随机数

    <?php //打印从1到100中间的10个随机的数 ; $index < ; $index++) { print(rand(,) . "<BR>\n"); ...

  2. 148-PHP strip_tags函数,剥去字符串中的 HTML 标签(二)

    <?php //定义一段包含PHP代码的字符串 $php=<<<PHP 这里是PHP代码的开始 <?php echo "hello!"; PHP; $ ...

  3. 自定义spark UDAF

    官网链接 样例代码: import java.util.ArrayList; import java.util.List; import org.apache.spark.sql.Dataset; i ...

  4. VIJOS-P1282 佳佳的魔法照片 排序

    Description 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从 ...

  5. SpringBoot学习(五)——Profile

    Profile是Spring对不同环境(例如开发人用开发环境,测试人员用测试环境)提供不同配置功能的支持,可以通过激活,指定参数等方式快速切换环境 1.多profile文件形式 格式:applicat ...

  6. CV_图像滤波[转]---python+opencv均值滤波,高斯滤波,中值滤波,双边滤波

    1.图像滤波算法(cv2) https://blog.csdn.net/qq_27261889/article/details/80822270 2.

  7. html特殊字符的写法

    符号 写法 (空格)   <(小于号) < >(大于号) > " " ®(已注册) ® ©(版权) © ™(商标) ™ (半方大的空白)   (全方大的空白 ...

  8. 2.11 DataBinding 简单使用

    DataBindIng 在我理解看来类似于其他语言当中的全局变量,只修改一处就可全部修改 添加位置和代码如下: 打开DataBinding 开关: dataBinding { enabled true ...

  9. javascript面向对象编程的3种常见封装形式解析

    javascript如何才能脱离函数式编程,拥抱面向对象编程呢,常见的有3种形式,其它形式可以说都是这3种的变种.   1.直接定义对象直接量的形式   var Util={     getType: ...

  10. C语言编程实现SYN-Flood(Dos)攻击

    ## 实验环境为了方便,直接在win10 VS2013Ultimate实现(攻击机),靶机为同一局域网的另外一台主机或外网服务器.   ## 实验依赖基于WinPcap实现,需要安装WinPcap4. ...