2020-08-22 幂次方

2020-08-22  本文已影响0人  JalorOo

https://www.luogu.com.cn/problem/P1010

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

long long qmi(int m, int k)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1) res = res * t;
        t = t * t;
        k >>= 1;
    }
    return res;
}

int read(){
    int x = 0,f = 1;
    char c = getchar();
    while (c<'0'||c>'9') {
        if (c=='-') {
            f = -1;
        }
        c = getchar();
    }
    while (c>='0'&&c<='9') {
        x = x*10+c-'0';
        c = getchar();
    }
    return x*f;
}

int a;
void getString(int x)
{
    for(int i = 14;i>=0;i--) //两万的数据最多是2(14)
    {
        if(qmi(2,i) <= x){//是否符合题目要求
            if(i==1) cout<<"2"; //2(1)不用再往后分解了且2^1输出为2,单独出来
            else if(i==0) cout<<"2(0)"; //2(0)也不用再往后分解了,单独出来
            else{ //若i>1则继续分解指数i
                cout<<"2(";
                getString(i);
                cout<<")";
            }
            x -= qmi(2,i); //继续循环分解余下的
            if(x!=0) cout<<"+";
            //加号处理的最简单方法:若此x还没分解完,则后面还有项,所以输出一个+号
        }
    }
}
int main()
{
    a = read();
    getString(a);
    return 0;
}
/*
1315
============
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
*/
上一篇 下一篇

猜你喜欢

热点阅读