add: 221020
This commit is contained in:
parent
af18853bc3
commit
87d5c03860
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2210)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2210 221020-CN.cpp)
|
||||
ADD_EXECUTABLE(2210 221020.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue