#include <cstdio> #include <stack> #include <list> int main() { int t, k; std::stack< char > s; std::list< char > e; for(scanf("%d", &t); t; t--) { e.clear(); scanf("%d", &k); while(k > 1) { if(k%2 == 1 || k == 2) { s.push('a'); k--; } else { s.push('m'); k /= 2; } } e.push_back('1'); while(!s.empty()) { if(s.top() == 'a') { e.push_back('+'); e.push_back('1'); } else { e.push_front('('); e.push_back(')'); e.push_back('*'); e.push_back('('); e.push_back('1'); e.push_back('+'); e.push_back('1'); e.push_back(')'); } s.pop(); } for(auto &c: e) { printf("%c", c); } printf("\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 45 | #include <cstdio> #include <stack> #include <list> int main() { int t, k; std::stack< char > s; std::list< char > e; for(scanf("%d", &t); t; t--) { e.clear(); scanf("%d", &k); while(k > 1) { if(k%2 == 1 || k == 2) { s.push('a'); k--; } else { s.push('m'); k /= 2; } } e.push_back('1'); while(!s.empty()) { if(s.top() == 'a') { e.push_back('+'); e.push_back('1'); } else { e.push_front('('); e.push_back(')'); e.push_back('*'); e.push_back('('); e.push_back('1'); e.push_back('+'); e.push_back('1'); e.push_back(')'); } s.pop(); } for(auto &c: e) { printf("%c", c); } printf("\n"); } return 0; } |