diff --git a/cpp/2210/221020.cpp b/cpp/2210/221020.cpp new file mode 100644 index 0000000..254b44e --- /dev/null +++ b/cpp/2210/221020.cpp @@ -0,0 +1,39 @@ +#include +#include + +/** + * 12. Integer to Roman + * + * No desc since clear + */ + +class Solution { +public: + static std::string intToRoman(int num) { + static const char* s = "IVXLCDM"; + std::string ret; + for (int i = 0; i < 6; i += 2, num /= 10) { + int n = num % 10; + if (n == 4 || n == 9) { + ret.push_back(s[i + 1 + (n > 5)]); + ret.push_back(s[i]); + } else { + ret.append(n % 5, s[i]); + if (n >= 5) ret.push_back(s[i + 1]); + } + } + ret.append(num, 'M'); + std::reverse(ret.begin(), ret.end()); + return ret; + } +}; + +int main() { + for (int i = 1; i <= 50; ++i) { + std::cout << Solution::intToRoman(i) << "\n"; + } + std::cout << Solution::intToRoman(3) << "\n"; + std::cout << Solution::intToRoman(58) << "\n"; + std::cout << Solution::intToRoman(1994) << "\n"; + return 0; +} diff --git a/cpp/2210/CMakeLists.txt b/cpp/2210/CMakeLists.txt index c5d055b..b8cde8e 100644 --- a/cpp/2210/CMakeLists.txt +++ b/cpp/2210/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2210) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2210 221020-CN.cpp) +ADD_EXECUTABLE(2210 221020.cpp)