#include<stdio.h> #include<string.h> #define MAX_LENGTH 10000 int main() { char arr[MAX_LENGTH]; char* current_arr; int result, k, current; int opened_counter; const char three[] = "1+1+1"; const char two[] = "1+1"; const char three_mul[] = "(1+1+1)*("; const char two_mul[] = "(1+1)*("; const char one_plus[] = "1+"; const char* arr_to_add = NULL; int val_to_add = 0; int t,i; scanf("%d\n", &t); for (i=0; i<t; ++i) { scanf("%d\n", &k); arr[0] = '\0'; current_arr = arr; result = 0; opened_counter = 0; current = k; while ( current > 1 ) { switch (current) { case 3: arr_to_add = three; val_to_add = 5; current = 1; result += 3; break; case 2: arr_to_add = two; val_to_add = 3; current = 1; result += 2; break; default: if ( (current%3) == 0 ) { arr_to_add = three_mul; val_to_add = 9; result += 3; ++opened_counter; current /= 3; } else if ( (current%2) == 0 ) { arr_to_add = two_mul; val_to_add = 7; result += 2; ++opened_counter; current /= 2; } else { arr_to_add = one_plus; val_to_add = 2; ++result; --current; } break; } memcpy(current_arr, arr_to_add, val_to_add); current_arr += val_to_add; } if (opened_counter>0) { memset(current_arr, ')', opened_counter); current_arr += opened_counter; } (*current_arr) = '\0'; if ( k == 1 ) { //special case arr[0] = '1'; arr[1] = '\0'; } if (result<=100) { printf("%s\n",arr); } else { printf("NIE\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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include<stdio.h> #include<string.h> #define MAX_LENGTH 10000 int main() { char arr[MAX_LENGTH]; char* current_arr; int result, k, current; int opened_counter; const char three[] = "1+1+1"; const char two[] = "1+1"; const char three_mul[] = "(1+1+1)*("; const char two_mul[] = "(1+1)*("; const char one_plus[] = "1+"; const char* arr_to_add = NULL; int val_to_add = 0; int t,i; scanf("%d\n", &t); for (i=0; i<t; ++i) { scanf("%d\n", &k); arr[0] = '\0'; current_arr = arr; result = 0; opened_counter = 0; current = k; while ( current > 1 ) { switch (current) { case 3: arr_to_add = three; val_to_add = 5; current = 1; result += 3; break; case 2: arr_to_add = two; val_to_add = 3; current = 1; result += 2; break; default: if ( (current%3) == 0 ) { arr_to_add = three_mul; val_to_add = 9; result += 3; ++opened_counter; current /= 3; } else if ( (current%2) == 0 ) { arr_to_add = two_mul; val_to_add = 7; result += 2; ++opened_counter; current /= 2; } else { arr_to_add = one_plus; val_to_add = 2; ++result; --current; } break; } memcpy(current_arr, arr_to_add, val_to_add); current_arr += val_to_add; } if (opened_counter>0) { memset(current_arr, ')', opened_counter); current_arr += opened_counter; } (*current_arr) = '\0'; if ( k == 1 ) { //special case arr[0] = '1'; arr[1] = '\0'; } if (result<=100) { printf("%s\n",arr); } else { printf("NIE\n"); } } return 0; } |