43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
int n;
|
||
|
||
string dfs(int n) {
|
||
if (n == 0) return "0";
|
||
string res = "";
|
||
|
||
//遍历二进制的每一位
|
||
for (int i = 14; i >= 0; i--) {//从大到小噢
|
||
//13最后一个点会WA,14可以AC
|
||
//2^15=32768
|
||
//2^14=16384
|
||
//2^13=8192
|
||
//2*10^4=2*10000=20000
|
||
if ((n >> i) & 1) { //如果本位上有数字1
|
||
if (i == 1)//2^1需要写成2的形式,这是递归的出口
|
||
res += "2";
|
||
else {
|
||
//2^0,2^2,2^3...都需要写成2(n)的形式
|
||
res += "2(";
|
||
if (i > 2)//大于2的才需要再次分解,递归就可以了
|
||
res += dfs(i);
|
||
else//这里处理0和2的情况,它俩是一样的处理方式2(0),2(2),也就是2(to_string(i))
|
||
res += to_string(i);
|
||
//加上后扩号
|
||
res += ")";
|
||
}
|
||
//利用+号拼接在一起
|
||
res += "+";
|
||
}
|
||
}
|
||
//控制是不是显示最后的+号
|
||
if (res[res.size() - 1] == '+')res.pop_back();//最后一位是+号的,肯定是加多了,弹出去~
|
||
return res;
|
||
}
|
||
|
||
int main() {
|
||
cin >> n;
|
||
cout << dfs(n) << endl;
|
||
return 0;
|
||
} |