#include <cstdio> #include <vector> char out[10000]; inline void jed(int l) { for (int i = 0; i < l-1; i++) printf("1+"); printf("1"); } inline void dwa() { } int main() { int t; scanf("%d", &t); while (t--) { int o = 0; int k; scanf("%d", &k); if (k == 1) { printf("1\n"); continue; } std::vector<int> mod; while (k > 1) { mod.push_back(k % 2); //o += sprintf(out+o, "%d", k%2); k /= 2; } for (int i = 0; i < mod.size(); i++) { o += sprintf(out+o, "(1+1)"); if (i != mod.size()-1 ) o += sprintf(out+o, "*("); } for (int i = mod.size()-1; i >= 0; i--) { if (mod[i] == 1) o += sprintf(out+o, "+1"); if (i > 0) o += sprintf(out+o, ")"); } for (int i = 0; i < o-5; i++) if (out[i] == '(' && out[i+1] == '(' && out[i+5] == ')' && out[i+6] == ')') { out[i] = ' '; out[i+5] = ' '; } for (int i = 0; i < o; i++) if (out[i] != ' ') putc(out[i], stdout); printf("\n"); } }
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 61 62 63 | #include <cstdio> #include <vector> char out[10000]; inline void jed(int l) { for (int i = 0; i < l-1; i++) printf("1+"); printf("1"); } inline void dwa() { } int main() { int t; scanf("%d", &t); while (t--) { int o = 0; int k; scanf("%d", &k); if (k == 1) { printf("1\n"); continue; } std::vector<int> mod; while (k > 1) { mod.push_back(k % 2); //o += sprintf(out+o, "%d", k%2); k /= 2; } for (int i = 0; i < mod.size(); i++) { o += sprintf(out+o, "(1+1)"); if (i != mod.size()-1 ) o += sprintf(out+o, "*("); } for (int i = mod.size()-1; i >= 0; i--) { if (mod[i] == 1) o += sprintf(out+o, "+1"); if (i > 0) o += sprintf(out+o, ")"); } for (int i = 0; i < o-5; i++) if (out[i] == '(' && out[i+1] == '(' && out[i+5] == ')' && out[i+6] == ')') { out[i] = ' '; out[i+5] = ' '; } for (int i = 0; i < o; i++) if (out[i] != ' ') putc(out[i], stdout); printf("\n"); } } |