結果

問題 No.518 ローマ数字の和
ユーザー uafr_csuafr_cs
提出日時 2017-10-30 15:49:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 2,505 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 81,696 KB
実行使用メモリ 41,428 KB
最終ジャッジ日時 2024-05-02 01:22:11
合計ジャッジ時間 6,683 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
41,404 KB
testcase_01 AC 141 ms
41,364 KB
testcase_02 AC 142 ms
41,172 KB
testcase_03 AC 144 ms
41,428 KB
testcase_04 AC 138 ms
41,000 KB
testcase_05 AC 139 ms
41,092 KB
testcase_06 AC 142 ms
41,420 KB
testcase_07 AC 140 ms
41,116 KB
testcase_08 AC 145 ms
41,320 KB
testcase_09 AC 147 ms
41,420 KB
testcase_10 AC 136 ms
41,180 KB
testcase_11 AC 139 ms
41,204 KB
testcase_12 AC 144 ms
41,340 KB
testcase_13 AC 143 ms
41,304 KB
testcase_14 AC 140 ms
41,272 KB
testcase_15 AC 142 ms
41,376 KB
testcase_16 AC 143 ms
41,364 KB
testcase_17 AC 142 ms
41,352 KB
testcase_18 AC 140 ms
41,104 KB
testcase_19 AC 140 ms
41,204 KB
testcase_20 AC 138 ms
41,040 KB
testcase_21 AC 141 ms
41,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {

	public static final String[] strs = {"CM", "M", "CD", "D", "XC", "C", "XL", "L", "IX", "X", "IV", "V", "I"};
	public static final int[] values = {900, 1000, 400, 500, 90, 100, 40, 50, 9, 10, 4, 5, 1};
	
	public static int decode(String input){
		if(input.startsWith("CM")){
			return  900 + decode(input.substring(2));
		}else if(input.startsWith("M")){
			return 1000 + decode(input.substring(1));
		}else if(input.startsWith("CD")){
			return 400  + decode(input.substring(2));
		}else if(input.startsWith("D")){
			return 500 + decode(input.substring(1));
		}else if(input.startsWith("XC")){
			return  90 + decode(input.substring(2));
		}else if(input.startsWith("C")){
			return 100 + decode(input.substring(1));
		}else if(input.startsWith("XL")){
			return  40 + decode(input.substring(2));
		}else if(input.startsWith("L")){
			return  50 + decode(input.substring(1));
		}else if(input.startsWith("IX")){
			return   9 + decode(input.substring(2));
		}else if(input.startsWith("X")){
			return  10 + decode(input.substring(1));
		}else if(input.startsWith("IV")){
			return   4 + decode(input.substring(2));
		}else if(input.startsWith("V")){
			return   5 + decode(input.substring(1));
		}else if(input.startsWith("I")){
			return   1 + decode(input.substring(1));
		}else{
			return   0;
		}
	}
	
	public static String encode(int n){
		if(n >= 1000){
			return "M"  + encode(n - 1000);
		}else if(n >= 900){
			return "CM" + encode(n - 900);
		}else if(n >= 500){
			return "D"  + encode(n - 500);
		}else if(n >= 400){
			return "CD" + encode(n - 400);
		}else if(n >= 100){
			return "C"  + encode(n - 100);
		}else if(n >=  90){
			return "XC" + encode(n - 90);
		}else if(n >= 50){
			return "L"  + encode(n - 50);
		}else if(n >= 40){
			return "XL" + encode(n - 40);
		}else if(n >= 10){
			return "X"  + encode(n - 10);
		}else if(n >= 9){
			return "IX" + encode(n - 9);
		}else if(n >= 5){
			return "V"  + encode(n - 5);
		}else if(n >= 4){
			return "IV" + encode(n - 4);
		}else if(n >= 1){
			return "I"  + encode(n - 1);
		}else{
			return "";
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final int N = sc.nextInt();
		
		int sum = 0;
		for(int i = 0; i < N; i++){
			final int num = decode(sc.next());
			sum += num;
		}
		
		if(sum > 3999){
			System.out.println("ERROR");
		}else{
			System.out.println(encode(sum));
		}
		
	}
}
0