懒得动脑子,还就那个if-else枚举
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
string s;
int m = num / 1000;
for (int i = 0; i < m; i++) {
s.append("M");
}
num = num % 1000;
int c = num / 100;
num = num % 100;
if (c == 9) {
s.append("CM");
c = 0;
}
else if (c == 4) {
s.append("CD");
c = 0;
}
else if (c > 4) {
s.append("D");
c = c - 5;
}
for (int i = 0; i < c; i++){
s.append("C");
}
int x = num / 10;
num = num % 10;
if (x == 9) {
s.append("XC");
x = 0;
}
else if(x == 4) {
s.append("XL");
x = 0;
}
else if (x > 4) {
s.append("L");
x = x - 5;
}
for (int i = 0; i < x; i++) {
s.append("X");
}
if (num == 9) {
s.append("IX");
num = 0;
}
else if (num == 4) {
s.append("IV");
num = 0;
}
else if (num > 4) {
s.append("V");
num = num - 5;
}
for (int i = 0; i < num; i++) {
s.append("I");
}
return s;
}
};
int main() {
Solution test;
int num = 1994;
cout << test.intToRoman(num);
}