結果

問題 No.518 ローマ数字の和
ユーザー uafr_csuafr_cs
提出日時 2017-10-30 15:49:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 2,505 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 77,172 KB
実行使用メモリ 40,004 KB
最終ジャッジ日時 2023-08-14 13:08:11
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
39,396 KB
testcase_01 AC 120 ms
40,004 KB
testcase_02 AC 122 ms
39,952 KB
testcase_03 AC 124 ms
39,804 KB
testcase_04 AC 121 ms
39,216 KB
testcase_05 AC 120 ms
39,472 KB
testcase_06 AC 122 ms
39,704 KB
testcase_07 AC 120 ms
39,492 KB
testcase_08 AC 133 ms
39,900 KB
testcase_09 AC 123 ms
39,596 KB
testcase_10 AC 123 ms
39,336 KB
testcase_11 AC 122 ms
39,204 KB
testcase_12 AC 121 ms
39,832 KB
testcase_13 AC 123 ms
39,528 KB
testcase_14 AC 118 ms
39,224 KB
testcase_15 AC 121 ms
39,932 KB
testcase_16 AC 122 ms
39,920 KB
testcase_17 AC 122 ms
39,828 KB
testcase_18 AC 120 ms
39,484 KB
testcase_19 AC 122 ms
39,460 KB
testcase_20 AC 121 ms
39,772 KB
testcase_21 AC 114 ms
39,404 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