#include <cstdio> #include <stack> using namespace std; stack<char> S; int main() { int z, k; int current_pow = 0, current_bit = 0; int open_brackets = 0; scanf("%d", &z); while (z--) { scanf("%d", &k); if (k == 1) { printf("1\n"); continue; } do { // printf("k is: %d\n", k); current_bit = k % 2; k /= 2; if (current_bit == 1) { S.push(current_bit + '0'); S.push('+'); } if (k != 1 or current_bit == 1) S.push(')'); S.push('1'); S.push('+'); S.push('1'); if (k != 1 or current_bit == 1) S.push('('); if (k != 1) S.push('*'); S.push(')'); ++open_brackets; } while (k > 1); open_brackets--; S.pop(); while (open_brackets > 0) { S.push('('); --open_brackets; } int counter = 0; while (!S.empty()) { printf("%c", S.top()); if (S.top() == '1') counter++; S.pop(); } // printf("\n%d\n", counter); 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 46 47 48 49 50 51 52 | #include <cstdio> #include <stack> using namespace std; stack<char> S; int main() { int z, k; int current_pow = 0, current_bit = 0; int open_brackets = 0; scanf("%d", &z); while (z--) { scanf("%d", &k); if (k == 1) { printf("1\n"); continue; } do { // printf("k is: %d\n", k); current_bit = k % 2; k /= 2; if (current_bit == 1) { S.push(current_bit + '0'); S.push('+'); } if (k != 1 or current_bit == 1) S.push(')'); S.push('1'); S.push('+'); S.push('1'); if (k != 1 or current_bit == 1) S.push('('); if (k != 1) S.push('*'); S.push(')'); ++open_brackets; } while (k > 1); open_brackets--; S.pop(); while (open_brackets > 0) { S.push('('); --open_brackets; } int counter = 0; while (!S.empty()) { printf("%c", S.top()); if (S.top() == '1') counter++; S.pop(); } // printf("\n%d\n", counter); printf("\n"); } return 0; } |