結果

問題 No.518 ローマ数字の和
ユーザー Naoyk1212Naoyk1212
提出日時 2017-05-28 23:08:00
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,646 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 165,504 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-21 14:34:19
合計ジャッジ時間 2,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 WA -
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
	int n;
	cin >> n;
	vector<string> r(n);
	for(int i = 0; i < n; i++)cin >> r[i];

	int ans = 0;

	for(int i = 0; i < n; i++){
		int sum = 0;
		for(int j = 0; j < r[i].length(); j++){
			if(r[i][j] == 'I'){
				if(j < n - 1){
					if(r[i][j + 1] == 'V'){
						sum += 4;
						j++;
					}else if(r[i][j + 1] == 'X'){
						sum += 9;
						j++;
					}else sum++;
				}else{
					sum++;
				}
			}else if(r[i][j] == 'X'){
				if(j < n - 1){
					if(r[i][j + 1] == 'L'){
						sum += 40;
						j++;
					}else if(r[i][j + 1] == 'C'){
						sum += 90;
						j++;
					}else sum += 10;
				}else{
					sum += 10;
				}
			}else if(r[i][j] == 'C'){
				if(j < n - 1){
					if(r[i][j + 1] == 'D'){
						sum += 400;
						j++;
					}else if(r[i][j + 1] == 'M'){
						sum += 900;
						j++;
					}else sum += 100;
				}else{
					sum += 100;
				}
			}else if(r[i][j] == 'V')sum += 5;
			else if(r[i][j] == 'L')sum += 50;
			else if(r[i][j] == 'D')sum += 500;
			else sum += 1000;
		}
		ans += sum;
		cerr << sum << endl;
	}
	

	string hundred[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
	string ten[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
	string one[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "XI"};

	if(ans >= 4000)cout << "ERROR" << endl;
	else{
		int a = ans / 1000;
		if(a != 0)for(int i = 0; i < a; i++)cout << 'M';
		
		ans %= 1000;
		a = ans / 100;
		if(a != 0)cout << hundred[a - 1];

		ans %= 100;
		a = ans / 10;
		if(a != 0)cout << ten[a - 1];

		a = ans % 10;
		if(a != 0)cout << one[a - 1] << endl;
	}
}
0