Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class test {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String str = in.nextLine();
- String[] strParts = str.split(" ");
- StringBuffer sb = new StringBuffer("abcd");
- for (int i = 0; i < strParts.length; i++) {
- if (strParts[i].equals(""))
- continue;
- boolean f = true;
- for (int j = 0; j < strParts[i].length(); j++) {
- if (strParts[i].charAt(j) == 'M' || strParts[i].charAt(j) == 'D' || strParts[i].charAt(j) == 'C' || strParts[i].charAt(j) == 'L' || strParts[i].charAt(j) == 'X' || strParts[i].charAt(j) == 'V' || strParts[i].charAt(j) == 'I')
- sb = new StringBuffer(strParts[i]);
- else
- f = false;
- }
- if (f) {
- int res = 0;
- if (sb.indexOf("CM") >= 0) {
- res = res + 900;
- sb.delete(sb.indexOf("CM"), sb.indexOf("CM") + 2);
- }
- if (sb.indexOf("CD") >= 0) {
- res = res + 400;
- sb.delete(sb.indexOf("CD"), sb.indexOf("CD") + 2);
- }
- if (sb.indexOf("XC") >= 0) {
- res = res + 90;
- sb.delete(sb.indexOf("XC"), sb.indexOf("XC") + 2);
- }
- if (sb.indexOf("XL") >= 0) {
- res = res + 40;
- sb.delete(sb.indexOf("XL"), sb.indexOf("XL") + 2);
- }
- if (sb.indexOf("IX") >= 0) {
- res = res + 9;
- sb.delete(sb.indexOf("IX"), sb.indexOf("IX") + 2);
- }
- if (sb.indexOf("IV") >= 0) {
- res = res + 4;
- sb.delete(sb.indexOf("IV"), sb.indexOf("IV") + 2);
- }
- for (int k = 0; k < sb.length(); k++) {
- switch (sb.charAt(k)) {
- case 'M':
- res += 1000;
- break;
- case 'D':
- res += 500;
- break;
- case 'C':
- res += 100;
- break;
- case 'L':
- res += 50;
- break;
- case 'X':
- res += 10;
- break;
- case 'V':
- res += 5;
- break;
- default:
- res += 1;
- }
- }
- strParts[i] = "" + res;
- }
- }
- for (int j = 0; j < strParts.length; j++) {
- System.out.print(strParts[j] + " ");
- }
- }
- }
Add Comment
Please, Sign In to add comment