#include <stdio.h> #include <math.h> #include <string> using namespace std; int read_int() { int x; scanf("%d", &x); return x; } string decompose_number(int number) { if (number == 1) { return "1"; } else if (number % 2 == 0) { if (number == 2) { return "(1+1)"; } else { return "(1+1)*" + decompose_number(number/2); } } else { if (number == 3) { return "(1+1+1)"; } else { return "((1+1)*" + decompose_number((number-1)/2) + "+1)"; } } } int main() { int number_of_tests = read_int(); for (int i = 0; i < number_of_tests; ++i) { int number_to_decompose = read_int(); string decomposed_number = decompose_number(number_to_decompose); printf("%s\n", decomposed_number.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 | #include <stdio.h> #include <math.h> #include <string> using namespace std; int read_int() { int x; scanf("%d", &x); return x; } string decompose_number(int number) { if (number == 1) { return "1"; } else if (number % 2 == 0) { if (number == 2) { return "(1+1)"; } else { return "(1+1)*" + decompose_number(number/2); } } else { if (number == 3) { return "(1+1+1)"; } else { return "((1+1)*" + decompose_number((number-1)/2) + "+1)"; } } } int main() { int number_of_tests = read_int(); for (int i = 0; i < number_of_tests; ++i) { int number_to_decompose = read_int(); string decomposed_number = decompose_number(number_to_decompose); printf("%s\n", decomposed_number.c_str()); } return 0; } |