UESTC 982质因子分解

读入一个自然数,将nn分解为质因子连乘的形式输出。

Input

有多组测试数据。输入的第一行是整数TT(0<T≤10000),表示测试数据的组数。每一组测试数据只有一行,包含待分解的自然数nn。该行没有其它多余的符号。1<n<2^31 

Output

对应每组输入,输出一行分解结果,具体样式参看样例。该行不能有其它多余的符号。

Sample input and output

Sample InputSample Output
3
756
2
2093333998
756=2*2*3*3*3*7
2=2
2093333998=2*7*7*17*43*29221
/* ***********************************************
Author        :guanjun
Created Time  :2016/7/14 15:52:06
File Name     :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 50000
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int prime[maxn];
int vis[maxn];
int cnt;
void get(){
    cnt=0;
    cle(vis);
    for(int i=2;i<maxn;i++){
        if(!vis[i]){
            prime[++cnt]=i;
            for(int j=i+i;j<maxn;j+=i){
                vis[j]=1;
            }
        }
    }
}
vector<int>v;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    ll t,n;
    get();
    cin>>t;
    while(t--){
        cin>>n;
        ll tmp=n;
        v.clear();
        for(int i=1;i<=cnt;i++){
            int x=prime[i];
            while(n%x==0){
                n/=x;
                v.push_back(x);
            }
        }
        if(n>1)v.push_back(n);
        int m=v.size();
        printf("%d=",tmp);
        for(int i=0;i<m;i++){
            printf("%d%c",v[i],i==m-1?10:'*');
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/pk28/p/5674454.html