結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2017-05-28 22:16:41 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 26 ms / 2,000 ms |
コード長 | 3,888 bytes |
コンパイル時間 | 2,414 ms |
コンパイル使用メモリ | 116,160 KB |
実行使用メモリ | 18,176 KB |
最終ジャッジ日時 | 2024-09-21 15:32:53 |
合計ジャッジ時間 | 2,260 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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;using System.Collections.Generic;using System.Text;using System.Linq;class Program{public void Proc(){int MaxLimit = 3999;int itemCount = int.Parse(Reader.ReadLine());string[] inpt = new string[itemCount];inpt = Reader.ReadLine().Split(' ');int ans = 0;foreach (string roman in inpt){ans += RomanToArabic(roman);}if (ans > MaxLimit){Console.WriteLine("ERROR");}else{Console.WriteLine(this.ArabicToRoman(ans));}}private int RomanToArabic(string inpt){int idx = -1;int ans = 0;while (++idx < inpt.Length){if (idx < inpt.Length - 1){if (Conv(inpt[idx]) < Conv(inpt[idx+1])){ans += Conv(inpt[idx + 1]) - Conv(inpt[idx]);idx++;continue;}}ans += Conv(inpt[idx]);}return ans;}private string ArabicToRoman(int num){int calc = num;StringBuilder ans = new StringBuilder();int tmp = calc / 1000;for (int i = 0; i < tmp; i++){ans.Append("M");}calc = calc % 1000;tmp = calc / 100;if (tmp == 9){ans.Append("CM");tmp -= 9;}if (tmp >= 5){ans.Append("D");tmp -= 5;}if (tmp == 4){ans.Append("CD");tmp -= 4;}for (int i = 0; i < tmp; i++){ans.Append("C");}calc = calc % 100;tmp = calc / 10;if (tmp == 9){ans.Append("XC");tmp -= 9;}if (tmp >= 5){ans.Append("L");tmp -= 5;}if (tmp == 4){ans.Append("XL");tmp -= 4;}for (int i = 0; i < tmp; i++){ans.Append("X");}calc = calc % 10;tmp = calc;if (tmp == 9){ans.Append("IX");tmp -= 9;}if (tmp >= 5){ans.Append("V");tmp -= 5;}if (tmp == 4){ans.Append("IV");tmp -= 4;}for (int i = 0; i < tmp; i++){ans.Append("I");}return ans.ToString();}private int Conv(char c){// I = 1、V = 5、X = 10、L = 50、C = 100、D = 500、M = 1000switch (c){case 'I':return 1;case 'V':return 5;case 'X':return 10;case 'L':return 50;case 'C':return 100;case 'D':return 500;default:return 1000;}}public class Reader{public static bool IsDebug = false;private static String PlainInput = @"5IV XXI CCCXL III MMCMVI";private static System.IO.StringReader Sr = null;public static string ReadLine(){if (IsDebug){if (Sr == null){Sr = new System.IO.StringReader(PlainInput.Trim());}return Sr.ReadLine();}else{return Console.ReadLine();}}}static void Main(){#if DEBUGReader.IsDebug = true;#endifProgram prg = new Program();prg.Proc();}}