結果

問題 No.518 ローマ数字の和
ユーザー uafr_cs
提出日時 2017-10-30 15:49:36
言語 Java
(openjdk 23)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 2,505 bytes
コンパイル時間 2,638 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 41,580 KB
最終ジャッジ日時 2024-11-22 10:39:53
合計ジャッジ時間 6,410 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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