結果

問題 No.518 ローマ数字の和
ユーザー htensaihtensai
提出日時 2020-06-10 17:33:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,288 bytes
コンパイル時間 3,215 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 56,488 KB
最終ジャッジ日時 2023-09-05 13:31:29
合計ジャッジ時間 7,101 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,884 KB
testcase_01 AC 121 ms
55,356 KB
testcase_02 AC 120 ms
55,624 KB
testcase_03 AC 122 ms
56,000 KB
testcase_04 AC 120 ms
55,968 KB
testcase_05 AC 120 ms
55,752 KB
testcase_06 AC 121 ms
55,880 KB
testcase_07 AC 119 ms
55,976 KB
testcase_08 AC 129 ms
55,892 KB
testcase_09 AC 121 ms
56,036 KB
testcase_10 AC 122 ms
56,488 KB
testcase_11 AC 122 ms
55,796 KB
testcase_12 AC 122 ms
56,256 KB
testcase_13 AC 121 ms
55,732 KB
testcase_14 AC 123 ms
54,100 KB
testcase_15 AC 121 ms
55,840 KB
testcase_16 AC 122 ms
56,156 KB
testcase_17 AC 124 ms
55,828 KB
testcase_18 AC 121 ms
55,632 KB
testcase_19 AC 122 ms
55,800 KB
testcase_20 AC 122 ms
55,476 KB
testcase_21 AC 123 ms
55,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static HashMap<Character, Integer> romanToInteger = new HashMap<>();
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		romanToInteger.put('I', 1);
		romanToInteger.put('V', 5);
		romanToInteger.put('X', 10);
		romanToInteger.put('L', 50);
		romanToInteger.put('C', 100);
		romanToInteger.put('D', 500);
		romanToInteger.put('M', 1000);
		int sum = 0;
	    int n = sc.nextInt();
	    for (int i = 0; i < n; i++) {
	        sum += getInteger(sc.next());
	    }
	    if (sum >= 4000) {
	        System.out.println("ERROR");
	        return;
	    }
	    StringBuilder sb = new StringBuilder();
	    int first = sum / 1000;
	    for (int i = 0; i < first; i++) {
	        sb.append("M");
	    }
	    int second = sum % 1000 / 100;
	    if (second == 9) {
	        sb.append("CM");
	    } else if (second == 4) {
	        sb.append("CD");
	    } else {
	        if (second >= 5) {
	            second -= 5;
	            sb.append("D");
	        }
	        for (int i = 0; i < second; i++) {
	            sb.append("C");
	        }
	    }
	    int third = sum % 100 / 10;
	    if (third == 9) {
	        sb.append("XC");
	    } else if (third == 4) {
	        sb.append("XL");
	    } else {
	        if (third >= 5) {
	            third -= 5;
	            sb.append("L");
	        }
	        for (int i = 0; i < third; i++) {
	            sb.append("X");
	        }
	    }
	    int forth = sum % 10;
	    if (forth == 9) {
	        sb.append("IX");
	    } else if (forth == 4) {
	        sb.append("IV");
	    } else {
	        if (forth >= 5) {
	            forth -= 5;
	            sb.append("V");
	        }
	        for (int i = 0; i < forth; i++) {
	            sb.append("I");
	        }
	    }
	    System.out.println(sb);
	}
	
    static int getInteger(String s) {
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            int x = romanToInteger.get(c);
            if (stack.size() > 0 && stack.peek() < x) {
                stack.push(x - stack.pop());
            } else {
                stack.push(x);
            }
        }
        int ans = 0;
        while (stack.size() > 0) {
            ans += stack.pop();
        }
        return ans;
    }
}
0