AcWing 3483. 2的幂次方
原题链接
中等
作者:
故事里的大魔王
,
2025-01-15 13:45:04
,
所有人可见
,
阅读 1
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string change(int n) {
if (n == 0) return "0";
string res = "";
vector<int> pos;
for (int i = 15; i >= 0; --i) if (n >> i & 1) pos.push_back(i);
for (int i = 0; i < pos.size(); ++i) {
if (i != 0) res += "+";
if (pos[i] == 1) res += "2";
else {
res += "2(" + change(pos[i]) + ")";
}
}
return res;
}
int main() {
int n;
while(cin >> n)
cout << change(n) << endl;
return 0;
}