結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
bluemegane
|
| 提出日時 | 2021-05-08 20:23:50 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 29 ms / 2,000 ms |
| コード長 | 2,171 bytes |
| コンパイル時間 | 2,555 ms |
| コンパイル使用メモリ | 116,404 KB |
| 実行使用メモリ | 26,460 KB |
| 最終ジャッジ日時 | 2024-09-17 04:35:40 |
| 合計ジャッジ時間 | 4,336 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System.Collections.Generic;
using System;
public class Hello
{
public static Dictionary<string, int> d, d2;
public static Dictionary<int ,string > d3, d4;
static void Main()
{
var a = new string[] { "I", "V", "X", "L", "C", "D", "M" };
var a2 = new int[] { 1, 5, 10, 50, 100, 500, 1000 };
var b = new string[] { "IV", "IX", "XL", "XC", "CD", "CM" };
var b2 = new int[] { 4, 9, 40, 90, 400, 900 };
d = new Dictionary<string, int>();
d2 = new Dictionary<string, int>();
d3 = new Dictionary<int, string>();
d4 = new Dictionary<int, string>();
for (int i = 0; i < a.Length; i++) { d[a[i]] = a2[i]; d3[a2[i]] = a[i]; }
for (int i = 0; i < b.Length; i++) { d2[b[i]] = b2[i]; d4[b2[i]] = b[i]; }
var n = int.Parse(Console.ReadLine().Trim());
string[] line = Console.ReadLine().Trim().Split(' ');
var sum = 0;
foreach (var x in line) sum += calc(x);
getAns(sum);
}
static int calc(string s)
{
var res = 0;
foreach (var x in d2)
{
var sL = s.Length;
s = s.Replace(x.Key, "");
var sL2 = s.Length;
res += (sL - sL2) / 2 * x.Value;
}
foreach (var x in s) res += d[x.ToString()];
return res;
}
static void getAns(int t)
{
if (t > 3999) { Console.WriteLine("ERROR"); return; }
var s = "";
var a = t / 1000;
s += new string('M', a);
t %= 1000;
var b = t / 100;
if (b == 9) s += d4[900];
else if (b >= 5) s += d3[500] + new string('C', b % 5);
else if (b == 4) s += d4[400];
else s += new string('C', b);
t %= 100;
var c = t / 10;
if (c == 9) s += d4[90];
else if (c >= 5) s += d3[50] + new string('X', c % 5);
else if (c == 4) s += d4[40];
else s += new string('X', c);
t %= 10;
if (t == 9) s += d4[9];
else if (t >= 5) s += d3[5] + new string('I', t % 5);
else if (t == 4) s += d4[4];
else s += new string('I', t);
Console.WriteLine(s);
}
}
bluemegane