由于有的数字不需要前面那个括号,所以你得先判断如
3=2+2(0);
如果不判断那么3=2(2(0))+2(0);
自己设计的真是菜
从次数判断要不要加括号,加括号是取决于次数,而不是这个数本身
//count为1不操作,为0,2直接打印,剩下的搜
//count为1时代表这个数是2 ,直接输出即可,就不用操作了
#include <bits/stdc++.h>
using namespace std;
int n;
int a[1001];
string ans;
string sss;
void dfs(int x) {
if (x == 0)
return ;
printf("2");
int i = 1;
int count = 0;
while (i <= x) {
i *= 2;
count++;
}
count--;
i /= 2;
if (count == 0 || count == 2)
printf("(%d)", count);
//count为1不操作,为0,2直接打印,剩下的搜
if (count >= 3) {
cout << "(";
dfs(count);
cout << ")";
}
if (x != i) {
cout << "+";
dfs(x - i);
}
}
int main() {
cin >> n;
dfs(n);
cout << ans;
}