1059. Prime Factors (25)
2015-12-06 11:14
369 查看
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:[/b]
来源: <http://www.patest.cn/contests/pat-a-practise/1059>
[/code]
来自为知笔记(Wiz)
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:[/b]
97532468Sample Output:[/b]
97532468=2^2*11*17*101*1291
来源: <http://www.patest.cn/contests/pat-a-practise/1059>
#include <iostream>
#include <vector>
using namespace std;
vector<long int> factor;
vector<long int> exponent;
bool isPrime(long int a) {
if (a == 2 || a == 3)
return true;
for (long int i = 2; i*i < a; i++)
if (a%i == 0)
return false;
return true;
}
int main(void) {
long int n;
cin >> n;
cout << n << "=";
if (n < 2) {
cout << n;
return 0;
}
long int i = 2;
while (true)
{
if (!isPrime(i)) {
i++;
continue;
}
if (n%i == 0) {
int expCnt = 0;
factor.push_back(i);
while (true)
{
if (n%i == 0) {
n = n / i;
expCnt++;
}
else {
exponent.push_back(expCnt);
break;
}
}
}
i++;
if (n == 1) {
break;
}
}
for (int i = 0; i < exponent.size(); i++) {
if (exponent[i] == 1)
cout << factor[i];
else
cout << factor[i] << "^" << exponent[i];
if (i != exponent.size() - 1)
cout << "*";
}
return 0;
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- 几个比较简单的题,但是。。。。。
- nginx gzip压缩提升网站速度
- 1058. A+B in Hogwarts (20)
- 【android布局】px、dp和sp,这些单位有什么区别?
- Redis Mass Insertion(--pipe批量插入)
- 定义底层view的click等事件的作用范围
- iOS UITableView中异步加载图片
- 一种javascript链式多重继承的方式(__proto__原型链)
- 1057. Stack (30)
- 使用jQuery+EasyUI实现CheckBoxTree的级联选中特效
- 1056. Mice and Rice (25)
- 报错apachectl -t
- java内部类实例化
- 1055. The World's Richest (25)
- ubuntu开机自动启动程序设置
- 蓝桥杯 错误票据(排序)
- 高效能人士的七个习惯
- Mac之Git/GitHub使用(3)——Fork A Repo
- jquery入门
- Android FM学习中的模块 FM启动过程