結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 23:18:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,293 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 77,604 KB
実行使用メモリ 57,656 KB
最終ジャッジ日時 2023-10-21 13:34:17
合計ジャッジ時間 5,995 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
57,404 KB
testcase_01 AC 117 ms
55,608 KB
testcase_02 AC 100 ms
56,264 KB
testcase_03 AC 109 ms
57,256 KB
testcase_04 AC 107 ms
55,292 KB
testcase_05 AC 111 ms
57,524 KB
testcase_06 AC 114 ms
57,400 KB
testcase_07 AC 117 ms
57,128 KB
testcase_08 AC 177 ms
57,656 KB
testcase_09 AC 112 ms
57,356 KB
testcase_10 AC 128 ms
57,408 KB
testcase_11 AC 125 ms
57,468 KB
testcase_12 AC 139 ms
57,520 KB
testcase_13 AC 134 ms
57,452 KB
testcase_14 AC 117 ms
57,464 KB
testcase_15 AC 117 ms
57,464 KB
testcase_16 AC 114 ms
57,452 KB
testcase_17 AC 120 ms
57,460 KB
testcase_18 AC 119 ms
57,532 KB
testcase_19 AC 109 ms
57,412 KB
testcase_20 AC 111 ms
57,076 KB
testcase_21 AC 112 ms
57,308 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