Guest User

Untitled

a guest
Apr 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //I V X L C D M
  4. //1 5 10 50 100 500 1000
  5. static constexpr const int val[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  6. static constexpr const char *str[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  7. class Solution {
  8. public:
  9. string intToRoman(int num) {
  10. string ans = "";
  11. for(int i = 0 ; i < 13 ; i++){
  12. while(num >= val[i]) ans += str[i], num -= val[i];
  13. }
  14. return ans;
  15. }
  16. };
  17.  
  18. int main(){
  19. Solution sol;
  20. cout << sol.intToRoman(1) << endl;
  21. cout << sol.intToRoman(1234) << endl;
  22. cout << sol.intToRoman(2345) << endl;
  23. cout << sol.intToRoman(3456) << endl;
  24. }
Add Comment
Please, Sign In to add comment