結果
| 問題 | 
                            No.518 ローマ数字の和
                             | 
                    
| コンテスト | |
| ユーザー | 
                             hotpepsi
                         | 
                    
| 提出日時 | 2017-06-17 22:45:52 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 945 bytes | 
| コンパイル時間 | 596 ms | 
| コンパイル使用メモリ | 60,792 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-01 11:48:08 | 
| 合計ジャッジ時間 | 1,237 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main(int argc, char *argv[]) {
	int n, tot = 0, vtbl[256] = {};
	vtbl['I'] = 1, vtbl['V'] = 5;
	vtbl['X'] = 10, vtbl['L'] = 50;
	vtbl['C'] = 100, vtbl['D'] = 500;
	vtbl['M'] = 1000;
	cin >> n;
	string s, digits = "IVXLCDM";
	for (int i = 0; i < n; ++i) {
		cin >> s;
		int prev = 5000;
		for (char c : s) {
			if (vtbl[c] > prev) {
				tot -= prev * 2;
			}
			tot += vtbl[c];
			prev = vtbl[c];
		}
	}
	string ans;
	if (tot >= 4000) {
		ans = "ERROR";
	} else {
		for (int i = 6; i >= 0; --i) {
			while (tot >= vtbl[digits[i]]) {
				ans += digits[i];
				tot -= vtbl[digits[i]];
			}
			int j = 2 - (i % 2);
			if (i >= j && tot >= vtbl[digits[i]] - vtbl[digits[i - j]]) {
				ans += digits[i - j];
				ans += digits[i];
				tot -= vtbl[digits[i]] - vtbl[digits[i - j]];
			}
		}
	}
	cout << ans << endl;
	return 0;
}
            
            
            
        
            
hotpepsi