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 ndk编译项目(android-ndk-16r1)
由于采用android-ndk-16r1版本的ndk来编译 编译的环境之类在这里省略,注意是最后编译的命令如下 Administrator@WIN-AF6P80LVIJ0 ~ $ cd $ANDROI ...
- Linux System Programming --Chapter Nine
这一章的标题是 "信号" ,所以本文将对信号的各个方面进行介绍,由于Linux中的信号机制远比想象的要复杂,所以,本文不会讲的很全面... 信号机制是进程之间相互传递消息的一种方法 ...
- 基于ROS_Arduino室内移动机器人SLAM实验测试
纯手工搭建的机器人,因此外观并不美. 基于ROS(indigo)以及Arduino等搭建软硬件平台,包括语音.视觉.激光.码盘等传感器设备. 整体如下图所示: 底盘特写: 语音输入: Arduino模 ...
- 知名IT公司的年度大会合集
很多知名的IT公司都有年度大会,比如说谷歌,微软,Adobe,甲骨文,苹果等等.在这些公司的年度大会上,都会展示一些公司比较前沿的产品.看看这些大会的视频(也可以参会,但是门票可是非常贵的),对我们了 ...
- 【65】Mybatis详解
Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis能够 ...
- AngularJS进阶(九)控制器controller之间如何通信
AngularJS控制器controller之间如何通信 注:请点击此处进行充电! angular控制器通信的方式有三种: 1,利用作用域继承的方式.即子控制器继承父控制器中的内容 2,基于事件的方式 ...
- SoftMax regression
最终收敛到这个结果,巨爽. smaple 0: 0.983690,0.004888,0.011422,likelyhood:-0.016445 smaple 1: 0.940236,0.047957, ...
- obj-c在Xcode之外如何使用@import关键字
在Xcode中@import可以很方便的代替#import的功能,具体区别和便利请自行google之. 这里简单介绍下在Xcode之外如何使用@import.直接以 @import Foundatio ...
- Java I/O最简单的几个类
今天把I/O中最简单的几个类整理了一下,之所以整理最简单的,是因为这样会让我更加快速方便的理顺这里面的东西,以前每一次用的时候都要先百度一下,觉得很烦. 首先需要先看一下Read,Write和Stre ...
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
第一步:生成与JDK版本对应的weblogicjar,利用cmd 进入到weblogic_home 路径下进入到server/lib目录,然后运行 JDK 1.6 命令 "java -j ...