From 5ad6c8b296b44c3170e4ef3be88d5cc77e74eeee Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 3 Apr 2022 19:13:02 +0800 Subject: [PATCH] add: 0012 [cpp] --- cpp/more/0012.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cpp/more/0012.cpp diff --git a/cpp/more/0012.cpp b/cpp/more/0012.cpp new file mode 100644 index 0000000..b5348c1 --- /dev/null +++ b/cpp/more/0012.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +class Solution { +private: + const inline static std::vector> RN = { + {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, + {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, + {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, + {"", "M", "MM", "MMM"} + }; +public: + static std::string intToRoman(int num) { + return RN[3][num / 1000] + + RN[2][(num / 100) % 10] + + RN[1][(num % 100) / 10] + + RN[0][num % 10]; + } +}; + +int main() { + std::cout << Solution::intToRoman(1994); + return 0; +}