add: 221020

This commit is contained in:
Eatswap 2022-10-20 22:40:13 +08:00
parent af18853bc3
commit 87d5c03860
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 40 additions and 1 deletions

39
cpp/2210/221020.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <string>
#include <iostream>
/**
* 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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2210)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2210 221020-CN.cpp) ADD_EXECUTABLE(2210 221020.cpp)