#include<cstdio> #include<list> #include<string> using namespace std; list<char> result; void brackets() { if(result.size() < 3) return; result.push_front('('); result.push_back(')'); } void append(string text) { for(char t: text) { result.push_back(t); } } void add_1() { brackets(); append("+1"); } void multiply_by_2() { brackets(); append("*(1+1)"); } void calc(int x) { if(x==1) result.push_back('1'); else { calc(x/2); multiply_by_2(); if(x%2) add_1(); } } void print_res(int x) { result.clear(); calc(x); for(char l: result) { putchar(l); } putchar('\n'); } int main() { int K; scanf("%d", &K); for(int i = 0; i<K; i++) { int x; scanf("%d", &x); print_res(x); } 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 | #include<cstdio> #include<list> #include<string> using namespace std; list<char> result; void brackets() { if(result.size() < 3) return; result.push_front('('); result.push_back(')'); } void append(string text) { for(char t: text) { result.push_back(t); } } void add_1() { brackets(); append("+1"); } void multiply_by_2() { brackets(); append("*(1+1)"); } void calc(int x) { if(x==1) result.push_back('1'); else { calc(x/2); multiply_by_2(); if(x%2) add_1(); } } void print_res(int x) { result.clear(); calc(x); for(char l: result) { putchar(l); } putchar('\n'); } int main() { int K; scanf("%d", &K); for(int i = 0; i<K; i++) { int x; scanf("%d", &x); print_res(x); } return 0; } |