結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 21:50:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 77,716 KB
実行使用メモリ 57,852 KB
最終ジャッジ日時 2023-10-19 22:41:41
合計ジャッジ時間 6,599 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
57,440 KB
testcase_01 AC 139 ms
57,468 KB
testcase_02 AC 138 ms
57,492 KB
testcase_03 AC 139 ms
57,496 KB
testcase_04 AC 140 ms
57,524 KB
testcase_05 AC 141 ms
57,520 KB
testcase_06 AC 139 ms
57,520 KB
testcase_07 AC 140 ms
57,468 KB
testcase_08 AC 166 ms
57,852 KB
testcase_09 AC 141 ms
57,520 KB
testcase_10 AC 144 ms
57,164 KB
testcase_11 AC 145 ms
57,660 KB
testcase_12 AC 141 ms
57,468 KB
testcase_13 AC 142 ms
57,488 KB
testcase_14 AC 142 ms
57,520 KB
testcase_15 AC 141 ms
57,732 KB
testcase_16 AC 141 ms
57,492 KB
testcase_17 AC 142 ms
57,252 KB
testcase_18 AC 141 ms
57,684 KB
testcase_19 AC 138 ms
57,472 KB
testcase_20 AC 141 ms
57,464 KB
testcase_21 AC 138 ms
57,208 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==5 && num[5]==1 && num[4]==4) {ans.append("CM"); num[4]=0;}
				else if (i==4 && num[4]==4) {ans.append("CD");}
				else if (i==3 && num[3]==1 && num[2]==4) {ans.append("XC"); num[3]=0; num[2]=0;}
				else if (i==2 && num[2]==4) {ans.append("XL"); num[2]=0;}
				else if (i==1 && num[1]==1 && num[0]==4) {ans.append("IX"); i--; num[1]=0; num[0]=0;}
				else if (i==0 && num[0]==4) {ans.append("IV"); num[0]=0;}
				else {for (int j=0; j<num[i]; j++) {ans.append(ar[i]);}}
			}
			System.out.println(ans);
		}
	}
}
0