fork download
  1. #include <bits/stdc++.h>
  2. #include <string>
  3. using namespace std;
  4.  
  5. /**
  6. Convert decimal number lying between 1-3999 to roman numerals
  7.  
  8. SYMBOL VALUE
  9. I 1
  10. II 2
  11. III 3
  12. IV 4
  13. V 5
  14. X 10
  15. L 50
  16. C 100
  17. D 500
  18. M 1000
  19. **/
  20.  
  21. vector<pair<int, string>> romanMapping = {
  22. {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
  23. {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
  24. {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}
  25. };
  26.  
  27. string roman(int n){
  28.  
  29. string rom = "";
  30. for(pair<int,string> &p : romanMapping) {
  31.  
  32. if(p.first < n) {
  33.  
  34. rom+= p.second;
  35. rom+= roman(n%(p.first));
  36.  
  37. break;
  38. }
  39. }
  40.  
  41. return rom;
  42.  
  43.  
  44. }
  45.  
  46. int main() {
  47. int intNum = 0;
  48. cin >> intNum;
  49. cout << roman(intNum);
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5272KB
stdin
10
stdout
IX