1059. Prime Factors (25)
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
题意:将一个整数分解为若干素数的乘积,注意N==1的情况即可。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long int ll;
vector< pair<ll,int> > my_vector;
int main(){
ll num;
cin >> num;
cout << num << "=";
if(num==1){
cout << 1 << endl;
return 0;
}
ll tmp = sqrt(num)+1;
int count;
for(ll i=2; i<=tmp; i++){
if(num%i==0){
count = 0;
do{
count++;
num /= i;
}while(num%i==0);
my_vector.push_back({i,count});
}
}
if(num!=1)
my_vector.push_back({num,1});
int size = my_vector.size();
for(int i=0; i<size; i++){
cout << my_vector[i].first;
if(my_vector[i].second>1)
cout << "^" << my_vector[i].second;
if(i+1!=size)
cout << "*";
}
cout <<endl;
return 0;
}