#include <iostream> #include <string> #include <bitset> #include <algorithm> std::string formWord(std::string word, int zeros, int ones, int n) { std::string newWord = ""; for (int i = 0; i < n; i++) { // 5 zeros && 3 ones || 2 zeros && 6 ones //std::cout << zeros << " " << ones<<std::endl; //std::cout << zeros / 5 << " " << ones / 3 << std::endl; if (9 * zeros > 7 * ones && zeros >= 5 && ones >= 3) { newWord += "a"; // a zeros -= 5; ones -= 3; } else if(zeros >= 2 && ones >= 6) { newWord += "o"; // o zeros -= 2; ones -= 6; } else { newWord += (zeros == 3) ? "g" : "c"; // g or c } } return newWord; } int main() { // Number of letters int n; // Letters std::string word; std::cin >> n >> word; int zeros = std::count(word.begin(), word.end(), '0'); int ones = std::count(word.begin(), word.end(), '1'); // Can word be made if (zeros >= 2 * n && ones >= 3 * n) { std::cout << formWord(word, zeros, ones, n); } else { std::cout << "NIE"; } 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 | #include <iostream> #include <string> #include <bitset> #include <algorithm> std::string formWord(std::string word, int zeros, int ones, int n) { std::string newWord = ""; for (int i = 0; i < n; i++) { // 5 zeros && 3 ones || 2 zeros && 6 ones //std::cout << zeros << " " << ones<<std::endl; //std::cout << zeros / 5 << " " << ones / 3 << std::endl; if (9 * zeros > 7 * ones && zeros >= 5 && ones >= 3) { newWord += "a"; // a zeros -= 5; ones -= 3; } else if(zeros >= 2 && ones >= 6) { newWord += "o"; // o zeros -= 2; ones -= 6; } else { newWord += (zeros == 3) ? "g" : "c"; // g or c } } return newWord; } int main() { // Number of letters int n; // Letters std::string word; std::cin >> n >> word; int zeros = std::count(word.begin(), word.end(), '0'); int ones = std::count(word.begin(), word.end(), '1'); // Can word be made if (zeros >= 2 * n && ones >= 3 * n) { std::cout << formWord(word, zeros, ones, n); } else { std::cout << "NIE"; } return 0; } |