#include <cstdio>
#include <string>
using namespace std;
string rozloz(int n) {
int arr[32];
int tmp = n;
int i = 0;
while (tmp > 0) {
arr[i] = tmp & 1;
i++;
tmp >>= 1;
}
int ile_jedynek = 0;
int bits = i;
string base = "(1+1)";
string res = "";
for (i=bits-1; i>=0; i--) {
// res = res * base + digit;
if (res.length() > 0) {
res = res + "*" + base;
ile_jedynek += 2;
}
if (res.length() > 0) {
if (arr[i] == 1) {
ile_jedynek++;
res = "(" + res + "+" + to_string(arr[i]) + ")";
}
} else {
if (arr[i] == 1) {
res = "1";
ile_jedynek++;
}
}
}
// printf("ile_jedynek=%d\n",ile_jedynek);
return res;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
string solution = rozloz(n);
printf("%s\n", solution.c_str());
}
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 | #include <cstdio> #include <string> using namespace std; string rozloz(int n) { int arr[32]; int tmp = n; int i = 0; while (tmp > 0) { arr[i] = tmp & 1; i++; tmp >>= 1; } int ile_jedynek = 0; int bits = i; string base = "(1+1)"; string res = ""; for (i=bits-1; i>=0; i--) { // res = res * base + digit; if (res.length() > 0) { res = res + "*" + base; ile_jedynek += 2; } if (res.length() > 0) { if (arr[i] == 1) { ile_jedynek++; res = "(" + res + "+" + to_string(arr[i]) + ")"; } } else { if (arr[i] == 1) { res = "1"; ile_jedynek++; } } } // printf("ile_jedynek=%d\n",ile_jedynek); return res; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); string solution = rozloz(n); printf("%s\n", solution.c_str()); } return 0; } |
English