qradmanq

вя 4.6

Nov 12th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. int FromRoman(string n)
  9. {
  10. map<char, int> m;
  11. m['I'] = 1;
  12. m['V'] = 5;
  13. m['X'] = 10;
  14. m['L'] = 50;
  15. m['C'] = 100;
  16. m['D'] = 500;
  17. m['M'] = 1000;
  18.  
  19. int tmp = 0;
  20. int res = 0;
  21. for (char c : n)
  22. {
  23. int n = m[c];
  24. if (n < tmp)
  25. {
  26. res = res + tmp;
  27. tmp = n;
  28. }
  29. else if (n > tmp)
  30. {
  31. if (tmp == 0)
  32. tmp = n;
  33. else
  34. {
  35. res = res + (n - tmp);
  36. tmp = 0;
  37. }
  38. }
  39. else if (n == tmp)
  40. {
  41. res = res + (tmp + n);
  42. tmp = 0;
  43. }
  44. }
  45. return res + tmp;
  46. }
  47.  
  48. int main()
  49. {
  50. string n;
  51. cin >> n;
  52. cout << FromRoman(n);
  53. }
Add Comment
Please, Sign In to add comment