PAT1059:Prime Factors
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^2*11*17*101*1291 思路 按指定格式打印一个数的所有素(质)数因子。
1.先初始化建立一个素数表。
2.从2开始,不断用素数i除输入的数num,能够整除说明满足条件,用cnt记录被整除的次数。
3.对于第一个除数的输出进行判断来决定在除数前面是否加"*"。
4.cnt > 2就在当前除数后加"^cnt"。
5.重复2.3.4直至num < 2。
注:对于1要进行特殊处理,不然输出为"1=",正确输出应该是"1=1"。
代码
#include<iostream>
#include<vector>
using namespace std; vector<int> isPrime(666666,1); void Init()
{
for(int i = 2; i * i < 666666; i++)
{
for(int j = 2; j*i < 666666; j++)
{
isPrime[i * j] = 0;
}
}
} int main()
{
Init();
long long num;
cin >> num;
cout << num <<"=";
if(num == 1)
cout << 1;
bool first = true;
for(int i = 2; num >= 2; i++)
{
int cnt = 0;
if(isPrime[i] == 1 && num % i == 0)
{
while(num % i == 0)
{
cnt++;
num = num/i;
}
if(first)
first = false;
else
cout << "*";
cout << i;
if(cnt > 1)
cout << "^" << cnt;
}
}
}
PAT1059:Prime Factors的更多相关文章
- pat1059. Prime Factors (25)
1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- PAT-1059 Prime Factors (素数因子)
1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 2014辽宁ACM省赛 Prime Factors
问题 L: Prime Factors 时间限制: 1 Sec 内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...
- A1059. Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
随机推荐
- 【公开课】【阿里在线技术峰会】何登成:AliSQL性能优化与功能突破的演进之路
MySQL的公开课,可能目前用不上这些,但是往往能在以后想解决方案的时候帮助到我.以下是阿里对公开课的整理 摘要: 本文根据阿里高级数据库专家何登成在首届阿里巴巴在线技术峰会上的分享整理而成.他主要介 ...
- Android Studio中创建Kotlin For Android项目
Kotlin俗称Android中的Swift,它是Jetbrains公司开发的基于JVM的一门语言,JetBrains公司可能大家并不熟悉,不过相信IntelliJ IDE大家一定知道,Android ...
- Dynamics CRM ISV文件夹禁用后的解决方案
众所周知微软在CRM2011的12补丁后取消了对ISV文件夹的支持,那我们自定义开发的一些web应用或者是想部署个服务该怎么办,有的选择了另开一个站点发布.我们以服务为例这样的另开站点的发布方式会导致 ...
- saiku 网站简介
Saiku web:http://docs.analytical-labs.com/saiku/documentation/2013/08/15/datasources.html Click &quo ...
- Oracle EBS BOM模块常用表结构
表名: bom.bom_bill_of_materials 说明: BOM清单父项目 BILL_SEQUENCE_ID NUMBER 清单序号(关键字)ASSEMBLY_ITEM_ID NUMBE ...
- Android4.0Sd卡移植之使用vold自动挂载sd卡
在cap631平台上移植android4.0,发现内核驱动没有任何问题,能够读写,当总不能挂载. 后来发现是因为自动挂载需要vold的支持.vold程序负责检查内核的 sysfs 文件系统,发现有SD ...
- MySql常用操作语句(2:数据库、表管理以及数据操作)
本文主要内容转自一博文. 另外可供参考资源: SQL语句教程 SQL语法 1.数据库(database)管理 1.1 create 创建数据库 mysql> create database f ...
- Unity Socket UDP
using System.Collections; using System.Collections.Generic; using System.Net.Sockets; using System.N ...
- multiset基础学习,可以有重复类型的多重集合容器
#include <set> #include <iostream> using namespace std; struct Student { char *name; int ...
- Webapck项目开发基本构建及配置
1.创建项目文件夹 myapp 手动创建myapp,或mkdir myapp 2.cd myapp 3.npm init (初始化项目) 4.一路回车(关于项目信息的填写,可以不写,一路回车即可) 可 ...