1059. Prime Factors (25)【素数】——PAT (Advanced Level) Practise
来源:互联网 发布:美女直播间源码 编辑:程序博客网 时间:2024/06/01 10:05
题目信息
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代码
#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;}
0 0
- 1059. Prime Factors (25)【素数】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1059 Prime Factors (25)
- PAT (Advanced Level) Practise 1059 Prime Factors (25)
- 【PAT】【Advanced Level】1059. Prime Factors (25)
- 1096. Consecutive Factors (20)——PAT (Advanced Level) Practise
- 【C++】PAT(advanced level)1059. Prime Factors (25)
- Pat(Advanced Level)Practice--1059(Prime Factors)
- PAT (Advanced Level) 1059. Prime Factors (25) 求所有质因子
- PAT (Advanced Level) Practise 1096 Consecutive Factors (20)
- PAT (Advanced Level) Practise 1096 Consecutive Factors (20)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 1010. Radix (25)——PAT (Advanced Level) Practise
- 1016. Phone Bills (25)——PAT (Advanced Level) Practise
- 1020. Tree Traversals (25)——PAT (Advanced Level) Practise
- 1029. Median (25)【排序】——PAT (Advanced Level) Practise
- 1032. Sharing (25)【链表】——PAT (Advanced Level) Practise
- 1070. Mooncake (25)【贪心】——PAT (Advanced Level) Practise
- [PAT] (Advanced Level) Practise
- 二、SQL语句映射文件(1)resultMap
- 17款最佳代码省查工具
- git 出错及解决办法
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- 解决64位进程调用32位库文件报错问题
- 1059. Prime Factors (25)【素数】——PAT (Advanced Level) Practise
- poj3750约瑟夫环,循环队列
- 一些自己不太常用,但是却很有用的知识点
- MyBatis学习 之 四、动态SQL语句
- 关于SetCapture() 和 ReleaseCapture()的用法
- HDU 2063 过山车
- copy from user函数 和copy to user 函数分析
- 提高数倍工作效率的Android Studio技巧
- MyBatis学习 之 五、MyBatis配置文件