#include<iostream> #include<bitset> #include<string> using namespace std; void write(int n) { bitset<30> b(n); string res = ""; bool started = false; int parentheses = 0; for (int i = 30; i >= 0; i--) { if (started) { if (b[i]) { res += "+1"; if (i > 0) { res += ")"; parentheses++; } } if (i > 0) res += "*(1+1)"; } else if (b[i]) { res = "(1+1)"; started = true; } } cout << string(parentheses, '(') << res << '\n'; } int main() { int t, n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; if (n == 1) { cout << "1\n"; continue; } write(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 32 33 34 35 36 37 38 39 40 41 | #include<iostream> #include<bitset> #include<string> using namespace std; void write(int n) { bitset<30> b(n); string res = ""; bool started = false; int parentheses = 0; for (int i = 30; i >= 0; i--) { if (started) { if (b[i]) { res += "+1"; if (i > 0) { res += ")"; parentheses++; } } if (i > 0) res += "*(1+1)"; } else if (b[i]) { res = "(1+1)"; started = true; } } cout << string(parentheses, '(') << res << '\n'; } int main() { int t, n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; if (n == 1) { cout << "1\n"; continue; } write(n); } } |