#include <iostream> #include <cstdio> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); if (n == 1) { printf("1\n"); continue; } if (n == 2) { printf("1+1\n"); continue; } int b[100]; int k = 0; while (n > 0) { b[k] = n % 2; n = (n - b[k])/2; k++; } k--; for (int j = 0; j < k-1; j++) printf("("); for (int j = k-1; j >=0; j--) { if (j == k - 1) printf("1+1"); else printf("(1+1)"); if (b[j] > 0) printf("+1"); if (j != 0) printf(")*"); } 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 | #include <iostream> #include <cstdio> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n); if (n == 1) { printf("1\n"); continue; } if (n == 2) { printf("1+1\n"); continue; } int b[100]; int k = 0; while (n > 0) { b[k] = n % 2; n = (n - b[k])/2; k++; } k--; for (int j = 0; j < k-1; j++) printf("("); for (int j = k-1; j >=0; j--) { if (j == k - 1) printf("1+1"); else printf("(1+1)"); if (b[j] > 0) printf("+1"); if (j != 0) printf(")*"); } printf("\n"); } } |