reference: "One-day-one-leetcode"
requirement : Convert the plastic numbers into Roman numerals, we all know there are 7 Roman numerals, I (1), V (5), X (Ten), L (M), C (), D (+), M. (1000), which has the disadvantage that we need to limit our input in 1~ In 3999, the Roman numeral representation cannot be used because it is greater than 3999 or less than 1.
Code implementation:
1) _simple We enumerate all the possible situations, and then make the corresponding match, the code logic is simple;
2 The second kind we discuss according to the classification, the logic is relatively simple, but the code implementation is more redundant.
#include <iostream> #include <string> #include <vector> using namespace std;
string inttoroman_simple (int num) {int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
String str = "";
for (int i = 0; i < sizeof (values)/sizeof (values[0)); i++) {String str_t = "";
while (Num >= values[i]) {num-= values[i];
str_t + = Numerals[i];
STR + + str_t;
return str;
string inttoroman (int num) {string str = "";
String Roman[7] = {"I", "V", "X", "L", "C", "D", "M"};
int j = 0;
while (num) {string str_t = "";
int temp = num% 10;
if (Temp < 4) {int count = temp;
while (count--) {str_t + = Roman[j];
} else if (temp = = 4) {str_t + = Roman[j]; str_t + = roman[j + 1];
else if (temp >= 5 && temp <= 8) {str_t + = roman[j + 1];
int count = temp-5;
while (count--) {str_t + = Roman[j];
} else{str_t + = Roman[j];
str_t + = Roman[j + 2];
str = str_t + str;
cout << str_t << Endl;
cout << str << Endl;
cout << "==================" << Endl;
Num/= 10;
j = j + 2;
return str;
int main () {int num = 3745;
cout << inttoroman_simple (num) << Endl;
return 0; }
Run Result: