4
11
2015
0

XJOI 1003 质因数分解

//学军OJ首杀撒花!!^_^
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    long long n;
    cin>>n;

    int x=0;
    while (n%2==0) {
        ++x;
        n/=2;
    }
    if (x==1) cout<<"2*";
    else if (x)
    {
        cout<<2<<'^'<<x;
        if (n!=1) cout<<'*';
    }
    //处理质因数为2的情况
    for (int i=3;i<=n;i+=2)//因为偶质数只有2,所以只考虑奇数
    {
        x=0;
        while (true) {
            if (n%i==0) {
                ++x;
                n/=i;
            }
            else
            {
                if (x==0) break;
                if (x==1) cout<<i;
                else
                    cout<<i<<'^'<<x;
                if (n!=1) cout<<'*';
                break;
            }
        }
    }
 
    return 0;
}
Category: 题解 | Tags: 数学 XJOI | Read Count: 258

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com