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 ...
随机推荐
- 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)
. 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...
- Esper剖析
Esper剖析 最近在看论文,发现文中有些语言自己未曾见过,经过一番搜索,才发觉是自己接触到了新知识. 官网: )esper的核心包包含了EPL语法解析引擎,事件监听机制,事件处理等核心模块. (2) ...
- 图像检索:CEDD(Color and Edge Directivity Descriptor)算法
颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...
- REST(Representational State Transfer表述性状态转移)
参考内容:http://www.csdn.net/article/2013-06-13/2815744-RESTful-API 定义了一组体系架构原则,您可以根据这些原则设计以系统资源为中心的 Web ...
- how tomcat works 读书笔记(一)----------一个简单的web服务器
http协议 若是两个人能正常的说话交流,那么他们间必定有一套统一的语言规则<在网络上服务器与客户端能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertext tran ...
- OpenCV 实现分水岭算法
种子点的标记没有太搞懂,这个算法的速度还是很快的 // watershed_test20140801.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&q ...
- Android进阶(二十)AndroidAPP开发问题汇总(四)
· Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...
- 史上最全Android Studio快捷键 -2016-02-28
- Android高效率编码-细节,控件,架包,功能,工具,开源汇总,你想要的这里都有
Android高效率编码-细节,控件,架包,功能,工具,开源汇总 其实写博客的初衷也并不是说什么分享技术,毕竟咱还只是个小程序员,最大的目的就是对自我的知识积累,以后万一编码的时候断片了,也可以翻出来 ...
- obj-c编程09:块的语法
在obj-c中,有一种和C截然不同的东西--块.块可以在外边定义,也可以在函数或方法内部定义,可以被赋值给一个变量,然后用该变量调用.默认情况下块对外部变量的访问只能读不能写,除非用__block显示 ...