Fork me on GitHub

UESTC 1237 质因子分解

 水题一枚。。

 

#include<iostream>   
#include<cstdio>   
#include<cstring>   
#include<cmath>   
#include<algorithm>   
#include<string>   
 
using namespace std;   
  
int isprime(int b,int n)   
{   
    int i;   
    for(i=b;i<=sqrt(n);i++)   
    {   
        if(n%i==0)   
            return i;   
    }   
    return 1;   
}   
  
int res[10005];   
  
int main()   
{   
    int t,i;   
    int x,tmp;   
    cin>>t;   
    while(t--)   
    {   
        cin>>x;   
        tmp = x;   
        int k = 0;   
        while(tmp%2==0)   
        {   
            tmp/=2;   
            res[k++]=2;   
        }   
        if(tmp>2)   
        {   
            i=3;   
            while(1)   
            {   
                if(tmp == 1)   
                    break;   
                if(tmp%i==0)   
                {   
                    res[k++]=i;   
                    tmp/=i;   
                }   
                else  
                {   
                    int ca = isprime(i,tmp);   
                    if(ca == 1)   
                    {   
                        res[k++]=tmp;   
                        break;   
                    }   
                    else  
                        i=ca;   
                }   
            }   
        }   
        cout<<x<<"="<<res[0];   
        for(i=1;i<k;i++)   
        {   
            cout<<"*"<<res[i];   
        }   
        cout<<endl;   
  
    }   
    return 0;   
}   
View Code

 

posted @ 2013-12-13 12:57  whatbeg  阅读(235)  评论(0编辑  收藏  举报