document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.*;
  2. public class cpromJ {
  3.     public static void main(String [] args) {
  4.         Scanner scn = new Scanner(System.in);
  5.         //as I, V, X, L, C, D and M has a single value such as 1, 5, 10, 50, 100, 500 and 1000
  6.         int i = 1,v = 5, X = 10, l = 50, c = 100, d = 500, m = 1000;
  7.        
  8.         while(scn.hasNextLine()) {
  9.             String s = scn.nextLine();
  10.             StringTokenizer st = new StringTokenizer(s, " ");
  11.             boolean subtract = true;
  12.             while(st.hasMoreTokens()) {
  13.                 String sub = st.nextToken();
  14.                 int arr [] = new int[sub.length()];
  15.                 for(int x=0;x<sub.length();x++) {
  16.                     if(sub.charAt(x)== \'I\') {
  17.                         arr[x] = i;
  18.                     }
  19.                     else if(sub.charAt(x)== \'V\') {
  20.                         arr[x] = v;
  21.                     }
  22.                     else if(sub.charAt(x)== \'X\') {
  23.                         arr[x] = X;
  24.                     }
  25.                     else if(sub.charAt(x)== \'L\') {
  26.                         arr[x] = l;
  27.                     }
  28.                     else if(sub.charAt(x)== \'C\') {
  29.                         arr[x] = c;
  30.                     }
  31.                     else if(sub.charAt(x)== \'D\') {
  32.                         arr[x] = d;
  33.                     }
  34.                     else if(sub.charAt(x)== \'M\') {
  35.                         arr[x] = m;
  36.                     }
  37.                 }
  38.                 int total=0;
  39.                 if(subtract) {
  40.                     for(int x=0;x<arr.length;x++) {
  41.                         if(arr[x] < arr[x+1]) {
  42.                             if(subtract) {
  43.                                 subtract = false;
  44.                                 total = arr[x+1]-arr[x];
  45.                                 x++;
  46.                             }
  47.                             else {
  48.                                 total += arr[x];
  49.                             }
  50.                         }
  51.                         else {
  52.                             total += arr[x];
  53.                         }
  54.                     }
  55.                 }
  56.                 else {
  57.                     for(int x=0;x<arr.length;x++) {
  58.                             total += arr[x];
  59.                     }
  60.                 }
  61.                 System.out.println(total);
  62.             }
  63.         }
  64.     }
  65. }
');