题目

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. 六十八、SAP中内表插入的三种方法之二,COLLECT的使用,用于计算数字字段之和

    一.使用COLLECT时,如果关键字没有,那么插入,如果有则求所有关键字列的和,代码如下 二.sy-index在循环中,每次循环从1开始递增 三.查看T_DATA数据 四.如下 五.循环时候,我们查看 ...

  2. 一条 SQL 在 Apache Spark 之旅

    转载自过往记忆大数据 https://www.iteblog.com/archives/2561.html Spark SQL 是 Spark 众多组件中技术最复杂的组件之一,它同时支持 SQL 查询 ...

  3. 【CF1154G】Minimum Possible LCM

    题意 给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数. \(n\le 10^6, a_i\le 10^7\) 题解 直接枚举 ,找到当前数最小的两个倍数,统 ...

  4. 深度解析Critical Thinking的四个阶段

    关于批判性思维我们一直都在讨论学习,但是小编相信没有几个留学生敢说自己有Critical Thinking,但它又是essay写作中必须存在的.那么批判性思维需要怎么培养呢?今天小编就给同学们分析一下 ...

  5. CF1209B Koala and Lights

    It is a holiday season, and Koala is decorating his house with cool lights! He owns n lights, all of ...

  6. 【LeetCode】 240. 搜索二维矩阵 II

    题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...

  7. mybatis初步配置容易出现的问题

    The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...

  8. # vim ~/.vimrc vim配色

    Ubuntu # vim ~/.vimrc        # /hom/zzx 下 set nomodeline                "(这个一定要写,目前有这个安全漏洞) set ...

  9. javascript设计模式(1)——面向对象基础

    用对象收编变量2种方式 1 函数式 var Object = { name:function(){ return this; }, email:function(){ return this; } } ...

  10. LeetCode简单题汇总

      1.两个数之和 给出一个整数数组,请在数组中找出两个加起来等于目标值的数, 你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index ...