結果

問題 No.518 ローマ数字の和
ユーザー tentententen
提出日時 2021-01-19 16:30:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,545 bytes
コンパイル時間 2,557 ms
コンパイル使用メモリ 74,344 KB
実行使用メモリ 58,080 KB
最終ジャッジ日時 2023-08-22 13:54:44
合計ジャッジ時間 6,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,972 KB
testcase_01 AC 121 ms
55,780 KB
testcase_02 AC 123 ms
56,192 KB
testcase_03 AC 121 ms
55,812 KB
testcase_04 AC 122 ms
55,512 KB
testcase_05 AC 123 ms
55,972 KB
testcase_06 AC 124 ms
56,092 KB
testcase_07 AC 126 ms
55,584 KB
testcase_08 AC 129 ms
56,076 KB
testcase_09 AC 124 ms
55,692 KB
testcase_10 AC 124 ms
58,080 KB
testcase_11 AC 124 ms
55,772 KB
testcase_12 AC 125 ms
55,876 KB
testcase_13 AC 124 ms
56,060 KB
testcase_14 AC 123 ms
56,012 KB
testcase_15 AC 125 ms
56,216 KB
testcase_16 AC 121 ms
55,980 KB
testcase_17 AC 122 ms
55,440 KB
testcase_18 AC 122 ms
56,312 KB
testcase_19 AC 123 ms
55,936 KB
testcase_20 AC 125 ms
56,140 KB
testcase_21 AC 125 ms
55,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int[] NUM = new int[]{1, 5, 10, 50, 100, 500, 1000};
    static final char[] ROMAN = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		for (int i = 0; i < n; i++) {
		    ans += getNum(sc.next());
		}
		if (ans > 3999) {
		    System.out.println("ERROR");
		} else {
		    System.out.println(getRoman(ans));
		}
	}
	
	static String getRoman(int x) {
	    StringBuilder ans = new StringBuilder();
	    if (x >= 1000) {
	        int count = x / 1000;
	        for (int i = 0; i < count; i++) {
	            ans.append("M");
	        }
	        x %= 1000;
	    }
	    if (x >= 100) {
	        int count = x / 100;
	        if (count == 9) {
	            ans.append("CM");
	            count = 0;
	        } else if (count == 4) {
	            ans.append("CD");
	            count = 0;
	        } else if (count >= 5) {
	            ans.append("D");
	            count -= 5;
	        }
	        for (int i = 0; i < count; i++) {
	            ans.append("C");
	        }
	        x %= 100;
	    }
	    if (x >= 10) {
	        int count = x / 10;
	        if (count == 9) {
	            ans.append("XC");
	            count = 0;
	        } else if (count == 4) {
	            ans.append("XL");
	            count = 0;
	        } else if (count >= 5) {
	            ans.append("L");
	            count -= 5;
	        }
	        for (int i = 0; i < count; i++) {
	            ans.append("X");
	        }
	        x %= 10;
	    }
	    if (x >= 1) {
	        int count = x;
	        if (count == 9) {
	            ans.append("IX");
	            count = 0;
	        } else if (count == 4) {
	            ans.append("IV");
	            count = 0;
	        } else if (count >= 5) {
	            ans.append("V");
	            count -= 5;
	        }
	        for (int i = 0; i < count; i++) {
	            ans.append("I");
	        }
	    }
	    return ans.toString();
	}
	
	static int getNum(String s) {
	    int stock = 0;
	    int ans = 0;
	    for (char c : s.toCharArray()) {
	        int x = getRomanToNum(c);
	        if (stock < x) {
	            stock = x - stock;
	        } else {
	            ans += stock;
	            stock = x;
	        }
	    }
	    ans += stock;
	    return ans;
	}
	
	static int getRomanToNum(char c) {
	    for (int i = 0; i < NUM.length; i++) {
	        if (c == ROMAN[i]) {
	            return NUM[i];
	        }
	    }
	    return 0;
	}
	
	
}
0