1059. Prime Factors (25)【素数】——PAT (Advanced Level) Practise
2015-12-08 09:15
573 查看
题目信息
1059. Prime Factors (25)时间限制50 ms
内存限制65536 kB
代码长度限制16000 B
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
解题思路
枚举所有素数AC代码
[code]#include <cstdio> #include <cmath> #include <climits> using namespace std; bool prime(long long n){ long long t = sqrt(n); for (long long i = 2; i <= t; ++i){ if (n%i == 0) return false; } return true; } int main(){ long long a; scanf("%lld", &a); printf("%lld=", a); if (a == 1){ printf("1\n"); }else{ for (long long i = 2; i <= a; ++i){ if (prime(i)){ int k = 0; while (a%i == 0){ ++k; a /= i; } if (k == 1){ printf("%lld", i); }else if (k > 1){ printf("%lld^%d", i, k); } if (k >= 1 && a > 1){ printf("*"); }else if (a == 1){ break; } } } } return 0; }
相关文章推荐
- file_get_contents与curl的区别
- 人工智能:第十一章 自然语言理解
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- 17款最佳代码省查工具
- Android 编译系统
- 人工智能:第十章 机器视觉
- JFlow手机端开发说明与案例
- 解决64位进程调用32位库文件报错问题
- HTML5定稿一周年,你必须要重新认识HTML5了
- 人工智能:第八章 自动规划
- 二、SQL语句映射文件(1)resultMap
- 最全Pycharm教程(10)——Pycharm调试器总篇
- 第8章3节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动AndroidDebugBridge
- 人工智能:第九章 Agent (艾真体)
- 人工智能:第七章 机器学习
- 第8章2节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-解析处理命令行参数
- Android平台Camera实时滤镜实现方法探讨(七)--滤镜基本制作方法(一)
- 用textView实现跑马灯效果
- 二叉树求高度、叶子节点个数、总节点个数
- javascript 保留两位小数