#include <cstdlib> #include <iostream> #include <string> #include <vector> #include <math.h> using namespace std; string solve(int n) { string result = ""; if (n == 1) { result.append("1"); return result; } string sign = ""; int bracketCount = 0; while (n > 1) { if (n % 3 == 0) { n = n / 3; sign = "*"; result.append("(1+1+1)"); } else if (n % 2 == 0) { n = n / 2; sign = "*"; result.append("(1+1)"); } else { n = n - 1; sign = "+"; bracketCount++; result.append("(1"); } if (n > 1) { result.append(sign); } } for (int i=0; i<bracketCount; i++) { result.append(")"); } return result; } int main(int argc, char** argv) { long t; long n; vector<string> results; cin >> t; for (int i=0; i<t; i++) { cin >> n; results.push_back(solve(n)); } for(int i=0; i<results.size(); i++){ cout << results[i] << 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 55 56 57 58 59 60 | #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <math.h> using namespace std; string solve(int n) { string result = ""; if (n == 1) { result.append("1"); return result; } string sign = ""; int bracketCount = 0; while (n > 1) { if (n % 3 == 0) { n = n / 3; sign = "*"; result.append("(1+1+1)"); } else if (n % 2 == 0) { n = n / 2; sign = "*"; result.append("(1+1)"); } else { n = n - 1; sign = "+"; bracketCount++; result.append("(1"); } if (n > 1) { result.append(sign); } } for (int i=0; i<bracketCount; i++) { result.append(")"); } return result; } int main(int argc, char** argv) { long t; long n; vector<string> results; cin >> t; for (int i=0; i<t; i++) { cin >> n; results.push_back(solve(n)); } for(int i=0; i<results.size(); i++){ cout << results[i] << endl; } return 0; } |