1059. Prime Factors (25)

来源:互联网 发布:佳能mg6280清零软件 编辑:程序博客网 时间:2024/05/23 02:08

特殊处理2,然后一次对x/3,/5。。。判断,知道x变成0

#include<iostream>#include<cmath>using namespace std;int main(){    long int x;    cin >> x;    cout << x << "=";    if (x == 0 || x==1) {//额外处理0,1,的情况        cout << x << endl;exit(0);    }    int count = 0;int flag = 0;    while (x % 2 == 0) {    //先把2处理掉        count++;        x = x / 2;        flag = 1;    }    if (count >1) cout << "2^" << count;    else if (count == 1) cout << "2";    for (int i = 3;x!=1;i += 2)//按3.5.7...顺序循环,知道x变成1    {        count = 0;        while (x%i == 0) {            count++;            x /= i;        }        if (flag == 1) {            if (count > 1) printf("*%d^%d", i, count);            else if (count == 1)printf("*%d", i);        }        else {            if (count > 1) printf("%d^%d", i, count);            else if (count == 1)printf("%d", i);            flag = 1;        }    }    cout << endl;}
0 0