递归--2的幂次方

淺唱寂寞╮ 提交于 2020-03-12 11:33:53
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
using namespace std;

string change(int x)
{
    if(x==0)
    return "0";
    if(x==1)
    return "";
    vector<int>m;
    while(x!=0)
    {
        m.push_back(x%2);
        x/=2;
    }
    string n="";
    for(int i=m.size()-1;i>=0;i--)
    {
        if(m[i]!=0)
        {
          if(change(i)!="")
        n=n+"2("+change(i)+")+";
        else
        {
            n=n+"2+";
        }
        
        }
    }
    n.erase(n.end()-1);
    return  n;
}
int main()
{
    int x;
    while(scanf("%d",&x)!=EOF)
    {
        cout<<change(x)<<endl;
    }
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!