参考一下?
```cpp
#include <bits/stdc++.h>
using namespace std;
int q;
string abc(int n)
{
if (n==2)
{
return "2";
}
else if (n==1)
{
return "2(0)";
}
else if (n==3)
{
return "2+2(0)";
}
else
{
int a=log(n)/log(2);
int b=pow(2,a);
if (n==b)
{
return "2("+abc(a)+")";
}
else
{
return "2("+abc(a)+")+"+abc(n-b);
}
}
}
int main()
{
cin>>q;
cout<<abc(q);
return 0;
}
```
~~(求关注)~~
by 2011Andy @ 2022-10-27 21:33:07
Like this:
```cpp
#include <bits/stdc++.h>
using namespace std;
int p2[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
string s;
void dfs(int x)
{
if(x==0||x==1)
{
s+=(x==1?"2+":"2(0)+");
return;
}
int y=x;
s+="2(";
for(int i=16;i>=0;i--)
{
if(p2[i]<=y)
{
dfs(i);
y-=p2[i];
}
}
s+=")+";
}
int main()
{
int n;
scanf("%d",&n);
dfs(n);
int len=s.size();
for(int i=2;i<len-2;i++)
{
if(s[i-1]==')'&&s[i]=='+'&&s[i+1]==')') continue;
if((s[i-1]=='0'||s[i-1]=='2')&&s[i]=='+'&&s[i+1]==')') continue;
printf("%c",s[i]);
}
}
```
by AsoltA @ 2022-12-30 10:33:37
当
2(n)比n小,则输出'+'.
```#include<iostream>
using namespace std;
void f(int a)
{
if(a==0) return;
int t=1,k=0;
while(t<=a)
{
t*=2;
k++;
}
t/=2;k--;
if(k==0)
cout<<"2(0)";
else if(k==1)
cout<<"2";
else
{
cout<<"2(";
f(k);
cout<<")";
}
if(t<a)
{
cout<<"+";
f(a-t);
}
}
int main()
{
int n;
cin>>n;
f(n);
return 0;
}
by xxc123 @ 2023-04-10 21:59:58