Created
November 24, 2018 09:58
-
-
Save Shuumatsu/96cf216f1502f2797786863013fff8f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// 罗马数字共有 7 个,即 I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和 | |
// M(1000) | |
string transform(string accu, int n) { | |
if (n == 0) | |
return accu; | |
int ms = n / 1000; | |
if (ms > 0) | |
return transform(string(ms, 'M'), n - ms * 1000); | |
int cs = n / 100; | |
if (cs > 0) { | |
int r = n - cs * 100; | |
if (cs < 4) | |
return transform(accu + string(cs, 'C'), r); | |
if (cs == 4) | |
return transform(accu + "CD", r); | |
if (cs < 9) | |
return transform(accu + "D" + string(cs - 5, 'C'), r); | |
return transform(accu + "CM", r); | |
} | |
int xs = n / 10; | |
if (xs > 0) { | |
int r = n - xs * 10; | |
if (xs < 4) | |
return transform(accu + string(xs, 'X'), r); | |
if (xs == 4) | |
return transform(accu + "XL", r); | |
if (xs < 9) | |
return transform(accu + "L" + string(xs - 5, 'X'), r); | |
return transform(accu + "XC", r); | |
} | |
if (n < 4) | |
return accu + string(n, 'I'); | |
if (n == 4) | |
return accu + "IV"; | |
if (n < 9) | |
return accu + "V" + string(n - 5, 'I'); | |
return accu + "IX"; | |
} | |
void convertToRoman(int n) { | |
// Your code here | |
cout << transform("", n); | |
} | |
int main() { | |
for (int i = 1; i < 4000; i++) { | |
convertToRoman(i); | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment