ACM 训练日志记录 · 思考 · 成长 提交 / 修改记录
郭一鸣 / 题目列表训练档案
导出

P1010 [NOIP 1998 普及组] 幂次方

来源:洛谷 ★ 1000

本题活力0.30按难度、完成结果与训练证据估算

算法如山行则将至

题目描述

将正整数表示为2的幂次方和,用特定格式括号表示指数。

代码

#include<iostream>
using namespace std;
void dfs(int n)
{
    int times=0,pows=1;
    while(pows*2<=n)
    {
        pows*=2;
        times++;
    }
    bool fla=true;
    while(times>=0)
    {
        if(n>=pows)
        {
            if(!fla)cout<<"+";
            fla=false;
            if(times==0)cout<<"2(0)";
            else if(times==1)cout<<"2";
            else{
                cout<<"2(";
                dfs(times);
                cout<<")";
            }
            n-=pows;
        }
        pows/=2;
        times--;
    }
}
int main()
{
    int n;cin>>n;
    dfs(n);
    cout<<endl;
    return 0;
}