课外1-[NOIP 1998 普及组] 幂次方

· · 个人记录


#include<bits/stdc++.h>
using namespace std;
void solve(int n)
{
    if(n==0)
    {
        cout<<"0";
        return;
    }
    bool first=true;//第一个数
    for(int i=31;i>=0;i--)
    {
        if(n&(1<<i))
        {
            if(!first) cout<<"+";//第一个数不要+号
            first=false;
            if(i==0) cout<<"2(0)";
            else if(i==1) cout<<"2";
            else
            {
                cout<<"2(";
                solve(i);
                cout<<")";
            }
        }
    }
}
int main()
{
    int n;
    cin>>n;
    solve(n);
    return 0;
}