PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
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^2*11*17*101*1291
代码:
#include <bits/stdc++.h>
using namespace std; long int P;
int prime[500010]; int main() {
memset(prime, 1, sizeof(prime));
for(int i = 2; i * i <= 500010; i ++)
for(int j = 2; j * i < 500010; j ++)
prime[j * i] = 0;
scanf("%ld", &P); printf("%ld=", P);
if(P == 1) printf("1"); bool First = false;
for(int i = 2; P >= 2; i ++) {
int cnt = 0, flag = 0;
while(prime[i] && P % i == 0) {
cnt ++;
P /= i;
flag = 1;
}
if(flag) {
if(First)
printf("*");
printf("%d", i);
First = true;
}
if(cnt > 1)
printf("^%d", cnt);
}
return 0;
}
素数表! Get
PAT 甲级 1059 Prime Factors的更多相关文章
- 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甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- 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 ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- 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 ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
随机推荐
- angularjs与vue循环数组对象是区别
一直都觉得angularjs和vue是想类似的,今天在限制加载的数据条数时发现 其不同,话不多说,直接看代码: 1.angularjs <li ng-repeat="item in d ...
- Spring事务控制
我们在实际业务场景中,经常会遇到数据频繁修改读取的问题.在同一时刻,不同的业务逻辑对同一个表数据进行修改,这种冲突很可能造成数据不可挽回的错乱,所以我们需要用事务来对数据进行管理. 1. 到底什么是事 ...
- Hadoop学习(一)
今天刚刚从hadoop网址下载了edition 0.21,上传到公司内网 server上试了一把. 跟着官方网站走了一趟,在执行 Format a new distributed-filesystem ...
- 20155206 实验一《Java开发环境的熟悉》实验报告
实验内容 使用JDK编译.运行简单的Java程序 使用IDEA 编辑.编译.运行.调试Java程序. 实验代码 `import java.text.DecimalFormat; import java ...
- 20155207JAVA第十二周课堂练习
20155207JAVA第十二周课堂练习 教材代码检查--P98 修改教材P98 Score2.java, 让执行结果数组填充是自己的学号 Arrays和String单元测试 在IDEA中以TDD的方 ...
- 人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码
人脸识别引擎SeetaFaceEngine中Identification模块用于比较两幅人脸图像的相似度,以下是测试代码: int test_recognize() { const std::stri ...
- centos7.4 防火墙设置
1.关闭默认的firewall防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service ...
- Gulp 有用的地址
gulp似乎成为web开发的必选工具. 推荐一个非常好的入门教程 https://markgoodyear.com/2014/01/getting-started-with-gulp/ 官方插件列表: ...
- robotframework 脚本编写规范
测试集.脚本 测试脚本的名字不要超过20个字符,文件类型应该为txt 名字必需易读且有意义(看名知意) 记住测试集的名字是自动根据文件.目录的名字创建的.后缀名会被截去,下划线会转换为空格,如果名 ...
- 04-容器 What, Why, How
What - 什么是容器? 容器是一种轻量级.可移植.自包含的软件打包技术,使应用程序可以在几乎任何地方以相同的方式运行.开发人员在自己笔记本上创建并测试好的容器,无需任何修改就能够在生产系统的虚拟机 ...