Advertisement
Guest User

Untitled

a guest
May 17th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. public class Part5 {
  2.  
  3. // 1 - I
  4. // 5 - V
  5. // 10 - X
  6. // 50 - L
  7. // 100 - C
  8. // 500 - D
  9. // 1000- M
  10.  
  11. public static void main(String[] args) {
  12. // System.out.println(roman2Decimal("MMMCMXCIX"));
  13. // System.out.println(decimal2Roman(3999));
  14. long startTime = System.currentTimeMillis();
  15.  
  16. for(int a = 0; a < 1000000; a++) {
  17. Part5.roman2Decimal("IV");
  18. Part5.decimal2Roman(4);
  19. Part5.roman2Decimal("XCIV");
  20. Part5.decimal2Roman(94);
  21. Part5.roman2Decimal("XCV");
  22. Part5.decimal2Roman(95);
  23. Part5.roman2Decimal("XCVI");
  24. Part5.decimal2Roman(96);
  25. Part5.roman2Decimal("XCVII");
  26. Part5.decimal2Roman(97);
  27. Part5.roman2Decimal("XCVIII");
  28. Part5.decimal2Roman(98);
  29. Part5.roman2Decimal("XCIX");
  30. Part5.decimal2Roman(99);
  31. Part5.roman2Decimal("C");
  32. Part5.decimal2Roman(100);
  33. }
  34. long timeSpent = System.currentTimeMillis() - startTime;
  35. System.out.println("программа выполнялась " + timeSpent + " миллисекунд");
  36.  
  37. }
  38.  
  39. public static int roman2Decimal(String s) {
  40. int Decimal = 0;
  41. char Previous = 0;
  42.  
  43. for (int x = 0; x < s.length(); x++) {
  44. if (s.charAt(x) == 'I')
  45. Decimal += 1;
  46.  
  47. if (s.charAt(x) == 'V') {
  48. if (Previous == 'I') {
  49. Decimal -= 2;
  50. }
  51. Decimal += 5;
  52. }
  53.  
  54. if (s.charAt(x) == 'X') {
  55. if (Previous == 'I') {
  56. Decimal -= 2;
  57. }
  58. Decimal += 10;
  59. }
  60.  
  61. if (s.charAt(x) == 'L') {
  62. if (Previous == 'X') {
  63. Decimal -= 20;
  64. }
  65. Decimal += 50;
  66. }
  67.  
  68. if (s.charAt(x) == 'C') {
  69. if (Previous == 'X') {
  70. Decimal -= 20;
  71. }
  72. Decimal += 100;
  73. }
  74.  
  75. if (s.charAt(x) == 'D') {
  76. if (Previous == 'C') {
  77. Decimal -= 200;
  78. }
  79. Decimal += 500;
  80. }
  81.  
  82. if (s.charAt(x) == 'M') {
  83. if (Previous == 'C') {
  84. Decimal -= 200;
  85. }
  86. Decimal += 1000;
  87. }
  88. Previous = s.charAt(x);
  89. }
  90. return Decimal;
  91. }
  92.  
  93. public static String decimal2Roman(int x) {
  94. if (x >= 4000 || x <= 0)
  95. return null;
  96. StringBuilder result = new StringBuilder();
  97. for (Integer key : units.descendingKeySet()) {
  98. while (x >= key) {
  99. x -= key;
  100. result.append(units.get(key));
  101. }
  102. }
  103. return result.toString();
  104. }
  105.  
  106. private static final NavigableMap<Integer, String> units;
  107. static {
  108. NavigableMap<Integer, String> initMap = new TreeMap<>();
  109. initMap.put(1000, "M");
  110. initMap.put(900, "CM");
  111. initMap.put(500, "D");
  112. initMap.put(400, "CD");
  113. initMap.put(100, "C");
  114. initMap.put(90, "XC");
  115. initMap.put(50, "L");
  116. initMap.put(40, "XL");
  117. initMap.put(10, "X");
  118. initMap.put(9, "IX");
  119. initMap.put(5, "V");
  120. initMap.put(4, "IV");
  121. initMap.put(1, "I");
  122. units = Collections.unmodifiableNavigableMap(initMap);
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement