PAT甲级 1059. Prime Factors (25)
来源:互联网 发布:windows 查看文件编码 编辑:程序博客网 时间:2024/06/14 12:07
题目:
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:97532468Sample Output:
97532468=2^2*11*17*101*1291思路:
1.先构建质数表,根据N的大小,把1~sqrt(N)之间的质数找出来;
2.根据质数表,从小到大把N的因子找出,这里利用map比较好做;
3.注意N=1的情况
代码:
#include<iostream>#include<cstring>#include<map>#include<math.h>using namespace std;int main(){//ifstream cin;//cin.open("case1.txt");long N;cin >> N;if (N == 1)printf("1=1\n");else{//构建质数表bool *prime = (bool *)malloc(sizeof(bool) * sqrt(N));memset(prime, 0, sizeof(bool)*sqrt(N));int j, i = 2;for (; i <= sqrt(N); ++i){if (!prime[i]){for (j = 2 * i; j <= sqrt(N); j += i){prime[j] = 1;}}}//根据质数表进行分解Nlong NN = N;map<int, int> p;for (i = 2; i <= N;){if ((!prime[i]) && (N%i == 0)){//遇到质数,且该质数是N的因数p[i]++;N /= i;}else{//跳过++i;}}//outputmap<int, int>::iterator iter = p.begin();printf("%ld=", NN);while (1){printf("%d", iter->first);if (iter->second > 1){printf("^%d", iter->second);}iter++;if (iter != p.end()){printf("*");}else{break;}}printf("\n");} system("pause");return 0;}
阅读全文
0 0
- 1059. Prime Factors (25) PAT 甲级
- 【PAT甲级】1059. Prime Factors (25)
- PAT甲级练习1059. Prime Factors (25)
- PAT甲级1059. Prime Factors (25)
- PAT 甲级 1059. Prime Factors (25)
- PAT甲级 1059. Prime Factors (25)
- 1059. Prime Factors (25)-PAT
- 【PAT】1059. Prime Factors (25)
- PAT 1059. Prime Factors (25)
- PAT 1059. Prime Factors (25)
- PAT 1059. Prime Factors (25)
- 1059. Prime Factors (25)-PAT甲级真题(素数表的建立)
- PAT 1059. Prime Factors
- PAT 1059. Prime Factors
- 【PAT】1059. Prime Factors
- pat 1059. Prime Factors
- PAT--1059. Prime Factors
- PAT- Prime Factors (25)
- 【Tensorflow】tf.placeholder函数
- 机器学习实战笔记-利用K均值聚类算法对未标注数据分组
- .NET Web API 项目中的一些问题与解决方案
- Open-Falcon源码部署、修改绘图曲线精度
- sql case when
- PAT甲级 1059. Prime Factors (25)
- 关于Websocket一次Broken pipe的异常处理
- Android反编译工具总结
- c++ 单例模式下实现内存自动回收或销毁
- EMD分析 Matlab 精华总结(转载!目前为止最详细的)
- SQL注入
- MFC光标闪动
- nodejs模块 node-schedule使用,定时任务
- 一部值得你看的电影——黑客帝国