Advertisement
unknown_0711

Untitled

Feb 24th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5.  
  6. public class Main
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. Scanner sc = new Scanner(System.in);
  11. int test = sc.nextInt();
  12.  
  13. while(test-- !=0)
  14. {
  15. String str = sc.next();
  16. int n = str.length();
  17.  
  18. int sum = 0;
  19. for(int i = 0; i<n; i++)
  20. {
  21. switch(str.charAt(i))
  22. {
  23. case 'I':
  24. {
  25. if( i != n-1 && (str.charAt(i+1) == 'V' ||str.charAt(i+1) == 'X') )
  26. sum -= 1;
  27. else
  28. sum += 1;
  29. }
  30. break;
  31.  
  32. case 'V':
  33. sum += 5;
  34. break;
  35.  
  36. case 'X':
  37. {
  38. if(i != n-1 && (str.charAt(i+1) == 'L' ||str.charAt(i+1) == 'C' ))
  39. sum -= 10;
  40. else
  41. sum += 10;
  42. }
  43. break;
  44.  
  45. case 'L':
  46. sum += 50;
  47. break;
  48.  
  49. case 'C':
  50. {
  51. if(i != n-1 && (str.charAt(i+1) == 'D' ||str.charAt(i+1) == 'M' ))
  52. sum -= 100;
  53. else
  54. sum += 100;
  55. }
  56. break;
  57.  
  58. case 'D':
  59. sum += 500;
  60. break;
  61.  
  62. case 'M':
  63. sum += 1000;
  64. break;
  65.  
  66. }
  67. }
  68.  
  69. System.out.println(sum + " ");
  70. }
  71. }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement