fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. using namespace std;
  5. using uint = unsigned int;
  6. using cstr = const char *;
  7.  
  8. const pair<uint, cstr> roman_numbers[]
  9. {
  10. { 1000, "M" },
  11. { 900, "CM" },
  12. { 500, "D" },
  13. { 400, "CD" },
  14. { 100, "C" },
  15. { 90, "XC" },
  16. { 50, "L" },
  17. { 40, "XL" },
  18. { 10, "X" },
  19. { 9, "IX" },
  20. { 5, "V" },
  21. { 4, "IV" },
  22. { 1, "I" },
  23. };
  24.  
  25. string dec_to_roman(uint num)
  26. {
  27. string result;
  28. uint divisor, digits;
  29. cstr roman;
  30. for (auto& dr_pair : roman_numbers) {
  31. tie(divisor, roman) = dr_pair;
  32. digits = num / divisor;
  33. num -= digits * divisor;
  34. for (uint j = 0; j < digits; ++j)
  35. result += roman;
  36. }
  37. return result;
  38. }
  39.  
  40. int main() {
  41. for (uint i = 0; i < 101; ++i)
  42. cout << dec_to_roman(i) << endl;
  43. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII
XIII
XIV
XV
XVI
XVII
XVIII
XIX
XX
XXI
XXII
XXIII
XXIV
XXV
XXVI
XXVII
XXVIII
XXIX
XXX
XXXI
XXXII
XXXIII
XXXIV
XXXV
XXXVI
XXXVII
XXXVIII
XXXIX
XL
XLI
XLII
XLIII
XLIV
XLV
XLVI
XLVII
XLVIII
XLIX
L
LI
LII
LIII
LIV
LV
LVI
LVII
LVIII
LIX
LX
LXI
LXII
LXIII
LXIV
LXV
LXVI
LXVII
LXVIII
LXIX
LXX
LXXI
LXXII
LXXIII
LXXIV
LXXV
LXXVI
LXXVII
LXXVIII
LXXIX
LXXX
LXXXI
LXXXII
LXXXIII
LXXXIV
LXXXV
LXXXVI
LXXXVII
LXXXVIII
LXXXIX
XC
XCI
XCII
XCIII
XCIV
XCV
XCVI
XCVII
XCVIII
XCIX
C