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