結果

問題 No.518 ローマ数字の和
ユーザー Amanita2016Amanita2016
提出日時 2017-07-04 20:24:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 83,968 KB
実行使用メモリ 41,832 KB
最終ジャッジ日時 2024-04-15 16:13:03
合計ジャッジ時間 6,509 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
41,568 KB
testcase_01 AC 133 ms
41,264 KB
testcase_02 AC 133 ms
41,264 KB
testcase_03 AC 133 ms
41,400 KB
testcase_04 AC 136 ms
41,180 KB
testcase_05 AC 139 ms
41,832 KB
testcase_06 AC 134 ms
41,160 KB
testcase_07 AC 140 ms
41,552 KB
testcase_08 AC 133 ms
41,408 KB
testcase_09 AC 140 ms
41,540 KB
testcase_10 AC 135 ms
41,248 KB
testcase_11 AC 136 ms
41,412 KB
testcase_12 AC 134 ms
41,644 KB
testcase_13 AC 132 ms
41,692 KB
testcase_14 AC 131 ms
41,384 KB
testcase_15 AC 133 ms
41,228 KB
testcase_16 AC 136 ms
41,412 KB
testcase_17 AC 134 ms
41,676 KB
testcase_18 AC 133 ms
41,720 KB
testcase_19 AC 137 ms
41,148 KB
testcase_20 AC 133 ms
41,296 KB
testcase_21 AC 134 ms
41,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String roma = sc.nextLine();
		roma = sc.nextLine();
		int total = 0;
		String[] spl = roma.split(" ");
		for(int i = 0 ; i < spl.length ; i++){
			int subTotal = 0;
			for(int j = 0 ; j < spl[i].length() ; j++){
				int num = 0;
				switch(spl[i].charAt(j)){
				case 'I' :
					num = 1;
					break;
				case 'V' :
					num = 5;
					break;
				case 'X' :
					num = 10;
					break;
				case 'L' :
					num = 50;
					break;
				case 'C' :
					num = 100;
					break;
				case 'D' :
					num = 500;
					break;
				case 'M' :
					num = 1000;
					break;
					
				}
				if(num > subTotal && subTotal !=0){
					total += num - subTotal;
					subTotal = 0;
				}else{
					total += subTotal;
					subTotal = num;
				}
			}
			total += subTotal;
		}
		int[] numArray = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
		String[] romaArray = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		int work = total;
		String disp = "";
		for(int i = 0 ; i < numArray.length ; i++){
			int work2 = work/numArray[i];
			for(int j = 0 ; j < work2 ; j++){
				disp+=romaArray[i];
			}
			work %= numArray[i];
		}
		if(total <= 3999){
			System.out.println(disp);
		}else{
			System.out.println("ERROR");
		}
	}

}
0