PAT甲级1059. Prime Factors (25)
来源:互联网 发布:龙腾世纪 知乎 编辑:程序博客网 时间:2024/05/29 08:09
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N =
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 =
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
#include <vector>#include <iostream>#include <cmath>using namespace std;bool isPrime(long int N){ if(N==1) return false; if(N==2) return true; int r=floor(sqrt(N)+0.5); for(long int i=2;i<=r;i++) { if(N%i==0) return false; } return true;}struct PF{ long int prime; long int expo; PF(long int x,long int y):prime(x),expo(y){}};vector<PF> getPF(long int N){ vector<PF> result; int count=0; long int num=N; for(int i=2;i<=N;i++){ if(num<=1) break;//没有这句会超时 int j=0; int flag=1; if(isPrime(i)) { count=0; while(num%i==0){ num/=i; count++; } if(count>0) { result.push_back(PF(i,count)); } } } return result;}int main(){ long int N; cin>>N; if(N==1) { cout<<"1=1"<<endl; return 0; } vector<PF> result=getPF(N); int sz=result.size(); cout<<N<<"="; if(result[0].expo>1) cout<<result[0].prime<<"^"<<result[0].expo; else cout<<result[0].prime; for(long int k=1;k<sz;k++){ if(result[k].expo>1) cout<<"*"<<result[k].prime<<"^"<<result[k].expo; else cout<<"*"<<result[k].prime; } 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)
- [HAOI2016]找相同子串
- 军训队列
- Android动画(Android Animation)笔记
- MongoDB文档翻译-聚集-对用户的爱好进行聚集
- Algorithm: Quick Sort Mind and Related Questions
- PAT甲级1059. Prime Factors (25)
- ASP.NET Web API 基本操作(CRUD)
- 通过SSH监控远程主机的交互式工具-rtop
- react_native_router_flux_使用详解(二)
- 科普:Bin与Hex是什么鬼
- 模拟网络驱动snull
- Android绘图Canvas笔记
- 2017.2.28
- PAT 1003