#include <iostream> #include <cstdio> #include <vector> #include <string> using namespace std; int n, x; vector<int> DecToBin(int number) { vector<int> result; if (number == 0) { result.push_back(0); return result; } if (number == 1) { result.push_back(1); return result; } if (number % 2 == 0) { result = DecToBin(number / 2); result.push_back(0); return result; } else { result = DecToBin(number / 2); result.push_back(1); return result; } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x; vector<int> binary = DecToBin(x); string result = "1"; string bracket = ""; for (int j = 1; j < binary.size(); j++) { bracket += "("; result += "*(1+1)"; if (binary[j] == 1) { result += "+1"; } result += ")"; } cout << bracket << result << endl; } return 0; }
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 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <cstdio> #include <vector> #include <string> using namespace std; int n, x; vector<int> DecToBin(int number) { vector<int> result; if (number == 0) { result.push_back(0); return result; } if (number == 1) { result.push_back(1); return result; } if (number % 2 == 0) { result = DecToBin(number / 2); result.push_back(0); return result; } else { result = DecToBin(number / 2); result.push_back(1); return result; } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x; vector<int> binary = DecToBin(x); string result = "1"; string bracket = ""; for (int j = 1; j < binary.size(); j++) { bracket += "("; result += "*(1+1)"; if (binary[j] == 1) { result += "+1"; } result += ")"; } cout << bracket << result << endl; } return 0; } |