#include <stdio.h> #include <stdlib.h> char *L; int l; int jed(int K,int S) { int f = 0; while (K > 1) { if (S <= 0) return -1; if (f == 1) L[l++] = '*'; if (K % 5 == 0) { K /= 5; S -= 5; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 4 == 0) { K /= 4; S -= 4; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 3 == 0) { K /= 3; S -= 3; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 2 == 0) { K /= 2; S -= 2; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else { K--; S--; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; if (jed(K,S) == -1) return -1; L[l++] = ')'; return 0; } } return 0; } int main() { int t,T,K,k,S,i,j; scanf("%d",&T); for (t = 0; t < T; t++) { scanf("%d",&K); if (K < 6) { for (k = 0; k < K-1; k++) printf("1+"); printf("1\n"); } else { k = K; S = 0; L = (char*) malloc(10000*sizeof(char)); for (l = 0; l < 10000; l++) L[l] = 0; l = 0; if (jed(K,100) == -1) printf("NIE\n"); else printf("%s\n",L); } } 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <stdio.h> #include <stdlib.h> char *L; int l; int jed(int K,int S) { int f = 0; while (K > 1) { if (S <= 0) return -1; if (f == 1) L[l++] = '*'; if (K % 5 == 0) { K /= 5; S -= 5; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 4 == 0) { K /= 4; S -= 4; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 3 == 0) { K /= 3; S -= 3; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else if (K % 2 == 0) { K /= 2; S -= 2; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; L[l++] = '1'; L[l++] = ')'; f = 1; } else { K--; S--; L[l++] = '('; L[l++] = '1'; L[l++] = '+'; if (jed(K,S) == -1) return -1; L[l++] = ')'; return 0; } } return 0; } int main() { int t,T,K,k,S,i,j; scanf("%d",&T); for (t = 0; t < T; t++) { scanf("%d",&K); if (K < 6) { for (k = 0; k < K-1; k++) printf("1+"); printf("1\n"); } else { k = K; S = 0; L = (char*) malloc(10000*sizeof(char)); for (l = 0; l < 10000; l++) L[l] = 0; l = 0; if (jed(K,100) == -1) printf("NIE\n"); else printf("%s\n",L); } } return 0; } |