結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 21:44:13
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,342 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 77,312 KB
実行使用メモリ 57,736 KB
最終ジャッジ日時 2023-10-19 20:03:16
合計ジャッジ時間 5,968 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
57,160 KB
testcase_01 WA -
testcase_02 AC 132 ms
57,088 KB
testcase_03 AC 130 ms
57,572 KB
testcase_04 AC 131 ms
57,380 KB
testcase_05 AC 132 ms
57,416 KB
testcase_06 AC 134 ms
57,180 KB
testcase_07 AC 132 ms
57,428 KB
testcase_08 AC 168 ms
57,736 KB
testcase_09 AC 132 ms
57,536 KB
testcase_10 AC 134 ms
57,304 KB
testcase_11 AC 134 ms
57,276 KB
testcase_12 AC 132 ms
57,044 KB
testcase_13 WA -
testcase_14 AC 130 ms
56,968 KB
testcase_15 AC 130 ms
57,352 KB
testcase_16 AC 131 ms
57,420 KB
testcase_17 AC 131 ms
57,048 KB
testcase_18 WA -
testcase_19 AC 128 ms
57,088 KB
testcase_20 AC 136 ms
57,036 KB
testcase_21 AC 130 ms
57,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		String[] a = {"IV","IX","XL","XC","CD","CM"};
		String[] b = {"IIII","VIIII","XXXX","LXXXX","CCCC","DCCCC"};
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<n; i++) {
			String temp = sc.next();
			for (int j=0; j<6; j++) {
				temp = temp.replaceAll(a[j],b[j]);
			}
			sb.append(temp);
		}

		int total = 0;
		char[] ar = {'I','V','X','L','C','D','M'};
		int[] number = {1,5,10,50,100,500,1000};
		for (int i=0; i<sb.length(); i++) {
			for (int j=0; j<7; j++) {
				if (sb.charAt(i)==ar[j]) {total += number[j];}
			}
		}

		if (total > 3999) {System.out.println("ERROR");}
		else {
			int[] num = new int[7];
			for (int i=6; i>=0; i--) {
				num[i] = total/number[i];
				total = total%number[i];
			}


			StringBuilder ans = new StringBuilder();
			for (int i=6; i>=0; i--) {
				if (i==4 && num[4]==4) {ans.append("CD");}
				else if (i==3 && num[3]==1 && num[2]==4) {ans.append("XC"); i--;}
				else if (i==2 && num[2]==4) {ans.append("XL");}
				else if (i==1 && num[1]==1 && num[0]==9) {ans.append("IX"); i--;}
				else if (i==0 && num[0]==4) {ans.append("IV");}
				else {for (int j=0; j<num[i]; j++) {ans.append(ar[i]);}}
			}
			System.out.println(ans);
		}
	}
}
0