Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- int test = sc.nextInt();
- while(test-- !=0)
- {
- String str = sc.next();
- int n = str.length();
- int sum = 0;
- for(int i = 0; i<n; i++)
- {
- switch(str.charAt(i))
- {
- case 'I':
- {
- if( i != n-1 && (str.charAt(i+1) == 'V' ||str.charAt(i+1) == 'X') )
- sum -= 1;
- else
- sum += 1;
- }
- break;
- case 'V':
- sum += 5;
- break;
- case 'X':
- {
- if(i != n-1 && (str.charAt(i+1) == 'L' ||str.charAt(i+1) == 'C' ))
- sum -= 10;
- else
- sum += 10;
- }
- break;
- case 'L':
- sum += 50;
- break;
- case 'C':
- {
- if(i != n-1 && (str.charAt(i+1) == 'D' ||str.charAt(i+1) == 'M' ))
- sum -= 100;
- else
- sum += 100;
- }
- break;
- case 'D':
- sum += 500;
- break;
- case 'M':
- sum += 1000;
- break;
- }
- }
- System.out.println(sum + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement