题目地址

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p
​1
​​
​k
​1
​​
​​ ×p
​2
​​
​k
​2
​​
​​ ×⋯×p
​m
​​
​k
​m
​​
​​ .

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 = p
​1
​​ ^k
​1
​​ *p
​2
​​ ^k
​2
​​ p
​m
​​ ^k
​m
​​ , where p
​i
​​ 's are prime factors of N in increasing order, and the exponent k
​i
​​ is the number of p
​i
​​ -- hence when there is only one p
​i
​​ , k
​i
​​ is 1 and must NOT be printed out.

Sample Input:
97532468

Sample Output:
97532468=2^211171011291

作者: HE, Qinming
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB

#include <iostream>
#include <vector>
#include<algorithm>
#include <cmath>
#include<map>
#include<cstring>
#include<queue>
#include<string>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=10010,inf=100000000;
struct node{
    int data,cnt;
}fac[maxn];
bool is_prime(int x){
    if(x<=1) return 0;
    int sqr=sqrt(x*1.0);
    for(int i=2;i<=sqr;i++){
        if(x%i==0) return 0;
    }
    return 1;
}
int main(){
    int num,cnt=0,f_cnt=0;
    cin>>num;int temp=num;
    for(int i=2;i<=sqrt(temp*1.0);i++){
        if(num==1) break;
        if(num%i==0){
            fac[f_cnt].data=i;
            fac[f_cnt].cnt=0;
            while(num%i==0){            //主要就是从小到大找因子,以及每个因子的数量;
                fac[f_cnt].cnt++;
                num/=i;
            }
            f_cnt++;
        }
    }
    cout<<temp<<"=";
    if(is_prime(temp)){
         cout<<temp;return 0;
    }
    if(temp==1||temp==0) {
        cout<<temp;
        return 0;
    }
    for(int i=0;i<f_cnt;i++){
        if(i!=0) cout<<"*";
        cout<<fac[i].data;
        if(fac[i].cnt>1) cout<<"^"<<fac[i].cnt;
    }
    if(num!=1)
    cout<<"*"<<num;
}