weinan030416

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

dfs:2的幂次方表示数字

复制代码
#include<iostream>
#include<vector>
#include<bitset>
#include<string>
#include<algorithm>
using namespace std;

vector<string> res;
void dfs(int x)
{
    bitset<16>    a(x);//转换为2进制 ,根据题目给的最大数设置bitset位数 
    bool isfirstone=true;
    for(int i=15;i>=0;--i)
    {
        if(a.test(i))//第i位置是1 
        {
            if(!isfirstone)//不是第一个数 
            {
                res.push_back("+");
            }
            else
                isfirstone=false;
            if(i==0)
                res.push_back("2(0)");//除了1和2直接写上,其他要转换 
            else if(i==1)
                res.push_back("2");
            else
            {
                res.push_back("2(");//例子:把2的7次方这种变为2(2(2)+2+2(0)) 4+2+1
                dfs(i);
                res.push_back(")");
            }
        }
    }
    return; 
}
int main()
{
    int x;
    cin>>x;
    dfs(x);
    for(vector<string>::iterator m=res.begin();m!=res.end();m++)
    {
        cout<<*m;
    }
 }
复制代码

 

posted on   楠030416  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示