From f44a67a938767ace2acab5bd046dc69b9100f137 Mon Sep 17 00:00:00 2001 From: arasgungore Date: Thu, 10 Mar 2022 11:02:07 +0300 Subject: [PATCH] Updated C++.cpp --- C++.cpp | 121 ++++++++++++++++---------------------------------------- 1 file changed, 35 insertions(+), 86 deletions(-) diff --git a/C++.cpp b/C++.cpp index 37429ac..82472a5 100644 --- a/C++.cpp +++ b/C++.cpp @@ -2,112 +2,61 @@ #include #include #include + +#include +#include +#include +#include + + class ContestApplication { - public: +public: void sortAndPrint(std::vector input) { // TODO You are expected to sort the list in the order of r-g-b // and print the sorted list + + // ASCII codes of '0-9,A-F' are ascending just like in the hexadecimal system. + // So a lexicographical order can be used to order these strings. + std::sort(input.begin(), input.end()); + + // Print the list sequentially. + for(const auto i : input) + std::cout << i << std::endl; } - std::vector mix(int i) { // TODO You are expected to refactor mix and convertS methods. // The main targets are "Efficiency" and "Readibility". // You are free to change everything. - std::vector j; + std::srand((unsigned int)std::time(NULL)); + std::vector res; while (i > 0) { - std::string s = "#"; - std::srand((unsigned int)std::time(NULL)); - int c1 = std::rand() % 16; - std::string s1; - s1 = convertS(c1); - s += s1; - int c2 = std::rand() % 16; - std::string s2; - s2 = convertS(c2); - s += s2; - int c3 = std::rand() % 16; - std::string s3; - s3 = convertS(c3); - s += s3; - int c4 = std::rand() % 16; - std::string s4; - s4 = convertS(c4); - s += s4; - int c5 = std::rand() % 16; - std::string s5; - s5 = convertS(c5); - s += s5; - int c6 = std::rand() % 16; - std::string s6; - s6 = convertS(c6); - s += s6; - j.push_back(s); + std::string str = "#"; + for (int j = 1; j <= 6; ++j) { + int ch_j = std::rand() % 16; + std::string str_j = convertS(ch_j); + str += str_j; + } + res.push_back(str); --i; } - return j; + return res; } - std::string convertS(int c1) { - std::string s1; - if (c1 == 0) { - s1 = "0"; - } - if (c1 == 1) { - s1 = "1"; - } - if (c1 == 2) { - s1 = "2"; - } - if (c1 == 3) { - s1 = "3"; - } - if (c1 == 4) { - s1 = "4"; - } - if (c1 == 5) { - s1 = "5"; - } - if (c1 == 6) { - s1 = "6"; - } - if (c1 == 7) { - s1 = "7"; - } - if (c1 == 8) { - s1 = "8"; - } - if (c1 == 9) { - s1 = "9"; - } - if (c1 == 10) { - s1 = "A"; - } - if (c1 == 11) { - s1 = "B"; - } - if (c1 == 12) { - s1 = "C"; - } - if (c1 == 13) { - s1 = "D"; - } - if (c1 == 14) { - s1 = "E"; - } - if (c1 == 15) { - s1 = "F"; - } - return s1; + std::string convertS(const int c1) { + std::stringstream stream; + stream << std::hex << c1; + std::string res = stream.str(); + return res; } }; -void main(std::string args[]) { +int main() { ContestApplication* app = new ContestApplication(); // Task 1: Refactor mix method - std::vector input = app->mix(1000000); + std::vector input = app->mix(100); // Task 2: Implement sortAndPrint method app->sortAndPrint(input); - return; -} \ No newline at end of file + return 0; +}