結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 22:23:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,062 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 77,440 KB
実行使用メモリ 57,844 KB
最終ジャッジ日時 2023-10-19 23:35:31
合計ジャッジ時間 6,099 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,532 KB
testcase_01 AC 133 ms
57,580 KB
testcase_02 AC 134 ms
57,456 KB
testcase_03 AC 136 ms
57,488 KB
testcase_04 AC 135 ms
57,448 KB
testcase_05 AC 135 ms
57,468 KB
testcase_06 AC 138 ms
57,588 KB
testcase_07 AC 138 ms
57,428 KB
testcase_08 AC 161 ms
57,844 KB
testcase_09 AC 137 ms
57,568 KB
testcase_10 AC 138 ms
57,432 KB
testcase_11 AC 137 ms
57,488 KB
testcase_12 AC 133 ms
57,260 KB
testcase_13 AC 137 ms
57,576 KB
testcase_14 AC 134 ms
57,444 KB
testcase_15 AC 133 ms
57,480 KB
testcase_16 AC 133 ms
57,540 KB
testcase_17 AC 134 ms
57,332 KB
testcase_18 AC 134 ms
57,692 KB
testcase_19 AC 138 ms
57,584 KB
testcase_20 AC 133 ms
57,680 KB
testcase_21 AC 135 ms
57,584 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;
		int[] number = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
		String[] roma = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		for (int i=0; i<sb.length(); i++) {
			for (int j=0; j<13; j++) {
				if (sb.substring(i,i+1).equals(roma[j])) {total += number[j];}
			}
		}
		
		if (total > 3999) {System.out.println("ERROR");}
		else {
			StringBuilder ans = new StringBuilder();
			for (int i=0; i<13; i++) {
				int ii = total/number[i];
				for (int j=0; j<ii; j++) {
					ans.append(roma[i]);
				}
				total = total%number[i];
			}
			System.out.println(ans);
		}
	}
}
0