随手写的递归
第一次没有讨论x=0的返回值,报错
第二次没有讨论x>pow(2,t),报错
第三次没有讨论x=3的返回值,答案怪怪的
感觉讨论到x=2就差不多了,但会出现2=2(2(0)),恼火
#include <bits/stdc++.h>
using namespace std;
string represent(int x)
{
if(x==0) return "";
if(x==1) return "2(0)";
if(x==2) return "2";
if(x==3) return "2+2(0)";
string s = "";
int t = log2(x);
s += "2(" + represent(t) +")";
if(x>pow(2,t))
s += "+" + represent(x-pow(2,t));
return s;
}
int main()
{
int n;
cin >> n;
cout << represent(n);
return 0;
}