結果

問題 No.518 ローマ数字の和
ユーザー GrenacheGrenache
提出日時 2017-05-28 21:42:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,660 bytes
コンパイル時間 3,609 ms
コンパイル使用メモリ 78,980 KB
実行使用メモリ 57,732 KB
最終ジャッジ日時 2023-10-21 14:06:06
合計ジャッジ時間 7,783 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
57,524 KB
testcase_01 AC 133 ms
57,480 KB
testcase_02 AC 134 ms
57,440 KB
testcase_03 AC 134 ms
57,464 KB
testcase_04 AC 134 ms
57,508 KB
testcase_05 AC 132 ms
57,464 KB
testcase_06 AC 137 ms
57,472 KB
testcase_07 AC 134 ms
57,720 KB
testcase_08 AC 141 ms
57,448 KB
testcase_09 AC 134 ms
57,484 KB
testcase_10 AC 137 ms
57,500 KB
testcase_11 AC 140 ms
57,732 KB
testcase_12 AC 139 ms
57,528 KB
testcase_13 AC 136 ms
57,600 KB
testcase_14 AC 133 ms
57,524 KB
testcase_15 AC 133 ms
57,520 KB
testcase_16 AC 134 ms
57,496 KB
testcase_17 AC 135 ms
57,516 KB
testcase_18 AC 136 ms
57,540 KB
testcase_19 AC 136 ms
57,564 KB
testcase_20 AC 134 ms
57,480 KB
testcase_21 AC 133 ms
57,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder518 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		String[] r = new String[n];
		int ret = 0;
		for (int i = 0; i < n; i++) {
			r[i] = sc.next();

			ret += conv(r[i]);
		}

		if (ret > 3999) {
			pr.println("ERROR");
		} else {
			pr.println(rconv(ret));
//			pr.println(ret);
		}
	}

	private static String[] xxx = {"MMM", "MM", "M", "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C", "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"};
	private static int[] xxi = {3000, 2000, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

	private static int conv(String r) {
		int ret = 0;
		while (r.length() > 0) {
			for (int i = 0, size = xxx.length; i < size; i++) {
				if (r.startsWith(xxx[i])) {
					ret += xxi[i];
					r = r.substring(xxx[i].length());
					break;
				}
			}
		}

		return ret;
	}

	private static String rconv(int r) {
		StringBuilder ret = new StringBuilder();
		while (r > 0) {
			for (int i = 0, size = xxi.length; i < size; i++) {
				if (r >= xxi[i]) {
					ret.append(xxx[i]);
					r -= xxi[i];
					break;
				}
			}
		}

		return ret.toString();
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0