結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 23:18:53
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 1,293 Byte
コンパイル時間 2,064 ms
使用メモリ 38,448 KB
最終ジャッジ日時 2022-06-11 06:46:53
合計ジャッジ時間 5,268 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 97 ms
37,332 KB
testcase_01 AC 100 ms
37,540 KB
testcase_02 AC 95 ms
37,400 KB
testcase_03 AC 99 ms
37,520 KB
testcase_04 AC 94 ms
37,572 KB
testcase_05 AC 94 ms
37,392 KB
testcase_06 AC 101 ms
37,672 KB
testcase_07 AC 98 ms
37,412 KB
testcase_08 AC 117 ms
38,448 KB
testcase_09 AC 94 ms
37,376 KB
testcase_10 AC 96 ms
37,368 KB
testcase_11 AC 101 ms
37,672 KB
testcase_12 AC 99 ms
37,536 KB
testcase_13 AC 101 ms
37,512 KB
testcase_14 AC 100 ms
37,480 KB
testcase_15 AC 95 ms
37,384 KB
testcase_16 AC 101 ms
37,480 KB
testcase_17 AC 96 ms
37,344 KB
testcase_18 AC 96 ms
37,332 KB
testcase_19 AC 95 ms
37,644 KB
testcase_20 AC 94 ms
37,540 KB
testcase_21 AC 93 ms
37,356 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();
		int total = 0;
		for (int i=0; i<n; i++) {
			total += RomanToArabic(sc.next());
		}
		System.out.println(total>3999?"ERROR":ArabicToRoman(total));
	}
	
	static int RomanToArabic (String s) {
		String[] a = {"IV","IX","XL","XC","CD","CM"};
		String[] b = {"IIII","VIIII","XXXX","LXXXX","CCCC","DCCCC"};
		for (int j=0; j<6; j++) {
			s = s.replaceAll(a[j],b[j]);
		}
		int n = 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<s.length(); i++) {
			for (int j=0; j<13; j++) {
				if (s.substring(i,i+1).equals(roma[j])) {n += number[j];}
			}
		}
		if (n > 3999) {return -1;}
		else {return n;}
	}
	
	static String ArabicToRoman (int n) {
		if (n<0 || 3999<n) {return "error";}
		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"};
		StringBuilder ans = new StringBuilder();
		for (int i=0; i<13; i++) {
			int ii = n/number[i];
			for (int j=0; j<ii; j++) {
				ans.append(roma[i]);
			}
			n = n%number[i];
		}
		return ans.toString();
	}
}
0