結果

問題 No.518 ローマ数字の和
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-21 23:18:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 1,293 bytes
コンパイル時間 2,329 ms
コンパイル使用メモリ 77,684 KB
実行使用メモリ 42,492 KB
最終ジャッジ日時 2024-09-21 14:49:25
合計ジャッジ時間 6,317 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,356 KB
testcase_01 AC 132 ms
41,424 KB
testcase_02 AC 133 ms
41,140 KB
testcase_03 AC 138 ms
41,680 KB
testcase_04 AC 134 ms
40,236 KB
testcase_05 AC 135 ms
41,328 KB
testcase_06 AC 136 ms
41,480 KB
testcase_07 AC 120 ms
41,936 KB
testcase_08 AC 175 ms
42,492 KB
testcase_09 AC 134 ms
41,496 KB
testcase_10 AC 134 ms
41,324 KB
testcase_11 AC 136 ms
41,820 KB
testcase_12 AC 135 ms
41,468 KB
testcase_13 AC 133 ms
41,364 KB
testcase_14 AC 134 ms
41,184 KB
testcase_15 AC 135 ms
41,448 KB
testcase_16 AC 134 ms
41,444 KB
testcase_17 AC 135 ms
41,384 KB
testcase_18 AC 137 ms
41,380 KB
testcase_19 AC 133 ms
41,324 KB
testcase_20 AC 133 ms
41,172 KB
testcase_21 AC 132 ms
41,280 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