#include <iostream> #include <string> using namespace std; inline string jed(long long n) { if(n == 1) return "1"; string res; res.reserve(1024); int bracets = 0; bool open = false; while(n != 1) { if(n % 2 == 0) { n /= 2; if(open && n == 1) res.append("1+1"); else res.append("(1+1)"); if (n != 1) { res.append("*("); bracets++; open = true; } } else { n -= 1; res.append("1+"); open = false; } } res.append(string(bracets, ')')); return res; } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); long long t, k; cin >> t; for(int testCase = 0; testCase < t; ++testCase) { cin >> k; cout << jed(k) << "\n"; } 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 | #include <iostream> #include <string> using namespace std; inline string jed(long long n) { if(n == 1) return "1"; string res; res.reserve(1024); int bracets = 0; bool open = false; while(n != 1) { if(n % 2 == 0) { n /= 2; if(open && n == 1) res.append("1+1"); else res.append("(1+1)"); if (n != 1) { res.append("*("); bracets++; open = true; } } else { n -= 1; res.append("1+"); open = false; } } res.append(string(bracets, ')')); return res; } int main(int argc, char** argv) { ios_base::sync_with_stdio(false); long long t, k; cin >> t; for(int testCase = 0; testCase < t; ++testCase) { cin >> k; cout << jed(k) << "\n"; } return 0; } |