递归--2的幂次方

#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;
}
发布了22 篇原创文章 · 获赞 1 · 访问量 570

猜你喜欢

转载自blog.csdn.net/baidu_37143827/article/details/104755323
今日推荐