#include <iostream> #include <vector> using namespace std; string solve(int k) { vector<int> bits; while(k) { bits.push_back(k & 1); k >>= 1; } string ret = "1"; int c = 0; for(auto it = next(bits.rbegin()); it != bits.rend(); it++) { ret += "*(1+1)"; if(*it == 1) { c += 1; ret += "+1)"; } } return string(c, '(') + ret; } int main() { int t, k; cin >> t; while(t--) { cin >> k; cout << solve(k) << "\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> #include <vector> using namespace std; string solve(int k) { vector<int> bits; while(k) { bits.push_back(k & 1); k >>= 1; } string ret = "1"; int c = 0; for(auto it = next(bits.rbegin()); it != bits.rend(); it++) { ret += "*(1+1)"; if(*it == 1) { c += 1; ret += "+1)"; } } return string(c, '(') + ret; } int main() { int t, k; cin >> t; while(t--) { cin >> k; cout << solve(k) << "\n"; } } |