PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]
题目
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,将其分解为质因数的乘法式
解题思路
- 创建质数表
- 依次判断质数表中质数i,若N%i==0,则打印质数(并内循环统计i整除N的次数tm,每次内循环完成更新N=N/i,若N%i!=0,退出内循环);
- 打印当前质数的指数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) [素数表的建⽴]的更多相关文章
- 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 ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- 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本身是一个质数,直接输出它等于自己即可,数据点 ...
- pat1059. Prime Factors (25)
1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
随机推荐
- 九十一、SAP中ALV事件之五,查看状态栏,工具栏和功能键等
一.我们按照说明,来到SE37功能模块,然后点击[转到]->[函数组]->[显示组] 二.按照说明输入SALV,点击勾选 三.点击主程序 四.点击主程序后,我们来到函数组页面,然后 五.我 ...
- 第七篇:Python3连接MySQL
第七篇:Python3连接MySQL 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可以连接该数据库的用户名和密码 你有一个有权限操作的data ...
- mybatis初步配置容易出现的问题
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...
- COGS 1489玩纸牌
%%%http://blog.csdn.net/clover_hxy/article/details/53171234 #include<bits/stdc++.h> #define LL ...
- 春节宅家火了短视频,手游 APP 成最大赢家!
春节历来是APP运营者翘首以盼的火热期,但2020年的春节有些特殊, 新型冠状病毒的爆发,牵动着全国亿万人民的心.响应号召不出门,宅在家里玩手机,于是打游戏.看新闻.追剧等成为大家打发时间.疏解内心压 ...
- LabVIEW面向对象的ActorFramework(3)
四.LabVIEW面向对象的编程架构:Actor Framework Actor Framework是一个软件类库,用以支持编写有多个VI独立运行且相互间可通信的应用程序,在该类型应用程序中,每个VI ...
- 51Nod大数加法(两个数正负都可)
很多大数的问题都运用模拟的思想,但是这个说一样也一样,但是难度较大,很麻烦,我自己谢写了100多行的代码,感觉很对,但就是WA.其实个人感觉C和C++没有大数类,是对人思想和算法的考验,但是有时候做不 ...
- LVM中逻辑卷的最大大小限制
前言: 本文是对这篇博客Maximum Size Of A Logical Volume In LVM的翻译,敬请尊重原创和翻译劳动成果,那些随意转载的大爷们,好歹也自觉注明出处.谢谢! 英文原文地址 ...
- [极客大挑战 2019]Upload
0x00 知识点 一个常规上传题目,知识点全都来自前几天写的文章: https://www.cnblogs.com/wangtanzhi/p/12243206.html 1:某些情况下绕过后缀名检测: ...
- POJ 3438:Look and Say
Look and Say Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9196 Accepted: 5566 Desc ...