#include <iostream> #include <stdio.h> #include <string> using namespace std; int min_bits = 3; int max_bits = 6; char letters[4] = {'a', 'c', 'g', 'o'}; void print_answer(int n, int ones) { char answer [n + 1]; answer[n] = '\0'; int i = 3; for (; i >= 0; i--) { // cout << min_bits << ' ' << (min_bits + i) * n << " " << ones << endl; if ((min_bits + i) * n <= ones) { break; } } for (int k = 0; k < n; k++) { answer[k] = letters[i]; } // cout << answer << endl; for (int k = 0; k < ones - (min_bits + i) * n; k++) { // cout << answer[k] << " " << letters[i+1] << endl; answer[k] = letters[i+1]; } printf("%s", answer); } int main() { int n; int ones = 0; scanf("%d", &n); char temp[8 * n]; scanf("%s", temp); for (int i = 0; i < 8 * n; i++) { if (temp[i] == '1') ones++; } if (ones < min_bits * n || ones > max_bits * n) { printf("NIE"); return 0; } print_answer(n, ones); 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 | #include <iostream> #include <stdio.h> #include <string> using namespace std; int min_bits = 3; int max_bits = 6; char letters[4] = {'a', 'c', 'g', 'o'}; void print_answer(int n, int ones) { char answer [n + 1]; answer[n] = '\0'; int i = 3; for (; i >= 0; i--) { // cout << min_bits << ' ' << (min_bits + i) * n << " " << ones << endl; if ((min_bits + i) * n <= ones) { break; } } for (int k = 0; k < n; k++) { answer[k] = letters[i]; } // cout << answer << endl; for (int k = 0; k < ones - (min_bits + i) * n; k++) { // cout << answer[k] << " " << letters[i+1] << endl; answer[k] = letters[i+1]; } printf("%s", answer); } int main() { int n; int ones = 0; scanf("%d", &n); char temp[8 * n]; scanf("%s", temp); for (int i = 0; i < 8 * n; i++) { if (temp[i] == '1') ones++; } if (ones < min_bits * n || ones > max_bits * n) { printf("NIE"); return 0; } print_answer(n, ones); return 0; } |