【PAT】【Advanced Level】1059. Prime Factors (25)
来源:互联网 发布:工业机器人网络培训 编辑:程序博客网 时间:2024/06/10 04:37
1059. Prime Factors (25)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HE, Qinming
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
https://www.patest.cn/contests/pat-a-practise/1059
https://www.nowcoder.com/pat/5/problem/4112
思路:
对于每一个数进行扫描,从2开始,每找到一个因子,不断相除,累加该因子次数。
——PAT AC,牛客网超时
对于每个数先判断是否是质数,如果是则不用再分解
——AC
CODE:
#include<iostream>#include<cstdio>#include<vector>using namespace std;typedef struct S{int nu;int p;};vector<S> pri;bool pr(int n){if (n==1||n==2||n==3) return 1;for (int i=2;i*i<=n;i++) if (n%i==0) return 0;return 1;}int main(){int n;cin>>n;cout<<n<<"=";if (pr(n)){ cout<<n; }else{int i=2;while (i<=n){if (n%i==0){S t;t.nu=i;t.p=0;while (n%i==0){t.p++;n/=i;}pri.push_back(t);}i++;}for (int i=0;i<pri.size()-1;i++){cout<<pri[i].nu;if (pri[i].p!=1){cout<<"^"<<pri[i].p; }cout<<"*";}cout<<pri[pri.size()-1].nu;if (pri[pri.size()-1].p!=1){cout<<"^"<<pri[pri.size()-1].p; }}return 0;}
阅读全文
0 0
- 【PAT】【Advanced Level】1059. Prime Factors (25)
- 【C++】PAT(advanced level)1059. Prime Factors (25)
- PAT (Advanced Level) Practise 1059 Prime Factors (25)
- PAT (Advanced Level) Practise 1059 Prime Factors (25)
- Pat(Advanced Level)Practice--1059(Prime Factors)
- PAT (Advanced Level) 1059. Prime Factors (25) 求所有质因子
- 1059. Prime Factors (25)【素数】——PAT (Advanced Level) Practise
- 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
- PAT 1059. Prime Factors
- 【PAT】1059. Prime Factors
- pat 1059. Prime Factors
- PAT--1059. Prime Factors
- PAT- Prime Factors (25)
- Junit
- 字典树
- 句子相似度计算的几种方法
- view 的拖拽移动,旋转缩放
- HDU1160-LIS&路径记录-J
- 【PAT】【Advanced Level】1059. Prime Factors (25)
- 设计模式学习笔记八:建造者模式(Builder Pattern)
- RabbitMq、ActiveMq、ZeroMq、kafka之间的比较,资料汇总
- 【NOIP2017模拟A组模拟8.5】带权排序
- 暴力求解法_隐式图搜索(八数码问题)
- Qt控件样式 Style Sheet Demo
- java创建线程的三种方式及其对比
- Java集合---Map
- LeetCode 70. Climbing Stairs (Python)