Files
python/TangDou/LuoGuBook/P1010.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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最后一个点会WA14可以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;
}